STL algorithm merge() function to concatenate char arrays


 
Thread Tools Search this Thread
Top Forums Programming STL algorithm merge() function to concatenate char arrays
# 1  
Old 05-06-2012
STL algorithm merge() function to concatenate char arrays

When the STL generic algorithm's merge() function is used to merge two char arrays, the output is not as expected. Below is the program I tried with.

Code:
#include <iostream>
#include <algorithm>
#include <cstring>
#include <deque>
#include <iterator>

using namespace std;

int main() {
    char str[] = "Hello";
    char srch[] = "World";
    char merg[strlen(str) + strlen(srch) + 1];
    std::deque<char> d(20);
    std::merge(&str[0], &str[strlen(str)], &srch[0], &srch[strlen(srch)], &merg[0]);
    std::merge(&str[0], &str[strlen(str)], &srch[0], &srch[strlen(srch)], d.begin());
    cout << merg << endl;
    std::copy(d.begin(), d.end(), ostream_iterator<char>(std::cout, ""));
    cout << endl;
}

I used both deque and char array to store the merged string. But I am getting the output as
Code:
HWelloorld
HWelloorld

But, I expected to get "HelloWorld"

Last edited by royalibrahim; 05-06-2012 at 02:52 AM..
# 2  
Old 05-06-2012
str[0] is the first character in the string, str[strlen(str)] is one beyond the last character of the string, etc, etc. I have no idea how you expected to get HelloWorld.

You should not be declaring arrays with dynamic sizes. Never use things like strlen() in an array size.

STL has absolutely no purpose here since you're using C strings.

Code:
#include <string.h>
#include <stdio.h>

int main(void)
{
    char str[] = "Hello";
    char srch[] = "World";
    char output[512];

    output[0]='\0';
    strcat(output, str);
    strcat(output, srch);
    printf("output is %s\n", output);
}

# 3  
Old 05-07-2012
Thank you Corona688 for the suggestion to use C library, but my intention was to understand the STL merge() and hence I tried it with char array.

Quote:
Originally Posted by Corona688
str[strlen(str)] is one beyond the last character of the string, etc, etc.
Yes, it should be one char beyond the last character of the string, since the C string stores the terminating null '\0' char.
# 4  
Old 05-07-2012
Why would you expect anything but "H" from a string that starts with H, then immediately has a null terminator?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Segmentation fault when I pass a char pointer to a function in C.

I am passing a char* to the function "reverse" and when I execute it with gdb I get: Program received signal SIGSEGV, Segmentation fault. 0x000000000040083b in reverse (s=0x400b2b "hello") at pointersExample.c:72 72 *q = *p; Attached is the source code. I do not understand why... (9 Replies)
Discussion started by: jose_spain
9 Replies

2. Shell Programming and Scripting

Merge two input files and concatenate the rest

Hi Guys, I need a help to creating a bash script to merging two files that containing not in order pattern into single file also within that single file I need to concatenate and manipulate the string from two files. Appreciate for any kind help given. Thanks. Here are the input files:... (3 Replies)
Discussion started by: jabriel
3 Replies

3. Shell Programming and Scripting

Awk, function of underscore char.

Hello Friends, I would appreciate so much if you could explain how the underscores works at the following code? Sorry if it sounds a bit novice question. awk -F',' 'NR==FNR{_=1;next}!_{print}' exclude infile KR, Eagle (6 Replies)
Discussion started by: EAGL€
6 Replies

4. Shell Programming and Scripting

Java - Arrays.binarySearch function equivalent in awk

Hi all Does anyone know Java-Arrays.binarySearch function equivalent in awk I tried like this but it's not correct one,it just returns array index if and only when searched value available in array, for some reason if searched value not found then I want to return upper nearest neighbour index.... (1 Reply)
Discussion started by: Akshay Hegde
1 Replies

5. Programming

Small query regarding function "char * strerror(int errnum)"

As this function returns the address of the string corressponding to the errno value provided to it. Can someone please let me know where, in the memory, it could be (on freeBSD). The MAN page tells under the BUG section that "For unknown error numbers, the strerror() function will return its... (5 Replies)
Discussion started by: Praveen_218
5 Replies

6. Programming

unable to send a char parameter from main to a function

why does this not work? #include <stdio.h> #include <stdlib.h> char getFileMode(char charChanger) { char filetype; /*var to hold the value to be returned*/ filetype = charSetter; /*set filetype to "l" if it is a symlink*/ return filetype; } int main(void){ char... (8 Replies)
Discussion started by: bluetxxth
8 Replies

7. Programming

Char arrays

This is in C++. Is there a way to take characters out of input data? For example, hello 0 1 2 3 4 5 is within my double dimensional array: char arr; How would I output only the characters h,e,l,l,o? (0 Replies)
Discussion started by: puttster
0 Replies

8. Shell Programming and Scripting

Recursive function and arrays

I have the following function in a bash script that fails to return the sorted array. I think the problem lies in the recursion not correctly passing the arrays, but I can't tell what I'm doing wrong. Anyone see the problem? function quicksort () { local array=( `echo "$1"` ) local... (7 Replies)
Discussion started by: tkg
7 Replies

9. Programming

C programming + problem with char arrays

Im trying to write some code atm which gets the complete pathname of a folder and strips off references to the parent folders. The end result should be just the name of the folder. Currently Im able to extract the folder name, however Im getting junk added onto the name as well which is making... (7 Replies)
Discussion started by: JamesGoh
7 Replies

10. Shell Programming and Scripting

shell: creating different arrays based on function argument

hi, I was wondering if there was a good way to create an array within a function, where the name is based on a passed argument? I tried this: _____________________________ func(){ #take in 1st arg as the arrayname arrayName=$1 let i=0 while read line do arrayName=${line} let i+=1... (5 Replies)
Discussion started by: nix21
5 Replies
Login or Register to Ask a Question