0% found this document useful (0 votes)
9 views16 pages

String Handling: By:Dr. P.S.Tanwar

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views16 pages

String Handling: By:Dr. P.S.Tanwar

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

String Handling

By:Dr. P.S.Tanwar
String Handling
String
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]

s1 ‘P’ ‘r’ ‘a’ ‘k’ ‘a’ ‘s’ ‘h’ ‘\0’


O/P 1001
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010

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] … …

s1 ‘P’ ‘r’ ‘a’ ‘k’ ‘a’ ‘s’ ‘h’ ‘\0’


O/P 1001
1001 1002 1003 1004 1005 1006 1007 1008 1010 1011

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]

2.strcpy() ‘P’ ‘r’ ‘a’ ‘k’ ‘a’ ‘s’ ‘h’ ‘\0’

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]

s1 ‘P’ ‘r’ ‘a’ ‘k’ ‘a’ ‘s’ ‘h’ ‘S’ ‘i’


‘\0’ ‘n’ ‘g’ ‘h’ ‘\0’
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
s2 ‘S’ ‘i’ ‘n’ ‘g’ ‘h’ ‘\0’
By:Dr. P.S.Tanwar
String Handling
4. strcmp()
return
type Syntax
int *strcmp(char *str1,char *str2);
Work
This method compares the strings str1 and str2 and return the
difference of first mismatched character.
str1>str2  return +ve
str1<str2  return -ve
str1=str2  return 0

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]

s1 ‘P’ ‘r’ ‘a’ ‘k’ ‘a’ ‘s’ ‘h’ ‘\0’ ‘k’-’m’=-2


[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
s2 ‘P’ ‘r’ ‘a’ ‘m’ ‘o’ ‘d’ ‘\0’
By:Dr. P.S.Tanwar
String Handling
5. strrev()
return
type Syntax
char *strrev(char *str)

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]

5.strrev() ‘P’ ‘r’ ‘a’ ‘k’ ‘a’ ‘s’ ‘h’ ‘\0’

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] … …

5.strrev() ‘P’ ‘r’ ‘a’ ‘k’ ‘a’ ‘s’ ‘h’ ‘\0’

s1 [0] [1] [2] [3] [4] [5] [6] [7] … …


s2 ‘h’ ‘s’ ‘a’ ‘k’ ‘a’ ‘r’ ‘P’ ‘\0’

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]

Palindrome ‘m’ ‘a’ ‘d’ ‘a’ ‘m’ ‘\0’

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

You might also like