C program to String Concatenation - STRCAT

Below is C program to String Concatenation - STRCAT.

Program:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/* STRING CONCATENATION - STRCAT */
 
#include<stdio.h>
 
int main()
{
 char str1[500], str2[100];
 int i, j;
 printf("Please enter first string: ");
 // gets(str);
 // fgets is a better option over gets to read multiword string .
 fgets(str1, 500, stdin);
 // Following can be added for extra precaution for '\n' character
 // if(str1[length(str1)-1] == '\n') str1[strlen(str1)-1]=NULL;
 
 printf("Please enter second string: ");
 // gets(str2);
 fgets(str2, 100, stdin);
 // if(str2[length(str2)-1] == '\n') str2[strlen(str2)-1]=NULL;
  
 for(i=0;str1[i]!=NULL;i++);
 for(j=0;str2[j]!=NULL;j++)
 {
  str1[i++]=str2[j];
 }
 str1[i]=NULL;
  
 printf("Concatenated string is: %s",str1);
 
 return 0;
}

No comments:

Post a Comment