262 Programming and Data Structures
/ * pointer cbp, i f given character ie not found strchrO returns n ull. */
clrscrO;
printf ("Enter Text Below
gets(string);
printf ("nCharacter to fin d :");
ch=getcharO;
chpsstrchristring,ch);
if(chp)
printf ("Character %c found in string.",ch);
else
printf ("Character % c not found in string.",ch);
OUTPUT;
Enter Text Below: Hello Beginners.
Character to fin d: r
Character r found in string.
OR
8.24 Write a program to find first occurrence of a given character in a given string. Find the
memoty location where the character occurs. Use strchrO function.
#include <stdio.h>
#include <string.h>
#include <conio.h>
void mainO
{
char linel[30] , line2,*chp;
int i ;
c lrsc rO ;
puts ("Enter Text :");
gets(linel);
puts ("Enter Character to find from the text:");
line2=getche();
for ( i=0,‘i<strlen(linel),i++)
*irintf( n % c %u",linel[i],!t.linel[i]);
chp=strchr(linel, line2);
if(chp)
{
printf ("nAddress o f first %c returned by strchrO is %u ",line2,chp);
Working with Strings & Standard Functions 263
else
printf (" '% c ' String is not present in Given String",line!);
}
QUXTLTT.;
Enter Text : HELLO
Enter Character to find from the text: L
Character Address
H 4032
E 4033
L 4034
L 4035
O 4036
Address of first L returned by strchrO is 4034.
Explanation This function finds the memory location where the first occurrence of a given character
is found in a given string. When it finds the character the program terminates and the strchr ()
function provides the address of that character. The s trchr () function returns the memory address
of the first occurred character i.e. it returns the memory address, which is collected by a pointer
variable.
Note: In place of strchr () one can use strrchrO. The difference between them is that the strchrO
searches occurrence of character from the beginning of the string where as s tr rc h r () searches
occurrence of character from the end (Reverse).
strstrO function
This function finds second string in the first string. It returns the pointer location from where the
second string starts in the first string. In case the first occurrence in the string is not observed, the
function returns a NULL character.
The format of this function is s t r s t r (s t r i n g l , sr in g 2 ).
8.25 Write a program using strstrO function for occurrence of second string in the first string.
#include <stdio.h>
#include <string.h>
#include cconio. h>
void main ()
{
char lin e l [30], line2 [30], *chp;
c lrs c r ( );
puts ("Enter Linel
gets(linel);
puts ("Enter Linel
gets(linel);
264 Programming and Data Structures
chp=strstr(linel,line2);
if(chp)
printf (" '% s ' String is present in Given String/',line2);
else
printf (" '% s ' String is not present in Given String/',line2);
I
OUTPUT:
Enter Linel : IND IA IS MY COUNTRY.
Enter Line2 : IN DIA
'IN D IA ' String is present in Given String.
strc atO function
This function appends the target string to the source string. Concatenation of two strings can be done
using this function. The format of this function is s treat (te xtl / text2).
8.26 Write a program to append second string at the end of first string using strcatO function.
#include <string.h>
#include <stdio.h>
void main ()
{
char te x t l[3 0 ], text2[1 0 ];
puts (''Enter Textl : " ) ;
gets(textl);
puts("Enter Text2 :");
gets (text2);
streat(textl/* " ) ;
streat(textl, text2);
clrscrO ;
prin tf("% sn", textl);
I
OUTPUT:
Enter T extl: I Am
Enter Text2: an Indian
I Am an Indian
Explanation In the above example two strings are entered in character array tex tl [ ] andtext2
[] . The s t r e a t () function concatenates both the strings i.e. second string is appended in the first
string. The p r in t f () function displays the contents of the textl [ ] with concatenation of two strings.
Working with Strings & Standard Functions 265
8.27 Write a program to Concatenate two strings without use of standard function.
# include <stdio.h>
# include <conio.h>
# include <string.h>
void main ()
{
char name [50] , fnamn [15], sname [15], lname [15];
int i, j ,k ;
clrscr ();
printf ("First Name
gets(fname);
printf ("Second Name
gets(sname);
printf ("L a st Name :");
gets(lname);
for (i=0,fname[i]!='0',i++)
name[i]=fname[i];
n a m eliW ';
for (j=0;sname[j]!='0',j++)
nante[i+j+l ]=sname[j];
n a m e [i+ j+ l]= ";
for (k=0;l_na me[k ]/= ' 0 ;fc++>
name[i+j+k+2]=lname[k];
/iamefi+;+fc+2j='0;
printf (" n ri');
printf ("Com plete Name After ConcatenationAn");
printf (" % s n, name);
getcheO;
I
OUTPUT:
First Name : MOHAN
Second Name: KARAM CHAND
Last Name : GANDHI
Complete Name After Concatenation.
MOHAN KARMCHAND GANDHI
Explanation In the above program three strings are entered. The f i r s t for loop copies string
fname [] to name [ ] array using simple assignment. The statement following for loop adds space
after the string. The next two fo r loops follow the same procedure to concatenate arrays sname [ ]
and lname [] in name [] .Thus, we get in name [] concatenation of three strings.
stmcatO function
266 Programming and Data Structures
This function is the same as that of s treat () .The difference between them is that the former does the
concatenation of the strings with another up to the specified length. The format of this function is
stracat (textl, text2,n) where, n is the number of characters to append.
8.28 Write a program to append second string with specified ( n ) number of characters at th*
end of first string using stmcatO function.
#include <string.h>
#include <stdio.h>
void mainO
{
char textl [30], text2 [10] ,n;
puts (*Bnter Textl i* ) j
gets(textl);
puts("Enter Text2:");
gets(text2);
printf ("Enter Number of Characters to A d d :");
getsfn);
strcatftextl," " );
stmcatf textl,text2,n);
clrscrO;
prin tf("% sn", textl);
)
OUTPUT:
Enter Textl : M AY I
Enter Text2: COME IN ?
Enter Number of Characters to A d d : 4
M A Y K O M E
Expla mtion In this program two strings are entered. Number of characters of the second string to
append in the first string is also entered. The stmcat () function uses three arguments viz. Textl [],
text2 [] and 'n ', wh ere' n ' characters of text 2 [] are appended in the te xtl [] string. In this
program two strings "MAY I " , "COME IN" and n=4 are entered. The stmcat () function return "MAY
I COME".
strrevO function
This function simply reverses the given string. The format of this function is strrev(string).
8.29/ 8.30 Write a program to display reverse of the given string.
# inc3 ids <stdio.h>
# inc .ude <string.h>
voia main ()
{
char text [15];
puts ("Enter String “);
gets(text);
puts ("Reverse String") /
puts (strrev(text));
}
..................Content has been hidden....................

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