C provides many predefined functions which are useful in manipulating strings. There are functions which can compare two strings, joins a string with another or copies a string. All these functions are provided in the header file
1. strlen : returns the length of a string
syntax : int strlen(char s[])
Example – To Determine The Length Of A String
#include
void main()
{
char str[10];
int l;
printf(“\nEnter a string : “);
scanf(“%s”,&str);
l=strlen(str);
printf(“\nLength of the string is %d”,l);
}
Output :
Enter a string : welcome
Length of the string is 7
Description
The strlen() calculates the length of the string and returns the result to the variable “l”. when l is printed the length of the string is determined. The Null character is not counted in the length.
Note
If we enter a string with spaces in it like, “hello there” the length will be printed as 5 not 12. This is because the scanf() can not read a space so when it encounters a space it just stops reading after it. To read any space inside a string replace the scanf() statement with gets(). The statement will now look like this "gets(str);". The specifier “%s” is not used with gets()
2. strcmp() : Compares two string and assertain if they are identical. The two strings must also be in the same case.
Syntax: int strcmp(char str1[],char str2[])
If both the strings are same the function returns 0, if they are different a non-zero value is returned.
Example : Comparing two strings
#include
void main()
{
char pass1[10],pass2[10];
int result;
printf(“\nEnter your login password : “);
scanf(“%s”,&pass1);
printf(‘\nPlease re-enter your password : “);
scanf(“%s”,&pass2);
result=strcmp(pass1,pass2);
if(result==0)
printf(“\nPassword is correct. You can login”);
else
printf(“\nPassword mismatch.”);
}
Enter your login password : hello
Please re-enter your password : hello
Password is correct. You can login
Description
The strcmp() compares two strings and returns 0 if they are exactly the same. If both the passwords are same then strcmp will return the result 0 and a non zero value if they are different. The strcmp() is case sensitive which means that if both the strings are same but their case (capital letter/small letter) is different the function will return a non zero value. If you want a case insensitive comparison use “strcmpi()” instead of “strcmp”. The extra “i” in strcmpi denotes that we want to ignore case while comparing.
3. strcmpi() : Compares two string and assertain if they are identical. Strcmpi ignores case while comparing
Syntax: int strcmpi(char str1[],char str2[])
If both the strings are same the function returns 0, if they are different a non-zero value is returned.
Example : Comparing two strings ignoring their case
void main()
{
char result[25];
printf(“\nWhat is the fullform of CPU ? : ”);
gets(result);// to enable the user to enter spaces.
if(strcmpi(result,”Central Processing Unit”)==0)
printf(“\nYou got it right”);
else
printf(“\nSorry. Ask your teacher for the correct answer”);
}
Output
What is the fullform of CPU ? : Central Processing Unit
You got it right
What is the fullform of CPU ? : CENTRAL PROCESSING UNIT
You got it right
What is the fullform of CPU ? : Central Power Unit
Sorry. Ask your teacher for the correct answer
3. Strcat() – Joins two strings to form a new string.
Syntax : strcat(string1,string2)
the second string will be added after the first string.
void main()
{
char fname[20],lname[10];
printf(“\nWhat’s your first name : “);
scanf(“%s”,&fname);
printf(“\nWhat’s your last name : “);
scanf(“%s”,&lname);
strcat(fname,lname);
printf(“\nHello %s”,fname);
}
Output
What’s your first name : Bill
What’s your last name : Gates
Hello Bill Gates
Description
The strcat() will append the lastname after the first name, therefore when the first name is printed the concatenated value is printed. After concatenation(joining) the first string becomes “Bill Gates” while the second string is still “Gates”.
0 comments:
Post a Comment