212 Programming and Data Structures
char msgl] = "C is Easy";
inti = 0;
clrscrO;
while (msglU)
putc(msg[i++], stdout);
I
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".
7.6 PREDEFINED STREAMS
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
12] PREPROCESSOR DIRECTIVES.
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;
clrscrO;
printf("lnput aText:");
for (i=0;i<10;i++)
chli]=getc(stdin);
printf("The Text inputted was");
for (i=0,i<10;i++)
Arrays 213
putc(ch[i]#tdout);
I
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];
clrscrO;
printf (“Enter Text Below
gets(text);
clrscrO;
printf (“Sorted Text Below An");
for (i=65,i<=90,-i++)
/
for (j=0,j<30,-j++)
I
if( textlj]=-toupper(i) I I text[j]==toloweiii))
printf C%c",text[j]);
I
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 */
OUTPUT:
Enter Text Below:
Hello
Sorted Text Below:
Ehllo
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.
mainO
{
int i, j,suma>0,a[10];
clrscrO;
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++)
I
if(i a[j])
printf ("%3d",a[j]);
I
I
printf (" Numbers In Descending Order:");
for (i=sum,i>=0,-i~)
{
for (j=0;j<10;j++)
I
if(i==a[j])
printf ("%3d",alj]);
I
I
}
OUTPUT:
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,
Arrays 215
' sum' variable contains addition of all the ten entered numbers. The inner loop checks all the values
of an array a [10] with current value of outer loop. The i f statement checks the value of outer loop
with entire array, when it finds that the match number gets printed. The o u ter loop continues till it
reaches the value of variable ' sum'. The outer loop is in ascending order. So the elements of an array
a [10] are printed in the ascending order. For descending order the outer fo r loop is made descending.
OR
7.21 Write a program to sort the numbers in ascending order by comparison method.
mainO
{
Int i, j,n,num[10] ,tenp;
clrscrO?
printf ("Enter how many numbers to sort
scanf ("%d",&cn);
for (i=0,-i<n,i++)
{
printf ("Enter numbers # %2d:",I+l); '
scanf ("%d",&num[i]);
printf (“The Numbers Entered through keyboard n");
for (i=0,i<n,i++)
printf ("%4d",num[i]);
for (i=0d<tt-l;i++)
for (j=i+l,-j<n,j++)
{
if(num[i]>num[j])
temp=num[i];
num[i]=num[j];
num[j]=temp;
}
}
}
printf ("n The Numbers in ascending order n");
for (i=0,i<n;i++)
printf("%4d",num[i]);
jjetchO;
OUTPUT:
Enter how many numbers to sort: 5
Enter numbers # 1: 8
Enter numbers # 2:3
Enter numbers # 3:2
Enter numbers # 4:1
Enter numbers # 5: 9
The Numbers Entered through keyboard
83219
The Numbers in ascending order
12389
..................Content has been hidden....................

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