String Functions in C
Let’s now briefly discuss about four essential String function used in C.
We are going to discuss about
a) strlen()
b) strcpy()
c) strcat()
d) strcmp()
Codes speak louder than words! Let’s see what these functions do in a simple C Program.
#includemain() { char stringOne[15] = "CoderzHeaven"; char stringTwo[] ="Codes"; char stringThree[15]; int varOne, varTwo; //Counting the string length varOne = strlen(stringOne); printf("n Length of stringOne = %d", varOne); //Copying stringOne to stringThree strcpy(stringThree, stringOne); printf("n After Copying process, stringThree=%s", stringThree); //Comparing two strings varTwo=strcmp(stringThree, stringOne); printf("n On Comparing stringThree & stringOne, varTwo=%d", varTwo); //Comparing two strings varTwo = strcmp (stringOne, stringTwo); printf("n On Comparing stringOne & stringTwo, varTwo=%d", varTwo); //Concatenating two strings strcat(stringOne, stringTwo); printf("n After Concatenation, stringOne = %s", stringOne); }
Output???
Here y
Length of stringOne = 12
After Copying process, stringThree = CoderzHeaven;
On Comparing stringThree & stringOne, varTwo = 0
On Comparing stringOne & stringTwo, varTwo= -1
After Concatenation, stringOne = CoderzHeavenCodes
Link to this post!