We have two strings and we want to concatenate them into one. Word2 at the end of Word1.
This is what function concat does in the following code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | #include <stdio.h> #include <string.h> #define SIZE 80 void concat(char *word1,char *word2){ if(strlen(word1)+strlen(word2)>SIZE-1) return; while(*word1++!='\0'); word1--; while(*word2!='\0') *word1++=*word2++; *word1='\0'; } int main(){ char name1[SIZE],name2[SIZE]; printf("Enter string1: "); scanf("%s",name1); printf("Enter string2: "); scanf("%s",name2); concat(name1,name2); printf("The new string is: %s\n",name1); return 0; } |
