212 Programming and Data Structures
char msgl] = "C is Easy";
inti = 0;
while (msglU)
putc(msg[i++], stdout);
Q U m K i
C is Easy
Explanation A character array and integer variable are initialized. Array always begins with element
number 0. In case % i ' is not initialized with 0 result provides garbage value. Standard stdo u t ()
function prints the string on console. Here the string is "C i s Easy".
Following are the streams
1. stdin()
2. stdout ()
3. stderrO
When C program is executed few "files" are automatically opened by the system for use by the
program. Constant FILE pointers recognize these files. Streams s td in , s tdou t and s t der r are
defined in the standard I/O include files. These are macros. Their details are illustrated in the chapter
a) stdin The file pointer s tdin identifies the standard input text and it is associated with our terminal.
All standard I/O functions perform input and do not take a FILE pointer as an argument and get their
input from s tdin.
b) stdout Similarly, it is used for outputting the text. It is a standard output stream.
c) a tderr It is an output stream in the text mode. It is a standard error stream.
7.18 Write a program to read the text through keyboard and display it by using stdin and
stdout Streams.
#include <stdio.h>
main (void)
char ch[ll] ;
int i;
printf("lnput aText:");
for (i=0;i<10;i++)
printf("The Text inputted was");
for (i=0,i<10;i++)
Arrays 213
Q U n ilL
Input a Text Hello World
The Text Entered was
Hello World
Explanation In the above program two f o r loops are used. The first f o r loop reads input characters
from the keyboard and stores in an array ch [ 11 ] by using s td in standard function. The second for
loop displays the string using stdo ut standard function.
7.19 Write a program to sort the given strings alphabetically.
# include <8tdio.h>
# include <ctype.h>
# include <canio.h>
void mainO
int i, j;
char text[30];
printf (“Enter Text Below
printf (“Sorted Text Below An");
for (i=65,i<=90,-i++)
for (j=0,j<30,-j++)
if( textlj]=-toupper(i) I I text[j]==toloweiii))
printf C%c",text[j]);
Tips The tolower and toupper are the 'C' functions for conversion from lower to upper or vice versa
or convert numerical to its ASCII equivalent .For initializing these functions header file ctype.h is to
be#included */
Enter Text Below:
Sorted Text Below:
Explanation ASCII equivalents of alphabets are used to sort out the given string. The standard
functions toupper () and tolower () in i f statement are used to ignore the case i.e. capital or small
214 Programming and Data Structures
letters are treated the same. If the character value given by f o r loop ' i ' andstring text [] value '
denoted by f o r loop % j ' are same that value gets printed. Because the value of a character supplied
by the outer f o r loop are taken alphabetically. Hence, characters when matched gets printed.
7.20 Write a program to arrange the numbers in increasing and decreasing order (ascending
& descending order) using loops.
int i, j,suma>0,a[10];
printf ("Enter ten numbers z");
for (i*0;i<10;i++)
scanf ("%d",&a[i]);
sumBsunt+a [i];
printf ("Numbers In Ascending Order:");
for (i=0,i<=sum,i++)
for (j=0,-j<10,-j++)
if(i a[j])
printf ("%3d",a[j]);
printf ("\nNumbers In Descending Order:");
for (i=sum,i>=0,-i~)
for (j=0;j<10;j++)
printf ("%3d",alj]);
Enter ten numbers :5214791012 93
Numbers In Ascending Order :123457991012
Numbers In Descending Order: 1210 99754321
Explanation In the above program ten numbers are entered through the keyboard in an array a [ 10 ].
In the same loop their sum is performed. The outer loop executes from 1 to variable 'sum'. Here,

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

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.