0

Problem : Build a program that reads two strings from the keyboard and generates a third string which consists of combining two strings in such a way that the characters of the first string are placed in odd positions while the characters of the second string in even positions. The length of the new string will be twice the length of the smaller string. Display all three strings on the screen.

My solution : (I've code something, but it gives me error. Can anyone show me where is the problem?)

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

int main()
{
 char str1[100], str2[100], str3[100];
 int i=0;
 int j; 
 int p;  

 printf("Give string  1: \n");
 gets(str1);
 printf("Give string  2: \n");
 gets(str2);

 if(strlen(str1)<strlen(str2)) 
    p=strlen(str1);
 else
   p=strlen(str2);

   j=0;
 for(i=0; i<p*2; i++){
   if(i%2==0)
       str3[i]=str2[j];
     else{
       str3[i]=str1[j];
       j++; }
}

 printf("\n String 3 is :  %d");
 printf("\n String 2 is :  %d");
 printf("\n String 1 is :  %d");
     return 0;
}
  • 3
    First of all, ***never ever*** use `gets`. It's a [dangerous](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used) function and have even been removed from the C standard. Use e.g. [`fgets`](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used) instead. – Some programmer dude May 06 '20 at 07:47
  • 1
    As for one other problem: What do you think `printf("\n String 3 is : %d");` should print? How can it print a string using the `%d` format? And without telling `printf` about the actual string itself? – Some programmer dude May 06 '20 at 07:48
  • 1
    Your approach is good. Two things: you must _terminate_ `str3` with `'\0'` (null character) and you must lookup the `printf` statement. – Paul Ogilvie May 06 '20 at 07:52
  • @Someprogrammerdude i fixed it. i changed it to %s and now it works. thanks :) –  May 06 '20 at 07:53
  • _"but it gives me error."_ which error? – Jabberwocky May 06 '20 at 08:43
  • @Jabberwocky it doesnt print me the value i was waiting for, i mean, the third string. –  May 06 '20 at 09:13
  • @MagicalVibez next time don't write "it gives me error" but write something like "I expected this output `Foo`, but I got that output `Bar`. – Jabberwocky May 06 '20 at 09:41

2 Answers2

0

You should give some arguments to printf() template:

printf("\n String 3 is :  %s", str3);
printf("\n String 2 is :  %s", str2);
printf("\n String 1 is :  %s", str1);

And %d is for integers, for char* strings you should use %s.

And, of course, before printing str3 do something like str3[2*p] = '\0';

artaxerx
  • 244
  • 1
  • 3
  • 11
0

Starting from the top:

char str1[100], str2[100], str3[100];

How can str3 be equal to str1 and str2 length if str3 is the combination of str1 and str2

 char str1[100], str2[100], str3[200];

Secondly when getting input it is recommended to use fget(),sscanf() or scanf() instead of get()

Thirdly, there is a logical flaw in your program logic here:

for(i=0; i<p*2; i++){
   if(i%2==0)
       str3[i]=str2[j];
     else{
       str3[i]=str1[j];
       j++; }
}

There is no account for uneven number. Try a string with str1 != str2 and you will realize that not all the characters are accounted for. You can fix that by appending the extra characters. Something like this would do.

if(strlen(str1)<strlen(str2)){
    for(int i=strlen(str3); i<strlen(str1)+strlen(str2); i++){
        str3[i]=str2[p++];
    }
}else if(strlen(str2)<strlen(str1)){
    for(int i=strlen(str3); i<strlen(str1)+strlen(str2); i++){
        str3[i]=str1[p++];
    }
}

Lastly,

printf("\n String 3 is :  %d");

A string should be represented with %s. %d are for integers. Also you forgot your value. So change that to:

printf("String 3 is :  %s\n",str3);

In sum, try something like this instead:

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

int main()
{
 char str1[100], str2[100], str3[200];
 int i=0;
 int j=0; 
 int p;  

 printf("Give string  1: ");
 scanf(" %s", str1);
 printf("Give string  2: ");
 scanf(" %s", str2);

 if(strlen(str1)<strlen(str2))
     p=strlen(str1);
 else
   p=strlen(str2);

 for(i=0; i<p*2; i++){
   if(i%2==0)
       str3[i]=str2[j];
     else{
       str3[i]=str1[j];
       j++; 
    }
}

if(strlen(str1)<strlen(str2)){
    for(int i=strlen(str3); i<strlen(str1)+strlen(str2); i++){
        str3[i]=str2[p++];
    }
}else if(strlen(str2)<strlen(str1)){
    for(int i=strlen(str3); i<strlen(str1)+strlen(str2); i++){
        str3[i]=str1[p++];
    }
}

 printf("String 3 is :  %s\n",str3);
 printf("String 2 is :  %s\n",str2);
 printf("String 1 is :  %s\n",str1);
     return 0;
}
Jackson
  • 1,213
  • 1
  • 4
  • 14