Reputation: 446
I am trying to merge two strings of variable length in C. The result should be 1st character from str1 then 1st character from str2 then 2nd character from str1, 2nd character from str2, etc. When it reaches the end of one string it should append the rest of the other string.
For Example:
str1 = "abcdefg";
str2 = "1234";
outputString = "a1b2c3d4efg";
I'm pretty new to C, my first idea was to convert both strings to arrays then try to iterate through the arrays but I thought there might be an easier method. Sample code would be appreciated.
UPDATE: I've tried to implement the answer below. My function looks like the following.
void strMerge(const char *s1, const char *s2, char *output, unsigned int ccDest)
{
printf("string1 is %s\n", s1);
printf("string2 is %s\n", s2);
while (*s1 != '\0' && *s2 != '\0')
{
*output++ = *s1++;
*output++ = *s2++;
}
while (*s1 != '\0')
*output++ = *s1++;
while (*s2 != '\0')
*output++ = *s2++;
*output = '\0';
printf("merged string is %s\n", *output);
}
But I get a warning when compiling:
$ gcc -g -std=c99 strmerge.c -o strmerge
strmerge2.c: In function ‘strMerge’:
strmerge2.c:41:5: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat]
And when I run it it doesnt work:
./strmerge abcdefg 12314135
string1 is abcdefg
string2 is 12314135
merged string is (null)
Why does it think argument 2 is an int and how do I fix it to be a char? If I remove the "*" off output in the printf it doesn't give a compile error but the function still doesn’t work.
Upvotes: 3
Views: 31622
Reputation:
char* getMerged(const char* str1, const char* str2) {
char* str = malloc(strlen(str1)+strlen(str2)+1);
int k=0,i;
for(i=0;str1[i] !='\0' && str2[i] !='\0';i++) {
str[k++] = str1[i];
str[k++] = str2[i];
}
str[k]='\0';
if (str1[i] != '\0') {
strcpy(&str[k], &str1[i]);
} else if (str2[i] != '\0') {
strcpy(&str[k], &str2[i]);
}
return str;
}
Upvotes: 3
Reputation: 1263
Your strMerge prints null because, you print the valueAt(*)output which was assigned null the previous step.
#include<stdio.h>
#include<string.h>
//strMerge merges two string as per user requirement
void strMerge(const char *s1, const char *s2, char *output)
{
printf("string1 is %s\n", s1);
printf("string2 is %s\n", s2);
while (*s1 != '\0' && *s2 != '\0')
{
*output++= *s1++;
*output++ = *s2++;
}
while (*s1 != '\0')
*output++=*s1++;
while (*s2 != '\0')
*output++ = *s2++;
*output='\0';
}
int main()
{
char *str1="abcdefg";
char *str2="1234";
char *output=malloc(strlen(str1)+strlen(str2)+1); //allocate memory 7+4+1 = 12 in this case
strMerge(str1,str2,output);
printf("%s",output);
return 0;
}
OUTPUT:
string1 is abcdefg
string2 is 1234
a1b2c3d4efg
Upvotes: 2
Reputation: 1887
In C, both strings are already arrays that can be accessed by their pointers. You just need to create a new buffer that's large enough, then copy into it.
E.g. something like this:
int str1Length = strlen(str1);
int str2Length = strlen(str2);
char* output = (char*) malloc(str1Length + str2Length + 1);
int j = 0;
for (int i = 0; i < str1Length; i++)
{
output[j++] = str1[i];
if (str2Length < i)
output[j++] = str2[i];
}
if (str2Length > str1Length)
{
for (int i = str2Length - str1Length; i < str2Length; i++)
{
output[j++] = str2[i];
}
}
Upvotes: 1
Reputation: 754470
The code below ensures that the strings can't overflow by making the output string as long as the two input strings, and using fgets()
to ensure that there is no overflow of the input strings. One alternative design would do dynamic memory allocation (malloc()
et al), at the cost of the calling code having to free()
the allocated space. Another design would pass the length of the output buffer to the function so that it could ensure no overflow occurs.
The test program doesn't emit prompts: it would not be hard to add a function to do so.
#include <stdio.h>
#include <string.h>
void interleave_strings(const char *s1, const char *s2, char *output)
{
while (*s1 != '\0' && *s2 != '\0')
{
*output++ = *s1++;
*output++ = *s2++;
}
while (*s1 != '\0')
*output++ = *s1++;
while (*s2 != '\0')
*output++ = *s2++;
*output = '\0';
}
int main(void)
{
char line1[100];
char line2[100];
char output[200];
if (fgets(line1, sizeof(line1), stdin) != 0 &&
fgets(line2, sizeof(line2), stdin) != 0)
{
char *end1 = line1 + strlen(line1) - 1;
char *end2 = line2 + strlen(line2) - 1;
if (*end1 == '\n')
*end1 = '\0';
if (*end2 == '\n')
*end2 = '\0';
interleave_strings(line1, line2, output);
printf("In1: <<%s>>\n", line1);
printf("In2: <<%s>>\n", line2);
printf("Out: <<%s>>\n", output);
}
}
$ ./interleave
abcdefgh
1234
In1: <<abcdefgh>>
In2: <<1234>>
Out: <<a1b2c3d4efgh>>
$
Upvotes: 3