Chapter-6
String Functions
|
Strings can be manipulated through following functions
which are defined in the library STRING.H
strcpy() |
used to copy one string to
another |
strcmp() |
used for comparing two strings |
stricmp() |
compares the strings by ignoring
the case. |
strlen() |
returns the length of the
string |
strcat() |
Concatenates the strings
to another string |
strrev() |
Reverses the string and returns
the pointer of that |
program to create two strings
and assign concatenated form to third string.
main()
{
char str1[20]="Syspro",str2[20]=" Internationals";
char str3[40];
printf("\nFirst string contains %s",str1);
printf("\nSecond string contains %s",str2);
strcpy(str3,str1); /* copy string 1 to string 3 */
strcat(str3,str2); /* concatenate string 2 to string 3*/
printf("\n After copy and concatenating string 3 contains %s",str3);
} |
following program calculates
the length of string.
main()
{
char name[20];
int l;
printf("Enter your name :");
scanf("%s",name);
l=strlen(name);
printf("The length of the %s string is %d",name,l);
} |
program to see the entered
string is palindrome or not.
main()
{
char str1[20],str2[20];
printf("Enter the word : ");
scanf("%s",str1);
strcpy(str2,str1);
if( strcmp(str1,str2)==0)
printf("Entered word is palindrome");
else
printf("\nEntered word is not palindrome");
} |
Examples of Palindromes are like MADAM,
MALYALAM, where reversing the string does not changes the meaning of word.
|