writr a c program to find the length of the string using strlen

3
INITIALIZING THE STRING /*String.c string variable*/ #include < stdi.! " ain$% & c!ar nt!'()+ ,rint- $Enter t!e string%+ gets $nt!%+ ,rint- $T!e string entered is 0s1 nt!%+ 2 /*3ritr a c ,rgra t -ind t!e lengt! - t!e string using strlen$% -unctin*/ #include < stdi.! " include < string.! " vid ain$% & c!ar nae'(44+ int lengt!+ ,rint-$Enter t!e string%+ gets$nae%+ lengt!5strlen$nae%+ ,rint-$6nNuber - c!aracters in t!e string is50d1lengt!%+ 2 strcat() function: when you combine two strings, you add the characters of one string to the end of other string. This process is called concatenation . The strcat() function joins 2 strings together. It takes the following form strcat(string1,string2) string1 string2 are character array s. !hen the function strcat is e"ecuted string2 is appended to string1. the string at string2 remains unchanged. Example strcpy(string1,”sri”); strcpy(string2,”Bhagavan”); Printf(“%s”,strcat(string1,string2); #rom the abo$e program segment the $alue of string1 becomes sribhaga$an. The string at str2 remains unchanged as bhagawan.

Upload: attaullahjohar040

Post on 06-Jan-2016

212 views

Category:

Documents


0 download

DESCRIPTION

finding the length of the string

TRANSCRIPT

7/17/2019 Writr a c Program to Find the Length of the String Using Strlen

http://slidepdf.com/reader/full/writr-a-c-program-to-find-the-length-of-the-string-using-strlen 1/3

INITIALIZING THE STRING

/*String.c string variable*/#include < stdi.! "ain$%&c!ar nt!'()+,rint- $Enter t!e string%+gets $nt!%+,rint- $T!e string entered is 0s1 nt!%+2

/*3ritr a c ,rgra t -ind t!e lengt! - t!e string using strlen$% -unctin*/

#include < stdi.! "include < string.! "vid ain$%&c!ar nae'(44+int lengt!+,rint-$Enter t!e string%+gets$nae%+lengt!5strlen$nae%+,rint-$6nNuber - c!aracters in t!e string is50d1lengt!%+2

strcat() function:when you combine two strings, you add the characters of one string to the end of 

other string. This process is called concatenation. The strcat() function joins 2 stringstogether. It takes the following form

strcat(string1,string2)

string1 string2 are character arrays. !hen the function strcat is e"ecuted string2 is

appended to string1. the string at string2 remains unchanged.

Example

strcpy(string1,”sri”);

strcpy(string2,”Bhagavan”);

Printf(“%s”,strcat(string1,string2);

#rom the abo$e program segment the $alue of string1 becomes sribhaga$an. The

string at str2 remains unchanged as bhagawan.

7/17/2019 Writr a c Program to Find the Length of the String Using Strlen

http://slidepdf.com/reader/full/writr-a-c-program-to-find-the-length-of-the-string-using-strlen 2/3

strcmp function:In c you cannot directly compare the $alue of 2 strings in a condition like

if(string1%%string2)&ost libraries howe$er contain the strcmp() function, which returns a 'ero if 2 strings

are eual, or a non 'ero number if the strings are not the same. The synta" of strcmp() is gi$en below

Strcmp(string1,string2) 

*tring1 string2 may be string $ariables or string constants. *tring1, string2 may

be string $ariables or string constants some computers return a negati$e if thestring1 is alphabetically less than the second and a positi$e number if the string is

greater than the second.

Example:

strcmp(“Newyor”,”Newyor”) will return 'ero because 2 strings are eual.

strcmp(“their”,”there”) will return a + which is the numeric difference between

*-II i/ and *-II /r/.strcmp(“!he”, “the”) will return 02 which is the numeric difference between *-II

 T *-II t.

strcmpi() functionThis function is same as strcmp() which compares 2 strings but not case sensiti$e.

Example

strcmpi(“!"#”,”the”); will return 3.

strcpy() function:- does not allow you to assign the characters to a string directly as in the statementname%4obert5

Instead use the strcpy(3 function found in most compilers the synta" of the functionis illustrated below.

strcpy(string1,string2); 

*trcpy function assigns the contents of string2 to string1. string2 may be a character

array $ariable or a string constant.

strcpy(Name,”$oert”);

In the abo$e e"ample 4obert is assigned to the string called name.

strlwr () function:This function con$erts all characters in a string from uppercase to lowercase.

syntax

7/17/2019 Writr a c Program to Find the Length of the String Using Strlen

http://slidepdf.com/reader/full/writr-a-c-program-to-find-the-length-of-the-string-using-strlen 3/3

str&wr(string);

For example:

str&wr(“#'$S*S”) con$erts to 6"forsys.

#or e"ample str+pr(“eforsys”) will con$ert the string to 67#84*9*.

-. #amp&e program to +se string f+nctions.-

/inc&+0e st0ioh 3

/inc&+0e stringh 3

voi0 main()

4

char s15267,s25267,s85267;

int ,&1,&2,&8;

printf(“#nter the strings”);

scanf(“%s%s”,s1,s2);

9strcmp(s1,s2);

if(:96)

4printf(“nStrings are not e<+a&n”);

strcat(s1,s2);

=

e&se

printf(“nStrings are e<+a&”);

strcpy(s8,s1);

&19str&en(s1);

&29str&en(s2);

&89str&en(s8);printf(“ns19%st &ength9%0 charactersn”,s1,&1);

printf(“ns29%st &ength9%0 charactersn”,s2,&2);

printf(“ns89%st &ength9%0 charactersn”,s8,&8);

=