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 ( \n Similar Characters Found in Both String.");
while (*a!='\0')
I
if(stricmp(*a,*b)=±0)
I
printf ("\n\t%c \t%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.

Get Programming and Data Structures now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.