String Functions in C

By | February 23, 2011

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.


#include
main()
{

	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

One thought on “String Functions in C

  1. Malcom Madeiros

    I’d have to give the go-ahead with you here. Which is not something I typically do! I love reading a post that will make people think. Also, thanks for allowing me to speak my mind!

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *