Loop Control Statements 179
while (scan[i]!='Of)
I
if(scatt[i]>=48 && scan[i]<=57)
num[++n]=scan[i];
else
if (scan[i]>=65 && scan[i]<-90)
cap[++c]=scan[i];
else
if(scan[i]>=97&&c (scan[i]<=122))
small[++s]=scan[i];
else
oth[++h]=sam[i];
/++;
I
printf(" Capital Letters
for (i=0,i<20,n++)
printf ("%c"£ap[i]);
printf("]nSmall Letters
for (i=0,i<20,i++)
printf ("%c",stnall[i]);
printf("]nNumaric Letters
for (.=0,*i<20,~i++)
printf (“%c”,num[i]);
printf("] Other Letters
for(i=0,H<20,n++)
prin tf ("% c ",o th[i ]);
printf CT);
getcheO;
I
OUTPUT:
Enter Text Here: HAVE A NICE DAY, contact me on 51606.
Capital Letters: [HAVEANICEDAY ]
Small Letters: [contactmeon ]
Numaric Letters : [ 51606 ]
Other Letters: [, ]
Explanation The array sca n [40] contains the string. The string is to be entered by the user. The
arrayssmall [20],num [20] &oth [20] are used at run time in the program. Thewhile loop reads
thearrayscan [] characterby character. The i f . .else la d d e r statements checks the characters
whether the character is small, capital or symbol and assigns it to the different arrays according to its
case or type.
180 Programming and Data Structures
6.66 / 6.67 Write a program to sort numbers 0 to 9, alphabets in upper and lower case using
equivalent ASCII values. The following table can be used.
ASQIvajues. CQjnreg.pgndjpg%mbQls
48 to 57 0 to 9
65 to 90 A to Z
97 to 120 a to z
#include <stdio.h>
mainO
{
char a;
int i=48;
clrscrO;
printf ( NUMBERS ");
while (i<=57)
I
printf C%c",i);
«'++;
I
i+=7;
printf (" CAPITAL A L PH A BETS );
while (i<=90)
I
printf (" °/oc",i);
i++;
I
printf (“ n SMALL ALPHABETS ");
while (i<=122)
{
printf ("%c"ri);
;
OUTPUT:
NUMBERS
0123456789
CAPITAL ALPHABETS
ABCDEFGHIJKLMNOPQRSTUVWXYZ
SMALL ALPHABETS
abcdefghijklmnopqrstuvwxyz
Loop Control Statements 181
Explanation In the above program threewhile loops are used. These threewhile loops are used for
sorting numbers, capital letters, and lower letters. The variable ' i ' is initialized to 48 because the
ASCII 48 is 'O'. The threewhile loops check ASCII values as per the ranges given in the Table. The
number, capital and small letters are not continuos in ASCI I. Some symbols are also present between
these values. To ignore these symbols, variable x i ' increases twice with 7 and 6.
OR
#include <stdio.h>
#include <ccnio.h>
#include <process.h>
madn()
{
Int 1*48;
c lr s c r O ;
printf ("Numbers
while (i<124)
I
if(i<58)
printf (“%2c",i);
else
{
if (i==58)
{
printf (" Capital Letters:");
i+—7;
1
if (i>64 && i<91)
printf (“%2c",i);
if (i==90)
{
printf ("n Small Letters
i+=7;
1
if(i>96&c& i<123)
printf r%2c",i);
)
i++;
II
Explanation The output of the above program is same as the previous one. Instead of nested while
loop, nested i f . .e ls e statements are used.
182 Programming and Data Structures
6.68 Write a program to display all ASCII numbers and their equivalent character, numbers
and symbols. Use while loop. User should prompt every time to press 'Y' or 'N'. If user
presses /Y/ display next alphabet otherwise terminate the program.
# include <stdio.h>
# include <conio.h>
void main ()
{
Int i=64;
char c = 'Y ';
c lrs c rO ;
while (c=='Y')
I
printf (" %c (%d)",i,i);
i++;
printf (" Print (YIN)");
c=getch();
I
OUTPUT:
@(64)
Print (Y/N) A(65)
Print (Y/N) B(66)
Print (Y/N) C(67)
Print (Y/N)
Explanation In the above program variable ' i ' is initialized to 64 and character variable 1 c ' is
equated to % Y'. The ASCII numbers 65 onwards are printed together with their equivalent characters
& symbols. Thewhile loop checks user's response by pressing key XY' or 'N'. If it is *Y' ASCIInumber
and it's equivalent symbol are displayed. There after ' i ' is increased for next value. This procedure is
repeated till the user enters %N'. After this program is terminated.
6.69 Write a program to convert entered character into its opposite case. For example *A'
should be converted to 'a' and vice versa. This procedure can be applied for all the
characters. While the user enters 'E', program should find it's lower case and will
terminate.
# include <stdio.h>
# include <conio.h>
# include <ctype.h>
void main ()
{
char c ;
c lrs c r O ;
printf (" * Enter' to exit");
printf ("n* Enter only characters");
while (c!='E')
I
Loop Control Statements 183
printf (" Enter a character
c=getche();
if(c~toupper(c))
printf (" lt's Lower Case :%c",c+32);
else
printf (" lt's Upper Case :%c",c-32);
OUTPUT;
* Enter 'E' to exit
* Enter only characters
Enter a character; s It's Upper Case :S
Enter a character: A It's Lower Case : a
Enter a character: E It's Lower Case : e
Explanation In the above mentioned program character variable ' c # is declared. The value is read
through getch e () function and stored in variable 'cIf the entered character which is stored in 'c',
is in upper case, then it's small case is printed or vice versa. The difference between all capital and
their small case characters in ASCII value is always 32. For example % A' has ASCII value 65 and for
' a ' has 97 and the difference between them is 32. When the entered character is in uppercase 32 is to
be added in its ASCII value to get the character in lower case. Otherwise for conversion of lower to
upper case 32 is subtracted from its ASCII value.
6.70 Write a program to use three while nested loops. Print numbers after each iteration &
messages after termination of each loop.
#include<stdio. h>
#include <conio.h>
void main ()
{
int i=l,j=l,k=l;
clrscr();
while (i<4)
I
while (j<3)
I
while (k<2)
{
printf (" i=%dj=%d k~%d",i,j,k);
/c++,*
1
printf C'n Inner Loop (k) Completed/');
k-1;
/++;
I
printf ("n Middle Loop (j) Completed.");
H :
..................Content has been hidden....................

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