Pointers 309
Explanation In the above program two strings are entered in the character array s t r 1 [ ] and s t r 2
[]. Their base addresses are assigned to pointer ' * a ' and '*b'. In thewhile loop two pointers are
compared using s tricmp () function. If the characters of two strings are the same the counter variable
% 1' is increased and the characters are printed. Otherwise the counter % c ' is increased. T is job is
done in the i f statement. The character pointers 'a ' and 'b ' are increased throughout thewhj leloop
to obtain the successive characters from both the strings. The last i f statement displays the r essages
giving strings that are different or identical depending on the value o fx c '.
9.33 Write a program to enter three characters, using pointers. Use the memcmp () function
for comparing the three characters. In case the entered characters are same display the
message "the characters are same" otherwise indicate their appearance before or after
one another or display the status of characters in alphabetic order. (The memcmp 0
function compares a specified number of characters from two buffers.)
#include <atdio.h>
#include <conio.h>
#include <string.h>
#include <process.h>
void main ()
{
char x,y,z,*xp,*yp,*zp;
int statnO;
clrscrO ;
printf ("Enter Three Characters");
scanf ( “%c %c y,&cz);
xp~hx,yp =&y, zp =&z;
stat= memcmp(yp, xp, strlen(yp));
if(*xp==*yp)
I
printf ("n lst and 2nd Character are sante.
");
goto next;
i
if (stat > 0 )
printf("2nd Character appears after the 1st Character in Alphabetic.n");
else
printf("2nd Character appears before the first Character in Alphabetic n");
next:
stat - memcmp(yp,zp, strlen(yp));
if(*yp ~ *zp )
{
printf (" t2nd and 3rd Character are same/');
exitil);
I
if (stat >0 )
printf("2nd Cnaracter appears after the 3rd Character in Alphabetic. n");
else
printf("2nd Character appears before 3rd Character in Alphabetic.n");
I
OUTPUT:
Enter Three Character C C A
1st and 2nd Character are same.
2nd Character appears after the 3rd Character in Alphabetic.