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