Working with Strings & Standard Functions 267
q it t p .ut :
Enter String
ABCDEFGHI
Reverse String
IHGFEDCBA
# include <stdio.h>
# include <string.h>
# include <conio.h>
void main ()
{
char text [15];
in t i=0;
c lrs c r();
printf ("Enter String
gets (text);
while (tex t[i]!=' 0 ')
{
printf (" n %c is stored at location % u",text[i],$ztext[i]);
i++;
I
strrev(text);
printf ("nReverse S t r i n g ");
pritt tf (" % s ", text);
i=0;
while (text[i]!='0')
I
printf (" n %c is stored at location % u ", text[i], & textli J);
i++;
I
OUTPUT:
Enter String ABC
A is stored at location 4054
B is stored at location 4055
C is stored at location 4056
Reverse S trin g C B A
C is stored at location 4054
B is stored at location 4055
A is stored at location 4056
268 Programming and Data Structures
Explanation In the above programs string is entered and passed it to the s t rr e v () function. On
execution of the function the given string appears in the reverse order. The s t r r e v () function
physically changes the sequence of characters in the reverse order. The output of the second program
shows the sequence of characters and their locations.
strsetO function
This function replaces every character of a string with the symbol given by the programmer, i.e. the
elements of the strings are replaced with the arguments given by the programmer. The format of the
function is strset (string, symbol).
8.31 Write a program to replace (set) given string with given symbol. Use strsetO function.
#include <stdio.h>
#include <string.h>
#include <canio.h>
void main ()
{
char string [15] ;
char symbol;
c lrs c r ( );
puts("Enter String:");
gets(string);
puts(" Enter Symbol for Replacement:");
scanf ("%c",&csymbol);
printf ("Before strsetO: %sn", string);
strset(stringf symbol);
printf("After strsetO: % sn", string);
I
OUTPUT:
Enter String: LEARN C
Enter Symbol for Replacement: Y
Before strsetO : LEARN C
After strset (): YYYYYYY
Explanation The s t r s e t ( ) function requires two arguments. First one is the string and another
character by which the string is to be replaced. Both these arguments are to be entered when the
progra m is executed .Thestrset ( ) function replaces every character of thefirst string with the
given character/ symbol i.e. every character of the string is replaced by the entered character.
stm setO function
This function is the same as that of s t r se t ( ) . Here the specified length is provided. The format of
tills function is strnset (strin g , symbol #n) where, n is the number of characters to be replaced.
Working with Strings & Standard Functions 269
8.32 Write a program to replace (set) given string with given symbol for given number of
arguments. Use stmsetO function.
#include <stdio.h>
#include <string.h>
#include <conio.h>
void main ()
{
char s t rin g [15] ; char symbol;
in t n;
c l r s c r () ;
putsC'Enter String
gets(string);
putsC'Enter Symbol for Replacement:");
scanf ("%c",&Lsymbol);
puts ("H o zv many String Character to be replaced." );
scanf ("%d",&cti);
printf("Before strsetO: % sn ”, string);
stmset(string, symbol,n);
printfi"After strsetO :% s n ", string);
I
QLZmET-:
Enter String: ABCDEFGHIJ
Enter Symbol for Replacement: +
How many String Characters to be replaced. 4
Before strsetO: ABCDEFGHIJ
After strsetO: ++++EFGHIJ
Explanation This program is same as that of the previous one. The only difference is that instead of
replacing all characters of the string only specified number of characters are to be replaced. Here, the
number entered is 4. Hence, only the first four characters are replaced by the given symbol. The
replacing process starts from the first character of the string.
strspnO function
This function returns the position of the string from where the source array does not matche with the
target one. The format of this function is s trspn (s t r i n g l , s trin g 2 ).
8.33 Write a program to enter two strings. Indicate after what character the lengths of the
two strings have no match.
# include <stdio.h>
# include <stringh>
# include <conio.h>
void main ()
{
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
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.200.3