306 Programming and Data Structures
OUTPUT:
llll
2 2 2 2
3 3 3 3
Explanation In the above example, addresses of one-array elements are assigned to another array.
The second array assigns addresses to the third one and so on. In the successive printf () statements
increases by one for getting higher level value.
9.9 POINTERS AND STRINGS
9.29 Write a program to read string from keyboard and display it using character pointer.
#Include <stdio.h>
#Include <conio.h>
nainO
{
char name [15] ,*ch;
printf ("Enter Your Name
getsiname);
ch=mame;
J* store base address of string name */
while (*ch !-'0 ')
I
printf C % c *ch);
ch++;
I
OUTPUT:
Enter Your Name: KUMAR
KUMAR
Explanation Here, the address of 0 th element is assigned to the character pointer ' ch'. In other
words base address of string is assigned to ' ch' . The pointer ' * ch' points to the value stored at that
memory location and it is printed through p rin tf () statement. After every increase in 'c h ' the
pointer goes to the next character of the string. When it encounters NULL character the w hile loop
terminates the program.
9.30 Write a program to find length of a given string including and excluding spaces using
pointers.
# include <stdio.h>
# include <ccnio.h>
void main ()
{
char str [20] ,*s?
int p»0,q*0;
Pointers 307
d ta c x O ;
printf ("Enter String
gets(str);
s=str;
while (*s!='0r)
I
printf ("% c "* s );
p++;
S++/
if(*s==32) /* ASCII equivalnet o f " (space)is 32*t
q++!
printf ("nLength of Siring including spaces: %d",p);
printf ("nhength of String excluding spaces : %d",p-q);
I
OUTPUT:
Enter String: POINTERS ARE EASY
POINTERS ARE EASY
Length of String including spaces: 17
Length of String excluding spaces: 15
Explanation The above program is the same as the previous one. Here, the counter variables ' p '
and ' q' are increased to count number of characters and spaces found in the string. The ASCII value
of space is 3 2. Thus, at the end of the program both the variables are printed.
931 Write a program to interchange elements of character array using pointer.
# include <stdio.h>
# include <conio.h>
void main ()
{
char *xxames [] *{
"kapil*,
"maaoj",
"amit",
"amol",
"pavan",
"mahesh"
} ;
char *tmp;
clrscrO ;
printf ("Original: %s %s",names[3],names[4]);
tmp=names[3];
namesl3]=names[4];
names[4]=tmp;
printf ("nNew : %s %s",names[3],names[4]);
}
OUTPUT:
Original: amol pavan
New : pavan amol
308 Programming and Data Structures
Explanation In the above program character array *names [ ] is declared and initialized. Another
character pointer *tmp is declared. The destination name that is to be replaced is assigned to the
variable ' *tm p '. The destination name is replaced with the source name and the source name is
replaced with * trap variable. Thus, using simple assignment statements two names are interchanged.
932 Write a program to read two strings through the keyboard. Compare these two strings
Character by character. Display the similar characters found in both the strings and
count the number of dissimilar characters.
# include <stdio.h>
# include <canio.h>
# include <string.h>
void main ()
{
char strl[20] ,str2[20] ,l,*a ,* b ;
Int cxO;
clrscr ();
printf ( ti Enter First String :");
gets(strl);
printf ("n Enter Second String
gets(strl);
a=strl;
b=str2;
printf ( Similar Characters Found in Both String.");
while (*a!='0')
I
if(stricmp(*a,*b)=±0)
I
printf ("nt%c %c”,*a,*b);
/++;
I
else
C + + ;
a++;
b++;
I
if (c==0)
printf ("n The String are Identical.’);
else
printf ("nThe Strings are different at %d places",c);
printf ("n The String Characters are similar at % d places.",!);
OUTPUT;
Enter First String : SUNDAY
Enter Second String: M O NDAY
Similar Characters Found in Both String.
N N
D D
A A
Y Y
The Strings are different at 2 places.
The String Characters are similar at 4 places.
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.
310 Programming and Data Structures
Explanation In the above program three characters are entered in the character variable 'x ','y ' and
*z' and their base addresses are stored in the pointers 'xp ', 'yp' and 'zp' respectively. The function
memcmp ( ) is used to compare two pointers for a specified length. If the first two characters are the
same a message is displayed and control goes to next label. In the next label second and third
characters are compared. Using the value of variable ' s t a t ' locations of characters are decided.
9.34 Write a program to compare two strings irrespective of cafce. Compare the characters at
the specific position. If they are same display ' The characters are same at that
position".
#include <stdio.h>
#include <string.h>
# include <ccnio.h>
void main ()
{
char *bufl = "computer" ;
char *buf2 a "computer";
int a tat;
stat a memicmp (buf 1, buf2, 4);
clrscr ();
printf("The Characters up to 4th position are ");
if (stat) *1 if stat is non zero then prints'not' otherwise 'sam e'.t */
printf("not");
printf("same ");
I
OUTPUT:
The Characters up to 4th position are same.
Explanation The memicmp () function compares two strings for a specified number of characters. It
returns zero if both the strings are same up to a specified length of characters. Otherwise non-zero
value will be returned. Depending upon the value it returns, the i f statement displays the respective
message.
9.35 Write a program to read a string. Print the string up to the first occurrence of character
entered through the keyboard.
#include <string.h>
#include <stdio.h>
# include ccomio. h>
void main O
{
char src[2 0],dest[50 ],*ptr,*sr,f;
c lrs c r();
printf ("n Enter a String: " );
gets(src);
printf ("n Enter a Character to find in the text:
scanf ("%c",/kf);
..................Content has been hidden....................

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