276 Programming and Data Structures
Table 8.4 ASCII difference.
ASCn Value ASCn Value
Difference
78 (N) 78 (N)
0
80 (P) 75 (K)
5
65 (A) 72 (H)
-7
8.41 / 8.42 Write a program to enter names of cities and display all the entered names
alphabetically.
# Include <stdlo.h>
# include <ccoio.h>
void main()
{
char city [5] [20];
int, i # j;
c lrs c r O ;
printf ("Enter Names o f Cities.nn");
for (i=0,'i<5,'i++)
scanf ("% s "jn t y [i]);
printf ("Sorted List ofCities.nn");
for (i=65,i'<=122/i‘++>
/
for (j=0,j<5,j++)
{
if (city[j][0]ssi)
printf (u %s"jHty[j1);
}
}
OUTPUT:
Enter Names of Cities.
MUMBAI
NANDED
BANGLORE
KANPUR
INDORE
Sorted List of cities.
BANGLORE
INDORE
KANPUR
MUMBAI
NANDED
Working with Strings & Standard Functions 277
Explanation In the above program the first fo r loop is used for entering the names of the cities. The
city name can be entered in either case i.e. upper or lower case. Hence, the second fo r loop is initialized
from 65 to 122 where 65 is A S C II value of 'A ' and 122 ASCII value of ' z '. The i f statement within
the third fo r loop makes the comparison. If there is a match the city name will be displayed otherwise
the loop continues. Thus the elements of city [ ] [ ] array are displayed in sorted order.
OR
# include <std±o.h>
# Include <conio.h>
# include <string.h>
void main ()
{
char city [5] [20] ,tenp[20];
in t i , j ;
c lrs c r () ;
printf ("Enter Names o f Citiesti");
fo r (i=0,i<5,i++)
scanf ("% s",city li]);
printf ("nSorted List o f Cities");
fo r (i=l,-i<5,i++)
for (j-l,'j<5,'j++)
{
i^(strcmp(city[j-l],city[j])>0)
strcpy(tem p^ity[j-l]);
strcpy(city[j-l]£ity[j]);
strcpy(cityljbtemp);
I
}
fo r (i=0,H<5p++)
printf ("n % s",c ity[i]);
I
OUTPUT:
Enter Names of Cities.
MUMBAI
NANDED
BANGLORE
KANPUR
INDORE
Sorted List of cities.
BANGLORE
INDORE
KANPUR
MUMBAI
NANDED
278 Programming and Data Structures
Explanation In the above program standard string functions s trcmp () and s trcpy () are used.
The s trcmp () function compares two successive city names. If their ASCII difference is greater than
zero city names are exchanged. This is accomplished by the body of the i f statement. Thus, on
execution of the program cities are displayed in the alphabetical order.
8.43 / 8.44 Write a program to find number of words in a given statement. Exclude spaces
between them.
# include <stdio.h>
# include <conio.h>
void main ()
{
char text [30] ;
int count=0,i=0;
clrscrO ;
printf ("Enter The Line of Text n ");
printf ("G iv e One Space After Each w ordn");
gets(text);
while (tex t[i++ ]!= '0 ')
if(text[i]==32 I I text[i]= = '0 ')
count++;
printf ("The Number o f words in line = %dn",count);
}
OUTPUT:
Enter The Line of Text
Give One Space After Each word
Read Books
The Number of words in line = 2
Explanation In the above program string is entered. It is known that the single space separates two
consecutive words. The logic for finding the number of words in a statement is to detect number of
spaces and N U L L character. For example, in a statement "C IS A PROGRAMMING LANGUAGE" there are
four spaces and a NULL character when the string is terminated. Thus, the total characters are five
(4+1). The i f statement counts number of spaces and NULL character in the string.
OR
# include <stdio.h>
# include <conio.h>
void main ()
{
char text [30];
int count=0,i=0;
clrscrO ;
printf ("Enter Text B e low :");
gets(text);
while (tex t[i]!= '0 ')
Working with Strings & Standard Functions 279
I
if (((text[i]> =97) && (text[i]<=122)) I I ((text[i]>=65) && (text[i]< =90)))
I
»'++;
if(text[i]==32)
I
co unt++;
i++;
I
I
if(te x t[i]= = 0 ')
co unt++;
printf ("H ie Number o f words in line - %dn", count);
I
OUTPUT:
Enter Text Below: Reading is a good Habit
The Num ber o f words in line = 5
Explanation In the above program string is entered. The first i f statement within the whi 1 e loop
checks every element of the string whether it is a character or space. If it is a character variable ' i ' is
increased and checked with second i f statement. If it is space then counter variables' i ' and ' count
are increased. Thus, by counting spaces and NULL character total number of words is calculated.
8.45 Read the names of mobile customers through keyboard and sort them alphabetically on
last name. Display the sorted list on the monitor.
# include <stdio.h>
# include <string.h>
# include <conio.h>
void main ()
{
char fnama [20] [10], sname [20] [10], surname [20] [10];
char name [20] [20] ,mdbile[20] [10] ,tesqp[20];
in t i f j ;
clrscr ();
printf ("Enter Names and M obile Numbers.n " );
fo r (i=0;i<5;i++)
{
scanf ("% s %s %s %s",fname[i],sname[i],sum ame[i],mobile[i]);
strcpy(name[i],sumame[i]);
strca t(name[i]f",");
temp[0]=fname[i][0];
tem p[l]='0 f;
strcat(name[i],temp);
strca t(name[i ] , " ) ;
temp[0]=sname[i][10];
tem p [l]='0';
strcat(nanie[i],temp);
..................Content has been hidden....................

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