Following are the functions defined in the file string.h
char *strcat(char *dest, char *src)
Appends the string pointed to, by src to the end of the string pointed to by dest.
char *strncat(char *dest, char *src, int n)
Appends the string pointed to, by src to the end of the string pointed to, by dest up to n characters long.
char *strchr(char *str, char c)
Searches for the first occurrence of the character c in the string pointed to, by the argument str.
int strcmp(char *str1, char *str2)
Compares the string pointed to, by str1 to the string pointed to by str2.
int strncmp(char *str1, char *str2, int n)
Compares at most the first n bytes of str1 and str2.
char *strlwr(char *str)
Converts a string pointed to by str to lowercase.
char *strcpy(char *dest, char *src)
Copies the string pointed to, by src to dest.
char *strncpy(char *dest, char *src, int n)
Copies up to n characters from the string pointed to, by src to dest.
int strlen(char *str)
Computes the length of the string str up to but not including the terminating null character.
char *strrchr(char *str, int c)
Searches for the last occurrence of the character c in the string pointed to by the argument str.
char *strstr(char *str1, char *str2)
Finds the first occurrence of the entire string str2 which appears in the string str1.
char *strupr(char *str)
Converts a string pointed to by str to uppercase.
char *strrev(char *str)
Reverses a string pointed to by str and returns reversed string.
int strcmpi(char *str1, char *str2)
Compares the string pointed to, by str1 to the string pointed to by str2 by ignoring case.
char * strset(char *str, char ch)
Sets all characters in pointed to by str to the value of ch.
char * strnset(char *str, char ch, int n)
Sets the first n characters in pointed to by str to the value of ch.
char * strtok(char *str1, char *str2)
searches str1 for tokens that are separated by delimiters specified in str2. Returns the pointers to the first character of first token in str1.
Program using string standard library functions
#include<stdio.h>
#include<string.h>
main()
{
char s1[20],s2[20];
int choice;
do
{
printf(โ\n1: LENGTH OF STRINGโ);
printf(โ\n2: COPY STRINGโ);
printf(โ\n3: CONVERT TO UPPERCASEโ);
printf(โ\n4: STRING CONCATENATIONโ);
printf(โ\n5: STRING COMPAREโ);
printf(โ\n6: EXITโ);
printf(โ\n enter your choiceโ);
scanf(โ%dโ,&choice);
switch(choice)
{
case 1:
printf(โ\n enter the string:โ);
gets(s1);
printf(โ\n The length is %dโ,strlen(s1));
break;
case 2:
printfโ(โ\n Enter the stringโ);
gets(s1);
printf(โ\n The copied string is %sโ, strcpy(s2,s1));
break;
case 3:
printf(โ\n Enter the stringโ);
gets(s1);
printf(โ\n The uppercase string is %sโ,strupr(s1));
break;
case 4:
printf(โ\n Enter the first string:โ);
gets(s1);
printf(โ\n Enter the second string:โ);
gets(s2);
strcat(s1,s2);
printf(โ\n The concatenated string is %sโ,s1);
break;
case 5:
printf(โ\n Enter the first string:โ);
gets(s1);printf(โ\n Enter the second string:โ);
gets(s2);
if (strcmp(s1,s2)==0)
printf(โ\n string are equalโ);
else
if(strcmp(s1,s2)>0)
printf(โs1 is greater than s2โ);
else
printf(โs2 is greater than s1โ);
}
}while(choice!=6);
}
Comments
Post a Comment
Please give us feedback through comments