Concatanated String Problems

Problem: Take three different string as input from the user(“We”, “ Love” and “ Bangladesh”). Now concate these three string and print it (“We Love Bangladesh”). [There is a leading space in “ Love” and “ Bangladesh”]

Code:

#include<stdio.h>
int main()
{
    char str1[20],str2[20],str3[20],deststr[60];
    int i,j,k;
    gets(str1);
    gets(str2);
    gets(str3);

    for(i=0;(deststr[i]=str1[i])!='\0';i++);

    for(j=0;(deststr[i]=str2[j])!='\0';i++,j++);

    for(k=0;(deststr[i]=str3[k])!='\0';i++,k++);
    printf("%s",deststr);

}

Another approach:

#include<stdio.h>
int main()
{
    char str1[3]="We";
    char str2[6]=" Love";
    char str3[12]=" Bangladesh";
    char str4[35];

    int length,i,j=0;

    for(i=0;i<strlen(str1);i++){
        str4[j++]=str1[i];
    }

    for(i=0;i<strlen(str2);i++){
        str4[j++]=str2[i];
    }

    for(i=0;i<strlen(str3);i++){
        str4[j++]=str3[i];
    }

    str4[j]='\0';
    printf("%s\n",str4);

   return 0;
}

 

It would be a great help, if you support by sharing :)
Author: zakilive

Leave a Reply

Your email address will not be published.