270 Programming and Data Structures
char stra[10] ,stzb[10] ;
int length;
clrscrO;
printf ("First String:");
gets ( stra);
printf ("Second String:");
gets(strb);
length-strspn(strafstrb);
printf ("A fter % d Characters there is no m atchn", length);
i
QUTPUT;
First String : GOOD MORNING
Second String: GOOD BYE
After 5 Characters there is no match.
Explanation In this program two strings are entered. Both the strings are passed to the function
s trspn (). The function searches the second string in the first string. It searches from the first character
of the string. If there is a match from the beginning of the string the function returns the number of
characters that are same.
This function returns 0 when the second string mismatches with the first from the beginning. For
example, assume the first string is "BOMBAY" and second "TROMBAY". On application of this function in
the above case the function returns 0 and message displayed will be "A fte r 0 Characters there is
no match."
strpbrkO function
This functions searches the first occurrence of the character in a given string and then it displays the
stringstarting from that character. This function returns the pointer position to the first occurrence of
the character text2 [2] string in the string t e x t l [20]. The format of this function is strpb rk
(tex tl, text2).
8.34 Write a program to print given string from first occurrence of given character.
# include <string.h>
# include <stdio.h>
# include <conio.h>
void main ()
{
char *ptr;
char textl [20] , text2 [2] ;
clrscrO ;
printf ("Enter String :");
gets(textl);
printf ("Enter Character:");
gets(text2);
ptr=strpbrk (textl,textl);
puts(" String front given Character");
printf (ptr);
I