Program 48) Write a program to illustrate the comparison of structure variables.
This program shows that how a structure variable can be copied into another of the same type. It also performs member-wise comparison to decide whether two structure variables are identical.
PROGRAM
struct class
{
int number ;
char name [20] ;
float marks:
} ;
main ( )
{
int x ;
struct class student1 = {111, "Rao", 72.50} ;
struct class student2 = {222, "Reddy", 67.00} ;
struct class student3 ;
student3 = student2 ;
x = ( (student3.number == student2.number) &&
(student3.marks == student2.marks) ) ? 1 : 0 ;
if (x ==1)
{
printf ("\nstudent2 and student3 are same\n\n") ;
printf ("%d %s %f\n", student3.number,
student3.name, student3.marks) ;
}
else
printf ("\nstudent2 and student3 are different\n\n") ;
}
Output: student2 and student3 are same
222 Reddy 67.000000
This site provides information on every topic in Hindi language. This is the first Hindi site which provide knowledge in Hindi language.
Defining and accessing structures members
Program 47) Define a structure type, struct personal that would contain person name, data of joining and salary. Using this structure, write a program to read this information for one person from the keyboard and print the same on the screen.
Structure definition along with the program is shown below. The scanf and printf functions illustrate how the member operator '.' is used to link the structure member to the structure variables. The variable name with a period and the member name is used like an ordinary variable.
PROGRAM
struct personal
{
char name [20] ;
int day ;
char month [10] ;
int year ;
float salary ;
} ;
main ( )
{
struct personal person ;
printf ("Input values\n") ;
scanf ("%s %d %s %f",person.name, &person.day,
person.month, &person. year, person salary) ;
}
Output: Input values
M .L.Geol 10 January 1945 4500
M.L.Goel 10 January 1945 4500.00
Structure definition along with the program is shown below. The scanf and printf functions illustrate how the member operator '.' is used to link the structure member to the structure variables. The variable name with a period and the member name is used like an ordinary variable.
PROGRAM
struct personal
{
char name [20] ;
int day ;
char month [10] ;
int year ;
float salary ;
} ;
main ( )
{
struct personal person ;
printf ("Input values\n") ;
scanf ("%s %d %s %f",person.name, &person.day,
person.month, &person. year, person salary) ;
}
Output: Input values
M .L.Geol 10 January 1945 4500
M.L.Goel 10 January 1945 4500.00
Illustration of static variable
Program 46) Write a program to illustrate the properties of static variable.
This program explains the behaviour of a static variable
PROGRAM
void stat (void) ;
main ( )
{
int i ;
for (i = 1; i <=3; i++)
stat ( ) ;
}
void stat (void)
{
static int x = 0 ;
x = x + 1 ;
printf ("x = %d\n", x) ;
}
Output: x = 1
x = 2
x = 3
A static variable is initialized only once, when the program is compiled. It is never initialized again. During the first call to stat, x is incremented to 1. Because x is static, this value persists and therefore, the next call adds another 1 to x giving it a value of 2. The value of x becomes three when the third call is made.
Had we declared x as an auto variable, the output would have been:
x = 1
x = 1
x = 1
This is because each time stat is called, the auto variable x is initialized to zero. When the function terminates, ita value of 1 is lost.
An external static variable is declared outside of all functions and is available to all the functions in that program. The difference between a static external variable and a simple external variable is that the static external variable is available only within the file where it is defined while the simple external variable can be accessed by other files.
It is also possible to control the scope of the function. For example, we would like a particular function accessible only to the functions in the file in which it is defined, and not to any function in other files. This can be accomplished by defining 'that' function with the storage class static.
This program explains the behaviour of a static variable
PROGRAM
void stat (void) ;
main ( )
{
int i ;
for (i = 1; i <=3; i++)
stat ( ) ;
}
void stat (void)
{
static int x = 0 ;
x = x + 1 ;
printf ("x = %d\n", x) ;
}
Output: x = 1
x = 2
x = 3
A static variable is initialized only once, when the program is compiled. It is never initialized again. During the first call to stat, x is incremented to 1. Because x is static, this value persists and therefore, the next call adds another 1 to x giving it a value of 2. The value of x becomes three when the third call is made.
Had we declared x as an auto variable, the output would have been:
x = 1
x = 1
x = 1
This is because each time stat is called, the auto variable x is initialized to zero. When the function terminates, ita value of 1 is lost.
An external static variable is declared outside of all functions and is available to all the functions in that program. The difference between a static external variable and a simple external variable is that the static external variable is available only within the file where it is defined while the simple external variable can be accessed by other files.
It is also possible to control the scope of the function. For example, we would like a particular function accessible only to the functions in the file in which it is defined, and not to any function in other files. This can be accomplished by defining 'that' function with the storage class static.
Illustration of properties of global variables
Program 45) Write a multifunction program to illustrate the properties of global variables.
This program illustrate the properties of global variables. Note that variable x is used in all functions but none except fun2, has a definition for x. Because x has been declared 'above' all the functions, it is available to each function without having to pass x as a function argument. Further, since the value of x is directly available, we need not use return(x) statements in fun1 and fun3. However, since fun2 has a definition of x, it returns its local value of x and therefore uses a return statement. In fun2, the global x is not visible. The local x hides its visibility here.
PROGRAM
int fun1 (void) ;
int fun2 (void) ;
int fun3 (void) ;
int x ; /* Global */
main ( )
{
x = 10 ; /* Global */
printf ("x = %d\n", x) ;
printf ("x = %d\n", fun1() ) ;
printf ("x = %d\n", fun2() ) ;
printf ("x = %d\n", fun3() ) ;
}
fun1 (void)
{
x = x + 10 ;
}
int fun2 (void)
{
int x ; /* Loacl */
x = 1 ;
return (x) ;
}
fun3 (void)
{
x = x + 10 ; /* Global */
}
Output: X = 10
X = 20
X = 1
X = 30
Once a variable has been declared as global, any function can use it and change its value. Then, subsequent functions can reference only that new value.
This program illustrate the properties of global variables. Note that variable x is used in all functions but none except fun2, has a definition for x. Because x has been declared 'above' all the functions, it is available to each function without having to pass x as a function argument. Further, since the value of x is directly available, we need not use return(x) statements in fun1 and fun3. However, since fun2 has a definition of x, it returns its local value of x and therefore uses a return statement. In fun2, the global x is not visible. The local x hides its visibility here.
PROGRAM
int fun1 (void) ;
int fun2 (void) ;
int fun3 (void) ;
int x ; /* Global */
main ( )
{
x = 10 ; /* Global */
printf ("x = %d\n", x) ;
printf ("x = %d\n", fun1() ) ;
printf ("x = %d\n", fun2() ) ;
printf ("x = %d\n", fun3() ) ;
}
fun1 (void)
{
x = x + 10 ;
}
int fun2 (void)
{
int x ; /* Loacl */
x = 1 ;
return (x) ;
}
fun3 (void)
{
x = x + 10 ; /* Global */
}
Output: X = 10
X = 20
X = 1
X = 30
Once a variable has been declared as global, any function can use it and change its value. Then, subsequent functions can reference only that new value.
Working of automatic variables
Program 44) Write a multifunction to illustrate how automatic variables work.
A program with two subprograms function1 and function2 is shown below. m is an automatic variable and it is declared at the beginning of each function. m is initialized to 10, 100, and 1000 in function1, function2, and main respectively.
When executed, main calls function2 which in turn calls function1. When main is active, m = 1000; but when function2 is called, the main's m is temporarily put on t he shelf and the new local m = 100 becomes active. Similarly, when function1 is called, both the previous values of m are put on the shelf and t he latest value of m (=10) becomes active. As soon as function1 (m=10) is finished, function2 (m=100) takes over again. As soon it is done, main (m=1000) takes over. The output clearly shows that the value assigned to m in one function does not affect its value in the other functions; and the local value of m is destroyed when it leaves a function.
PROGRAM
void function1 (void) ;
void function2 (void) ;
main ( )
{
int m = 1000 ;
function2 ( ) ;
printf ("%d\n",m) ; /* Third output */
}
void function1 (void)
{
int m = 10 ;
printf ("%d\n",m) ; /* First output */
}
void function2 (void)
{
int m = 100 ;
function1 ( ) ;
printf ("%d\n",m) ; /* Second output */
}
Output: 10
100
1000
There are two consequences of the scope and longevity of auto variables worth remembering. First, any variable local to main will be normally alive throughout the whole program, although it is active only in main. Secondly, during recursion, the nested variables are unique auto variables, a situation similar to function-nested auto variables with identical names.
A program with two subprograms function1 and function2 is shown below. m is an automatic variable and it is declared at the beginning of each function. m is initialized to 10, 100, and 1000 in function1, function2, and main respectively.
When executed, main calls function2 which in turn calls function1. When main is active, m = 1000; but when function2 is called, the main's m is temporarily put on t he shelf and the new local m = 100 becomes active. Similarly, when function1 is called, both the previous values of m are put on the shelf and t he latest value of m (=10) becomes active. As soon as function1 (m=10) is finished, function2 (m=100) takes over again. As soon it is done, main (m=1000) takes over. The output clearly shows that the value assigned to m in one function does not affect its value in the other functions; and the local value of m is destroyed when it leaves a function.
PROGRAM
void function1 (void) ;
void function2 (void) ;
main ( )
{
int m = 1000 ;
function2 ( ) ;
printf ("%d\n",m) ; /* Third output */
}
void function1 (void)
{
int m = 10 ;
printf ("%d\n",m) ; /* First output */
}
void function2 (void)
{
int m = 100 ;
function1 ( ) ;
printf ("%d\n",m) ; /* Second output */
}
Output: 10
100
1000
There are two consequences of the scope and longevity of auto variables worth remembering. First, any variable local to main will be normally alive throughout the whole program, although it is active only in main. Secondly, during recursion, the nested variables are unique auto variables, a situation similar to function-nested auto variables with identical names.
Sorting of array element using a function
Program 43) Write a program that uses a function to sort an array of integers.
A program to sort an array of integers using the function sort( ). Its output clearly shows that a function can change the values in an array passed as an argument.
PROGRAM
void sort (int m, int x[ ]) ;
main ( )
{
int i ;
int marks [5] = {40, 90, 73, 81, 35} ;
printf ("Marks before sorting\n") ;
for (i = 0; i < 5; i++)
printf ("%d", marks [i]) ;
printf ("\n\n") ;
sort (5, marks) ;
printf ("Marks after sorting \n") ;
for (i = 0; i < 5; i++)
printf ("%4d", marks [i]) ;
printf ("\n") ;
}
void sort (intm, int x[ ])
{
int i, j, t ;
for (i = 1; i<= m - 1; i++)
for (j = 1; j<= m - i; j++)
if (x [j - 1] >= x [j])
{
t = x [j - 1] ;
x [j - 1] = x [j] ;
x [j] = t ;
}
}
Output: Marks before sorting
40 90 73 81 35
Marks after sorting
35 40 73 81 90
A program to sort an array of integers using the function sort( ). Its output clearly shows that a function can change the values in an array passed as an argument.
PROGRAM
void sort (int m, int x[ ]) ;
main ( )
{
int i ;
int marks [5] = {40, 90, 73, 81, 35} ;
printf ("Marks before sorting\n") ;
for (i = 0; i < 5; i++)
printf ("%d", marks [i]) ;
printf ("\n\n") ;
sort (5, marks) ;
printf ("Marks after sorting \n") ;
for (i = 0; i < 5; i++)
printf ("%4d", marks [i]) ;
printf ("\n") ;
}
void sort (intm, int x[ ])
{
int i, j, t ;
for (i = 1; i<= m - 1; i++)
for (j = 1; j<= m - i; j++)
if (x [j - 1] >= x [j])
{
t = x [j - 1] ;
x [j - 1] = x [j] ;
x [j] = t ;
}
}
Output: Marks before sorting
40 90 73 81 35
Marks after sorting
35 40 73 81 90
Power functions: Illustration of return of float values
Program 42) Write a function power that computes x raised to the power y for integers x and y and returns doublr type value.
This program shows a power function that returns a double. The prototype declaration
double power (int, int) ;
appears in main, before power is called.
PROGRAM
main ( )
{
int x, y ; /* Input data */
double power (int, int) ; /* Prototype declaration */
printf ("Enter x,y:") ;
scanf ("%d %d", &x, &y) ;
printf ("%d to power %d is %f\n", x, y, power (x,y)) ;
}
double power (int x, int y) ;
{
double p ;
p = 1.0 ; /* x to power zero */
if (y >= 0)
while (y--) /* Computes positive powers */
p *= x ;
else
while (y++) /* Computes negative powers */
p /= x ;
return (p) ; /* Returns double type */
}
Output: Enter x, y : 16 2
16 to power 2 is 256.000000
Enter x, y : 16 -2
16 to power -2 is 0.003906
Another way to guarantee that power's type is declared before it is called in main is to define the power function before we define main. Power's type is then known from its definition, so we longer need its type declaration in main.
This program shows a power function that returns a double. The prototype declaration
double power (int, int) ;
appears in main, before power is called.
PROGRAM
main ( )
{
int x, y ; /* Input data */
double power (int, int) ; /* Prototype declaration */
printf ("Enter x,y:") ;
scanf ("%d %d", &x, &y) ;
printf ("%d to power %d is %f\n", x, y, power (x,y)) ;
}
double power (int x, int y) ;
{
double p ;
p = 1.0 ; /* x to power zero */
if (y >= 0)
while (y--) /* Computes positive powers */
p *= x ;
else
while (y++) /* Computes negative powers */
p /= x ;
return (p) ; /* Returns double type */
}
Output: Enter x, y : 16 2
16 to power 2 is 256.000000
Enter x, y : 16 -2
16 to power -2 is 0.003906
Another way to guarantee that power's type is declared before it is called in main is to define the power function before we define main. Power's type is then known from its definition, so we longer need its type declaration in main.
Functions with no argument and no return values
Program 41) Write a program with multiple functions that do not communicate any data between them.
A program with three user-defined function is given below main is the calling functions that calls printline and value functions. Since both the called functions contain no argument, there are no argument declarations. The printline function, when encountered, prints a line with a length of 35 characters as prescribed in the function. The value function calculates the value of principal amount after a certain period of years and prints the results. The following equation is evaluated repeatedly:
Value = Principal (1 + interset-rate)
PROGRAM
/* Function declaration */
void printline (void) ;
void value (void) ;
main ( )
{
printline ( ) ;
value ( ) ;
printline ( ) ;
}
/* Function1 : printline ( ) */
void printline (void) /* contains no argument */
{
int 1 ;
for (i = 1; i <= 35; i++)
printf ("%c", '-') ;
printf ("\n") ;
}
/* Function2 : value ( ) */
void value (void) /* contains no argument */
{
int year, period ;
float inrate, sum, principal ;
printf ("Principal amount?") ;
scanf ( "%f", &principal) ;
printf ("Interest rate? ") ;
scanf ("%f", &inrate) ;
printf ("Period? ") ;
scanf ( "%d", &period) ;
sum = principal ;
year = 1 ;
while (year <= period)
{
sum = sum * (1 + inrate) ;
year = year + 1 ;
}
printf ("\n%8.2f %5.2f %5d %12.2f\n", principal,
inrate, period, sum) ;
}
Output: ---------------------------------------------------------
Principal amount? 5000
Interest rate? 0.12
Period 5
5000.00 0.12 5 88.71
---------------------------------------------------------
It is important to note that the function value recieves its data directly from the terminal. The input data include principal amount, interest rate and the period for which the final value is to be calculated. The while loop calculates the final value and the results are printed by the library function printf. When the closing brace of value() is reached, the control is transferred back to the calling function main. Since everything is done by the value itself there is in fact nothing left to be sent back to the called function. Return types of both printline and value are declared as void.
Note that no return statement is employed. When there is nothing to be returned, the return statement is optional. The closing brace of the function signals the end execution of the function, thus returning the control, back to the calling function.
A program with three user-defined function is given below main is the calling functions that calls printline and value functions. Since both the called functions contain no argument, there are no argument declarations. The printline function, when encountered, prints a line with a length of 35 characters as prescribed in the function. The value function calculates the value of principal amount after a certain period of years and prints the results. The following equation is evaluated repeatedly:
Value = Principal (1 + interset-rate)
PROGRAM
/* Function declaration */
void printline (void) ;
void value (void) ;
main ( )
{
printline ( ) ;
value ( ) ;
printline ( ) ;
}
/* Function1 : printline ( ) */
void printline (void) /* contains no argument */
{
int 1 ;
for (i = 1; i <= 35; i++)
printf ("%c", '-') ;
printf ("\n") ;
}
/* Function2 : value ( ) */
void value (void) /* contains no argument */
{
int year, period ;
float inrate, sum, principal ;
printf ("Principal amount?") ;
scanf ( "%f", &principal) ;
printf ("Interest rate? ") ;
scanf ("%f", &inrate) ;
printf ("Period? ") ;
scanf ( "%d", &period) ;
sum = principal ;
year = 1 ;
while (year <= period)
{
sum = sum * (1 + inrate) ;
year = year + 1 ;
}
printf ("\n%8.2f %5.2f %5d %12.2f\n", principal,
inrate, period, sum) ;
}
Output: ---------------------------------------------------------
Principal amount? 5000
Interest rate? 0.12
Period 5
5000.00 0.12 5 88.71
---------------------------------------------------------
It is important to note that the function value recieves its data directly from the terminal. The input data include principal amount, interest rate and the period for which the final value is to be calculated. The while loop calculates the final value and the results are printed by the library function printf. When the closing brace of value() is reached, the control is transferred back to the calling function main. Since everything is done by the value itself there is in fact nothing left to be sent back to the called function. Return types of both printline and value are declared as void.
Note that no return statement is employed. When there is nothing to be returned, the return statement is optional. The closing brace of the function signals the end execution of the function, thus returning the control, back to the calling function.
Counting of charcaters, words and lines in a text
Program 39) One of the practical applications of string manipulations is counting the words in a text. We assume that a word is a sequence of any characters, except escape characters and blanks, and that two words are separated by one blank character. The algorithm for counting words is as follows:
1. Read a line of text.
2. Beginning from the first character in the line, look for
blank. If a blank found, increment words by 1.
3. Continue steps 1 and 2 until the last line is completed.
The implementation of this algorithm is shown below. The first while loop will be executed once for each line of text. The end of text is indicated by pressing the 'Return' Key an extra time after the entire text has been entered. The extra 'Return' Key causes a newline character as input to the last line and as a result, the last line contains only the null character.
The program checks for this special line using the test
if (line [0] == '\0')
and if the first (and only the first) character in the line is a null character, then counting is terminated. Note the difference between a null character and a blank character.
PROGRAM
#include <stdio.h>
main ( )
{
char line [81], ctr ;
int i, c, end = 0, words = 0, lines = 0 ;
printf ("KEY IN THE TEXT. \n") ;
printf ("GIVE ONE SPACE AFTER EACH WORD. \n") ;
printf ("WHEN COMPLETED, PRESS 'RETURN' .\n\n") ;
while (end == 0)
{
/* Reading a line of text */
c = 0 ;
while ( (ctr = getchar() ) ! = '\n') ;
line [c++] = ctr ;
line [c] = '\0' ;
/* counting the words in a line */
if (line [0] == '\0')
break ;
else
{
words++ ;
for (i = 0; line[i] != '\0'; i++)
if (line [i] == ' ' | | line [i] == '\t')
words++ ;
}
/* Counting lines and characters */
lines = lines + 1 ;
characters = characters + strlen(line) ;
}
printf ("\n") ;
printf ("Number of lines = %d\n", lines) ;
printf ("Number of words = %d\n", words) ;
printf ("Number of characters = %d\n", charcaters) ;
}
Output: KEY IN THE TEXT
GIVE ONE SPACE AFTER EACH WORD.
WHEN COMPLETD, PRESS 'RETURN'.
Admiration is a very short-lived passion.
Admiration involves a glorious obliquity of vision.
Always we like those who admire us but we do not
Like those whom we admire.
Fools admire, but men of sense approve.
Number of lines = 5
Number of words = 36
Number of characters = 205
The program also counts the number of lines read and the total number of characters in the text. Remember, the last line containing the null string is not counted.
After the first while loop is exited, the program prints the result of counting.
1. Read a line of text.
2. Beginning from the first character in the line, look for
blank. If a blank found, increment words by 1.
3. Continue steps 1 and 2 until the last line is completed.
The implementation of this algorithm is shown below. The first while loop will be executed once for each line of text. The end of text is indicated by pressing the 'Return' Key an extra time after the entire text has been entered. The extra 'Return' Key causes a newline character as input to the last line and as a result, the last line contains only the null character.
The program checks for this special line using the test
if (line [0] == '\0')
and if the first (and only the first) character in the line is a null character, then counting is terminated. Note the difference between a null character and a blank character.
PROGRAM
#include <stdio.h>
main ( )
{
char line [81], ctr ;
int i, c, end = 0, words = 0, lines = 0 ;
printf ("KEY IN THE TEXT. \n") ;
printf ("GIVE ONE SPACE AFTER EACH WORD. \n") ;
printf ("WHEN COMPLETED, PRESS 'RETURN' .\n\n") ;
while (end == 0)
{
/* Reading a line of text */
c = 0 ;
while ( (ctr = getchar() ) ! = '\n') ;
line [c++] = ctr ;
line [c] = '\0' ;
/* counting the words in a line */
if (line [0] == '\0')
break ;
else
{
words++ ;
for (i = 0; line[i] != '\0'; i++)
if (line [i] == ' ' | | line [i] == '\t')
words++ ;
}
/* Counting lines and characters */
lines = lines + 1 ;
characters = characters + strlen(line) ;
}
printf ("\n") ;
printf ("Number of lines = %d\n", lines) ;
printf ("Number of words = %d\n", words) ;
printf ("Number of characters = %d\n", charcaters) ;
}
Output: KEY IN THE TEXT
GIVE ONE SPACE AFTER EACH WORD.
WHEN COMPLETD, PRESS 'RETURN'.
Admiration is a very short-lived passion.
Admiration involves a glorious obliquity of vision.
Always we like those who admire us but we do not
Like those whom we admire.
Fools admire, but men of sense approve.
Number of lines = 5
Number of words = 36
Number of characters = 205
The program also counts the number of lines read and the total number of characters in the text. Remember, the last line containing the null string is not counted.
After the first while loop is exited, the program prints the result of counting.
Sorting of strings in alphabetical order
Program 37) Write a program that would sort a list of names in alphabetical order.
A program to sort the list of strings in alphabetical order. It employs the method of bubble sorting.
PROGRAM
#define ITEMS 5
#define MAXCHAR 20
main ( )
{
char string [ITEMS] [MAXCHAR],
dummy [MAXCHAR] ;
int i = 0, j = 0 ;
/* Reading the list */
printf (" Enter names of %d items \n ", ITEMS) ;
while (i < ITEMS)
scanf ("%s", string [i++]) ;
/* Sorting begins
for (i =1; i < ITEMS; i++) /* Outer loop begins */
{
for (j=1; j<= ITEMS - i; j++) /* Inner loop begins */
{
if (strcmp (string [j - 1], string [j] > 0)
{ /* Exchange of contents */
strcpy (dummy, string [j - 1]) ;
strcpy (string [j - 1], string [j] ) ;
strcpy (string [j], dummy ) ;
}
} /* Inner loop ends */
} /* Outer loop ends */
/* Sorting completd */
printf ("\nAlpphabetical list \n\n") ;
for (i =0; i < ITEMS; i++)
printf ("%s", string [i] ) ;
}
Output: Enter name of 5 items
London Manchester Delhi Paris Moscow
Alphabetical list
Delhi
London
Manchester
Moscow
Paris
Note that a two dimensional array is used to store the list of strings. Each string is read using a scanf function with %s format. Remember, if any string contains a while space, them the parts of the string after the white space will be treated as another item in the list by the scanf. In such cases, we should read the entire line as a string using a suitable algorithm. For example, we can use gets function to read a line of text containing a series of words. We may also use puts function in place of scanf for output.
A program to sort the list of strings in alphabetical order. It employs the method of bubble sorting.
PROGRAM
#define ITEMS 5
#define MAXCHAR 20
main ( )
{
char string [ITEMS] [MAXCHAR],
dummy [MAXCHAR] ;
int i = 0, j = 0 ;
/* Reading the list */
printf (" Enter names of %d items \n ", ITEMS) ;
while (i < ITEMS)
scanf ("%s", string [i++]) ;
/* Sorting begins
for (i =1; i < ITEMS; i++) /* Outer loop begins */
{
for (j=1; j<= ITEMS - i; j++) /* Inner loop begins */
{
if (strcmp (string [j - 1], string [j] > 0)
{ /* Exchange of contents */
strcpy (dummy, string [j - 1]) ;
strcpy (string [j - 1], string [j] ) ;
strcpy (string [j], dummy ) ;
}
} /* Inner loop ends */
} /* Outer loop ends */
/* Sorting completd */
printf ("\nAlpphabetical list \n\n") ;
for (i =0; i < ITEMS; i++)
printf ("%s", string [i] ) ;
}
Output: Enter name of 5 items
London Manchester Delhi Paris Moscow
Alphabetical list
Delhi
London
Manchester
Moscow
Paris
Note that a two dimensional array is used to store the list of strings. Each string is read using a scanf function with %s format. Remember, if any string contains a while space, them the parts of the string after the white space will be treated as another item in the list by the scanf. In such cases, we should read the entire line as a string using a suitable algorithm. For example, we can use gets function to read a line of text containing a series of words. We may also use puts function in place of scanf for output.
Subscribe to:
Comments (Atom)