Working with Strings & Standard Functions 257
I
clrscrQ;
printf (" N u n it o f Traffic is Earlang.");
}
OUTPUT:
What is the Unit of Traffic ? Earlan
Try Again!
What is the Unit of Traffic ? Earlam
Try Again!
What is the Unif of Traffic ? Earlang
Answer is Correct
Explanation In the above program s tricmp () function is used for comparing character array ans [
] & "Earlang". If function returns 0 'zero' the message displayed will be "Answer i s Correc t . " In
case the answer is wrong the message displayed will be "Try aga in! Three attempts are provided
using fo r loop for answering the question. The f f lush () function is used for clearing the buffer.
strcmp () functions
St rcmp () One can also use s trcmp () function instead of s tricmp () . The only difference between
them is the former function discriminates between small & capital letters where as the latter doesn't.
Theoutputof the above program after strcmp () in place of s tricmp () will be as follows.
Enter String (1) : HELLO
Enter String (2) : h ello
The Two Strings are Different
The above function compares two strings for finding whether they are same or different. Characters of
these strings are compared one by one. In case of a mismatch the function returns to non-zero value
otherwise zero i.e. when the two strings are same strcmp () returns the value zero. If they are different
it returns the numeric difference between the ASCII values of non-matching characters.
stmcmpO functions
Comparison of two strings can be made up to certain specified length. The function used for this is
stmcntp (). This function is same as strcmp () but it compares the character of the string to a specified
length. The format of this function is as follows,
stmcmp (source, target, argument);
where, argument is no. of characters up to which the comparison is to be made.
8.18 Write a program to compare two strings up to specified length.
#include <Stdio.h>
#include <ccnio.h>
#include <string.h>
258 Programming and Data Structures
medn()
{
char sr[1 0], tar [10];
int n,di£f;
clrscrO ;
printf ("Enter S trin g(l): " );
gets(sr);
printf ("Enter String(2): " );
gets(tar);
printf ("nEnter Length up to which comparison is to be made ");
scanf ("%d",&cn);
diff=stmcmp(sr,tar,n);
if(diff==0)
printf ("The Two Strings are Identical up to %d characters.",n);
else
putsC'The Two Strings are EHfferent");
getcheO;
}
Q M m n i
Enter String(l): HELLO
Enter String(2): HE M AN
Enter Length up to which comparison is to be made: 2
The Two Strings are Identical up to 2 characters.
Explanation One can also use stmicmp ( ) function instead ofstmcmp ( ) . The only difference between
them is that the former function discriminates between small & capital letters where as latter doesn't.
The output of the above program after s tmicmp () in place ofstmcmp () will be as follows.
Enter Strin g (1) : HELLO
Enter String (2) : he man
The two strings are identical up to 2 characters.
Similarly, a program without using strcmp () can be developed which is as given below.
8.19 Write a program to enter the two strings and compare them without using any standard
function. Determine whether the strings are identical or not. Also display the number
of position where the characters are different.
#include <stdio.h>
#include <string.h>
#include <conio.h>
main()
{
static char sr [10] , tar [10];
int diff=0 ,i;
clrscr();
printf ("Enter S trin g (l): " );
Working with Strings & Standard Functions 259
gets(sr);
printf ( “Enter String(2): " );
gets(tar);
for (i=0,H<10,i++)
1
if(sr[i)==ta r[i])
continue;
else
{
printf ("% c % c n ",st[i], tar[i]);
diff++;
}
if(strlen(sr)~strlen(tar) && diff==0)
puts("nThe Two Strings are Identical");
else
printf("nThe Two Strings are Different at %dplaces.",diff);
getcheO;
}
OUTPUT ;
Enter String(l): BEST LUCK
Enter String(2): GOOD LUCK
GB
OE
OS
D T
The Two Strings are Different at 4 places.
Explanation In the above program two strings up to 10 characters can be entered. The i f condition
within the for loop checks the corresponding characters of both the strings. If they are identical loops
are continued. Otherwise counter variable ' di f f ' is increased and different characters of two strings
are displayed. The last i f condition checks the variable ' di f f ' and displays the respective messages.
strlwrO function
This function can be used to convert any string to a lower case. When you are passing any upper case
string to this function it converts into lower case. The standard format of strlw r ( ) is as follows
strlvnr (string).
8.20 Write a program to convert upper case string to lower case using strlwrO.
# include <string.h>
# include <stdio.h>
# include <conio.h>
void main()
260 Programming and Data Structures
{
char upper [15];
clrscr ();
printf ("tiEtiter a string in Upper case
gets(upper);
printf ("A fter strhini): % s", strlwr(upper));
I
Qim v j :
Enter a string in Upper case: ABCDEFG
After strlwrO : abcdefg
Explanation In this program string is entered in capital letters. The string is passed to the function
s t r lw r () . This function converts the string to a lower case.
stru prO function
This function i&the same as s t r lw r () but the difference is that s tru p r () converts Lower case strings
to Upper case. The format of this function is s t r u p r ( s t r i n g ).
8.21 Write a program to convert Lower case string to Upper case using struprO.
# include <string.h>
# include <stdio.h>
# include <conio.h>
void mainO
{
char upper [15] ;
clrscr ();
printf ("nEnter a string in Lower Case
gets(upper);
printf ("After struprO: %s", strupr(upper));
I
OUTPUT:
Enter a string in Lower Case: abcdefg
After struprO : ABCDEFG
Explanation In this program string is entered in the lower case. The string is passed to the function
s trupr ( ) . This function converts the string to upper case.
StrdupO Function
This function is used for duplicating a given string at the allocated memory which is pointed by a
pointer variable. The format of this function is text2=s trdup ( t e x t l ) .
Where, te x t l is a s trin g & text2 is a pointer.
Working with Strings & Standard Functions 261
8.22 Write a Program to enter the string and get it's duplicate. Use strdupO function.
# include <stdio.h>
# include <conio.h>
# include <string.h>
void mainQ
{
char textl [20] ,*text2;
/* pointer *text2 is in itializ e d to the address of tex tl through strdupO function.
V
c lrs c r();
printf ("Enter Text:");
gets(textl);
text2=strdup( textl);
printf ("O riginal String = %snDuplicate String = % s ", textl, textl);
I
OUTPUT:
Enter Text: Today is a Good Day.
Original String = Today is a Good Day.
Duplicate String = Today is a Good Day.
Explanation In the above program character array te xtl [ ] and pointer variable * text2 are declared.
String is entered in character array t e x t l [ ]. The s trdup () function copies contents of te xt [ ]
array to pointer variable * text. The p r in t f () function displays the contents of both the variables
which are same.
strchrO function
This function returns the pointer to a position in the first occurrence of the character in the given
string. The format of this function is chp=strchr (strin g, c h );
Where, st r in g is character array, ch is character variable & chp is a pointer which collects address
returned by s trchr () function. The format of this function is s trchr (s tr in g , character).
8.23 Write a program to find first occurrence of a given character in a given string. Use
strchrO function.
#include <stdio.h>
#include <string.h>
# include <canio.h>
void main ()
{
char strin g [30] ,ch,*chp;
/* returns a pointer to the f i r s t occurrence of tha given character in */
..................Content has been hidden....................

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