0

write code for concatenate two strings without space in c language.

p=1st string q=2nd string output:-qp

11th Jan 2023, 12:09 PM
Amit Joe
Amit Joe - avatar
4 Antworten
+ 1
#include<stdio.h> #include<string.h> int main() { char p[20], q[20], result[20]; int i,j; printf("Enter 1st string: "); scanf("%s", p); printf("Enter 2nd string: "); scanf("%s", q); for(i=0; q[i]!='\0'; i++) { result[i]=q[i]; } for(j=0; p[j]!='\0'; j++) { result[i]=p[j]; i++; } result[i]='\0'; printf("Resultant String: %s", result); return 0; }
11th Jan 2023, 9:21 PM
Aditya Dixit
+ 2
Amit Joe oddly, the for loops are limiting the string length as though it were the ASCII value of the 'i'th character in the string. You probably meant something like for(i=0; st[i]!=0; i++)... For easier implementation, look up the string library functions strlen(), strcat(), strncat(). Another option is to use sprintf() for concatenation. The final printing loop could be simply printf("%s", st3); or puts(st3); (be sure that st3 has a terminating null, though)
11th Jan 2023, 6:02 PM
Brian
Brian - avatar
+ 2
You can use malloc and if you need more use realloc it save your data, simple example: https://code.sololearn.com/c2GFZKFY41eL/?ref=app
11th Jan 2023, 9:07 PM
Smith Welder
Smith Welder - avatar
+ 1
#include <stdio.h> int main(void) { char st[1000],st2[1000],st3[1000]; int i,k=1; gets(st); gets(st2); for(i=0;i<st2[i];i++) { if(st2[i]!=' ') { st3[k]=st2[i]; k+=1; } else { k++; continue; } } for(int i=0;i<st[i];i++) { if(st[i]!=' ') { st3[k]=st[i]; k+=1; } else { k++; continue; } } for(int j=0;j<k;j++) { printf("%c",st3[j]); } return 0; } --->Here Is My Code Its works fine bt the problem occurs when string is more than 38 character,
11th Jan 2023, 12:16 PM
Amit Joe
Amit Joe - avatar