0% found this document useful (0 votes)
49 views

C String Concatenation

The document discusses two C string functions - strcat() and strcmp(). Strcat() concatenates two strings by appending the second string to the first. Strcmp() compares two strings and returns 0 if they are equal. Code examples are provided to demonstrate the usage of each function.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views

C String Concatenation

The document discusses two C string functions - strcat() and strcmp(). Strcat() concatenates two strings by appending the second string to the first. Strcmp() compares two strings and returns 0 if they are equal. Code examples are provided to demonstrate the usage of each function.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

C String Concatenation: strcat()

The strcat(first_string, second_string) function concatenates two strings and result is


returned to first_string.

1. #include<stdio.h>  
2. #include <string.h>    
3. int main(){    
4.   char ch[10]={'h', 'e', 'l', 'l', 'o', '\0'};    
5.    char ch2[10]={'c', '\0'};    
6.    strcat(ch,ch2);    
7.    printf("Value of first string is: %s",ch);    
8.  return 0;    
9. }    

Output:

Value of first string is: helloc

C Compare String: strcmp()


The strcmp(first_string, second_string) function compares two string and returns 0 if
both strings are equal.

Here, we are using gets() function which reads string from the console.

1. #include<stdio.h>  
2. #include <string.h>    
3. int main(){    
4.   char str1[20],str2[20];    
5.   printf("Enter 1st string: ");    
6.   gets(str1);//reads string from console    
7.   printf("Enter 2nd string: ");    
8.   gets(str2);    
9.   if(strcmp(str1,str2)==0)    
10.       printf("Strings are equal");    
11.   else    
12.       printf("Strings are not equal");    
13.  return 0;    
14. }    
Output:

Enter 1st string: hello


Enter 2nd string: hello
Strings are equal

You might also like