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 Comment

  1. Malcom Madeiros June 21, 2011 at 9:11 pm

    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!

     

Leave a Reply

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

 

 

 

Theme by HermesThemes

Copyright © 2018 CoderzHeaven. All Rights Reserved

Please wait...

Subscribe to our newsletter

Want to be notified when our article is published? Enter your email address and name below to be the first to know.