Strings
String library functions
Nishma jagadish 1
String library functions
Strlen()
Strcmp()
Strcpy()
Strcat()
Strncpy()
Strncat()
Strncmp()
Strchr()
Strrchr()
Strstr()
Nishma jagadish 2
Strlen()
Size_t strlen (char const *string);
Eg:
main()
{
Int l;
Char str[20];
Str[]=“nishma”;
L=strlen (str);
Printf(“length = %d”,l);
}
o/p : length=6
Nishma jagadish 3
Strcmp()
int strcmp (const char *str1, const char *str2);
Eg:
main()
{
Int l;
Char str1[20] , str2[20];
Str1[]=“new”;
Str2[]=“new”;
L=strcmp (str1,str2);
Printf(“%d”,l);
}
o/p : 0
If s1<s2 o/p < 0
If s1==s2 o/p == 0
If s1>s2 o/p > 0
Nishma jagadish 4
Strcpy()
Char *strcpy (char *str1 , const char str2);
Eg:
main()
{
Char str1[20], str2[20];
Str1[]=“newyear”;
Str2[]=“York”;
strcpy (str1, str2);
Printf(“str1=%s, str2=%s”,str1,str2);
}
o/p :
Str1=“York”
Str2=“York”
Source
Dest
strcpy (str1, str2);
strcpy (str1, “York”);
strcpy (“new”, str2);
strcpy (“new”, “York”);
Nishma jagadish 5
Strcat()
char *strcat (char *str1, const char *str2);
Eg:
main()
{
Char str1[20] , str2[20];
Str1[]=“new”;
Str2[]=“york”;
strcat (str1,str2);
Printf(“str1=%s, str2=%s”,str1,str2);
}
o/p :
Str1=“newyork”
Str2=“York”
Nishma jagadish 6
Strncpy()
Char *strncpy (char *str1 , const char str2 , size_t n);
Eg:
main()
{
Int n=6;
Char str1[20], str2[20];
Str1[]=“department”;
Str2[]=household”;
strncpy (str1, str2,n);
Printf(“str1=%s, str2=%s”,str1,str2);
}
o/p :
Str1=“househment”
Str2=“household”
Source
Dest
strncpy (str1, str2,n);
strncpy (str1, “York”,n);
strncpy (“new”, str2,n);
strncpy (“new”, “York”,n);
Nishma jagadish 7
Strncat()
char *strncat (char *str1, const char *str2, size_t n);
Eg:
main()
{
Int n =6;
Char str1[20] , str2[20];
Str1[]=“department”;
Str2[]=“household”;
strncat (str1,str2,n);
Printf(“str1=%s, str2=%s”,str1,str2);
}
o/p :
Str1=“departmenthouseh”
Str2=“household”
Nishma jagadish 8
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
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
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
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
Thank you
Nishma jagadish 13

Strings library functions

  • 1.
  • 2.
  • 3.
    Strlen() Size_t strlen (charconst *string); Eg: main() { Int l; Char str[20]; Str[]=“nishma”; L=strlen (str); Printf(“length = %d”,l); } o/p : length=6 Nishma jagadish 3
  • 4.
    Strcmp() int strcmp (constchar *str1, const char *str2); Eg: main() { Int l; Char str1[20] , str2[20]; Str1[]=“new”; Str2[]=“new”; L=strcmp (str1,str2); Printf(“%d”,l); } o/p : 0 If s1<s2 o/p < 0 If s1==s2 o/p == 0 If s1>s2 o/p > 0 Nishma jagadish 4
  • 5.
    Strcpy() Char *strcpy (char*str1 , const char str2); Eg: main() { Char str1[20], str2[20]; Str1[]=“newyear”; Str2[]=“York”; strcpy (str1, str2); Printf(“str1=%s, str2=%s”,str1,str2); } o/p : Str1=“York” Str2=“York” Source Dest strcpy (str1, str2); strcpy (str1, “York”); strcpy (“new”, str2); strcpy (“new”, “York”); Nishma jagadish 5
  • 6.
    Strcat() char *strcat (char*str1, const char *str2); Eg: main() { Char str1[20] , str2[20]; Str1[]=“new”; Str2[]=“york”; strcat (str1,str2); Printf(“str1=%s, str2=%s”,str1,str2); } o/p : Str1=“newyork” Str2=“York” Nishma jagadish 6
  • 7.
    Strncpy() Char *strncpy (char*str1 , const char str2 , size_t n); Eg: main() { Int n=6; Char str1[20], str2[20]; Str1[]=“department”; Str2[]=household”; strncpy (str1, str2,n); Printf(“str1=%s, str2=%s”,str1,str2); } o/p : Str1=“househment” Str2=“household” Source Dest strncpy (str1, str2,n); strncpy (str1, “York”,n); strncpy (“new”, str2,n); strncpy (“new”, “York”,n); Nishma jagadish 7
  • 8.
    Strncat() char *strncat (char*str1, const char *str2, size_t n); Eg: main() { Int n =6; Char str1[20] , str2[20]; Str1[]=“department”; Str2[]=“household”; strncat (str1,str2,n); Printf(“str1=%s, str2=%s”,str1,str2); } o/p : Str1=“departmenthouseh” Str2=“household” Nishma jagadish 8
  • 9.
    Strncmp() int strncmp (constchar *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 (constchar *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
  • 13.