9. Strncmp()
int strncmp (const char *str1, const char *str2, size_t n);
Eg:
main()
{
Int l,n=4;
Char str1[20] , str2[20];
Str1[]=“newyear”;
Str2[]=“newyork”;
L=strncmp (str1,str2,n);
Printf(“%d”,l);
}
o/p :
If n=4, l==0
If n>4, and length of str1<str2, l is negative
If n>4, and length of str1<str2, l is positive
If s1<s2 o/p < 0
If s1==s2 o/p == 0
If s1>s2 o/p > 0
Nishma jagadish 9
10. Strchr()
char *strchr (const char *str , int ch);
Eg:
main()
{
Char str[20],*p;
Str[]=“department”;
P=strchr (str1, ‘e’);
Printf(“p=%s”,p);
}
o/p :
P = epartment
If the char is not present it returns null
First occurance(left most)
Nishma jagadish 10
11. Strrchr()
char *strrchr(const char *str , int ch);
Eg:
main()
{
Char str[20],*p;
Str[]=“department”;
P=strrchr (str, ‘e’);
Printf(“p=%s”,p);
}
o/p :
P = ent
If the char is not present it returns null
Last occurance(right most)
Nishma jagadish 11
12. Strstr()
char *strstr (const *s1 , const char *s2);
Eg:
main()
{
Char str1[20], str2[20];
Str1[]=“destination block”;
Str2[]=“nation”;
P=strstr (str1, str2);
Printf(“str1=%s , str2=%s”,str1,str2);
}
o/p :
Str1=nation block
Str2=nation
If str2 is station this string is not
present in str1 hence returns a
null
Used to locate the first
occurrence of a substring
in another string
Nishma jagadish 12