252 Programming and Data Structures
{
Int c,d;
static char s t r in g [ 12] ;
in t In;
p rin tf ("Enter a String : " ) ;
g e ts(strin g );
ln=strlen (s tr in g );
clrsc rO ;
printf ("tt Length o f Given String :% d "In );
printf (" n Sequence o f Characters Displayed on Screen”);
printf ( " n
------------
== -= -= ----------------------------------=
printf ( n nn");
for (c=0;c<=ln-2;c++)
1
d=c+l;
printf C/t%.*sn",d,string);
}
fo r (c= ln # >= 0 # ~ )
I
d=c+l;
printf ("t% .*sn ",d,string);
)
i
OUTPUT:
Enter a String: HAPPY
Length of Given String: 5
Sequence of Characters Displayed on Screen
H
HA
HAP
HAPP
HAPPY
HAPPY
HAPP
HAP
HA
H
Explanation In the above program the string is entered and its length is calculated. The first f o r
loop displays the characters from left to right. It adds one character in each successive line. The first
line displays first character, second two characters, and so on.
The second f o r loop displays characters from right to left. It removes the right most character in
each successive line.
Working with Strings & Standard Functions 253
Here, in the printf () the asterisk ( * ) used after decimal plays an important role in displaying the
characters. It requires an integer argument. It displays only the given number of characters from the
string.
strcpyO functions
This function copies the contents of one string to another.
The format of strcpyO is strcpy (s 2 ,s l ).
Where,
si is source string.
s2 is destination string.
si is copied to s2.
In many programs we copy the contents of one string to an other. Given below is an example which is
based on strcpyO function.
8.13 Write a program tp copy contents of one string to another string by using strcpyO.
xnain()
{
char ori [20] , dup [20];
clrscr ();
printf ("Enter Your Name
get$(ori);
strqnf(dup,ori);
printf ("Origional String: %s",ori);
printf ("nDuplicate String: %s",dup);
I
Q U W I L
Enter Your Name: SACHIN
Origional String: SACHIN
Duplicate String: SACHIN
Explanation In the above example we have declared two arrays namely o r i [20] and dup [ 20].
The function strcpy () copies characters of o r i [ ] to dup [ ] .The characters are copied one by
one from the source string ( o r i [20] ) to destination string (dup [20] ) .
Program without strcpy () function
8.14 Write a program to copy contents of one string to another, without strcpyO function.
xnainO
{
char o ri [20] , dup [20] ;
int i ;
254 Programming and Data Structures
clrscr()i
printf ("Enter Your Name
gets(ori);
for 6'=0,*i<20,i++)
dup[ifcori[i];
printf ("Origional String: %s",ori);
printf (" Duplicate String: %s",dup);
J
OUTPUT;
Enter Your Name: SACHIN
Origional String: SACHIN
Duplicate String: SACHIN
Explanation In the above program also we have declared two arrays namely or i [20] & dup[20].
Instead of using strc p y () function, by using f o r loop elements of source array are copied into
destination array one by one.
stmcpyO functions
Stm cpy () function performs the same task as strcpy (). The only difference between them is that the
former function copies specified length of characters from source to destination string. Whereas the
latter function copies the whole source string to destination string. The format of the function is
Stmcpy (d estin atio, source, n)
Where, n is the argument.
A simple example is illustrated below.
8.15 Write a program to copy source string to destination string up to a specified length.
Length is to be entered through the keyboard.
# ±ncluda<8tdio.h>
# Include ccanio. h>
f Include <string.h>
void main ()
{
char s t r l [15], str2 [15];
Int n;
clrsc rO /
printf ("Enter Source String:");
gets(strl);
printf ("Enter Destination String:");
gets(str2);
printf ("Enter Number o f Characters to Replace in Destination String:");
scanf ("% d "^m );
Working with Strings & Standard Functions 255
stmcpy(str2,strl,n);
printf ("Source String -.‘%S ,s trl);
printf ("nDestination String-:%s",str2);
Q M T U X ;
Enter Source String : wonderful
Enter Destination String: beautiful
Enter Number of Characters to Replace in Destination String: 6
Source String : wonderful
Destination String : wonderful
Explanation In the above program two strings are read from the terminal. Number of characters to
replace in the destination string from source string is also entered. After obtaining these three arguments
the stmcpy () function replaces the destination string with number of characters (argument). The
source string characters are "wonderful" and the destination "beauti fu l" before use of stmcpy ().
After execution, the first six characters of the destination string ( "b e a u ti") are replaced with first six
characters of source string ( "wonder"). The output of the program is as shown above.
stricmpO functions
This function compares the two strings. The characters of the strings may be in lower case or upper
case the function doesn't discriminates between them. i.e. This function compares two strings without
case. If the strings are same it returns to zero otherwise non-zero value.
8.16 Write a Program to compare the two strings using stricmpO function. If strings are
identical display "The Two Strings are Identical" otherwise The Strings are Different
Note: stricmpO function compares two strings character by character and returns 0 if the strings
are identical otherwise non zero value. This function doesn't discriminate between small &
capital letters.
#include <stdio.h>
xnainO
{
char s r [10] , tar [10] ;
int d i f f;
c lrs c r ( );
printf ("Enver S trin g(l): " ) ;
gets(sr);
printf ("Enter Strhtg(2): " );
gets(tar);
diff=stricmp(sr,tar);
if(diff==0)
puts("The Two Strings are Identical/');
else
256 Programming and Data Structures
puts("TT>e Two Strings are Different");
getcheO;
I
QLETP.UT ;
Enter String(l): HELLO
Enter String(2): hello
The Two Strings are Identical.
Explanation In the above program two strings are entered. Both the strings are compared using
s tricmp ( ) function. If both the strings are identical it returns 0 otherwise non-zero value. The returned
value of stricmp () function is assigned to variable ' di f f ' . The i f condition checks the value of
variable ' d i f f ' and the respective message is displayed.
8.17 Write a program to perform following.
1. Display the question "What is the Unit of Traffic?"
2. Accept the answer.
3. If the answer is wrong (Other than Earlang) display "Try again!" & continues to answer.
4. Otherwise, if it is correct "Earlang" display the message "Answer is correct".
5. If the user gives correct answer in first two attempts the program will terminate.
6. If the user fails to provide correct answer in three attempts the program itself gives the
answer.
# include <stdio.h>
# include <conio.h>
# include <process.h>
void main ()
{
char ans [8 ];
int i;
c lrs c r();
for (i=l,~i<=3;i++)
I
printf ("nW h a t is the Unit ofTraffic?");
scanf C'%s",ans);
fflush(stdin);
if(stricmp(ans,"Earlang")==0)
t
printf ("nAnsw er is Correct.");
exit(l);
I
else
if(i< 3)
printf (" n Try Again ! n ");
..................Content has been hidden....................

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