String Handling: By:Dr. P.S.Tanwar
String Handling: By:Dr. P.S.Tanwar
By:Dr. P.S.Tanwar
String Handling
String
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
Prakash
Example
char s1[10]=“Prakash”;
printf(“%s”, s1);
By:Dr. P.S.Tanwar
String Handling
String
[0] [1] [2] [3] [4] [5] [6] [7] … …
Prakash
Example
char *s1=“Prakash”;
printf(“%s”, s1);
By:Dr. P.S.Tanwar
String Handling
1. strlen()
return
type Syntax
int strlen(char *str)
Work
Return the length of String
By:Dr. P.S.Tanwar
String Handling
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
1.strlen()
s1 ‘P’ ‘r’ ‘a’ ‘k’ ‘a’ ‘s’ ‘h’ ‘\0’
1001 1002 1003 1004 1005 1006 1007 1008 1010 1011
1001
O/P Example
Length=7
char s1[10]=“Prakash”;
printf(“Length=%d”, strlen(s1));
By:Dr. P.S.Tanwar
String Handling
2. strcpy()
return
type Syntax
char *strcpy(char *dest,char *source)
Work
This method copy the source string
to the destination string and return
a pointer to the destination string
By:Dr. P.S.Tanwar
String Handling
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
s1 [0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
s2 ‘P’ ‘r’ ‘a’ ‘k’ ‘a’ ‘s’ ‘h’ ‘\0’
O/P Example
s1=Prakash char s1[10]=“Prakash”;
s2=Prakash
char s2[10];
strcpy(s2,s1);
printf(“\ns1=%s\ns2=%s”,s1,s2);
By:Dr. P.S.Tanwar
String Handling
3. strcat()
return
type Syntax
char *strcat(char *str1,char *str2);
Work
This method concatenates the
string str1 and str2 and return the
concatenated string
By:Dr. P.S.Tanwar
String Handling
3.strcat()
O/P Example
s1=PrakashSingh char s1[16]=“Prakash”;
s2=Singh char s2[10]=“Singh”;
strcat(s1,s2);
printf(“\ns1=%s\ns2=%s”,s1,s2);
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14] [15]
By:Dr. P.S.Tanwar
String Handling
4.strcmp()
O/P Example
-2 char s1[10]=“Prakash”;
char s2[10]=“Pramod”;
printf(“\n%d”,strcmp(s1,s2));
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
Work
This method returns the pointer to
the reverse of the given string
By:Dr. P.S.Tanwar
String Handling
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
s1 [0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
s2 ‘h’ ‘s’ ‘a’ ‘k’ ‘a’ ‘r’ ‘P’ ‘\0’
O/P Example
s1=Prakash char s1[10]=“Prakash”;
s2=hsakarP
char s2[10];
s2=strrev(s1);
printf(“\ns1=%s\ns2=%s”,s1,s2);
By:Dr. P.S.Tanwar
String Handling
[0] [1] [2] [3] [4] [5] [6] [7] … …
O/P Example
s1=Prakash char *s1=“Prakash”;
s2=hsakarP
char *s2;
s2=strrev(s1);
printf(“\ns1=%s\ns2=%s”,s1,s2);
By:Dr. P.S.Tanwar
String Handling
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
s1 [0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
s2 ‘m’ ‘a’ ‘d’ ‘a’ ‘m’ ‘\0’
char s1[10]=“madam”;
O/P char s2[10];
madam is Palindrome
s2=strrev(s1);
if(strcmp(s1,s2)==0)
printf(“%s is palindrome”,s1);
else
printf(“%s is not palindrome”,s1);
By:Dr. P.S.Tanwar
Thank You
By:Dr. P.S.Tanwar