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>