Sometimes useful to have an array of string values.
An array of strings is a two dimensional array of characters. This is often required in applications with a list of names, etc.
Example:
char cities[4];[10]={โPuneโ,โMumbaiโ,โDelhiโ,โChennaiโ}; Each string could be of different length.
Example: char *MonthNames[13]; /* an array of 13 strings */
MonthNames[1] = โJanuaryโ; /* String with 8 chars */
MonthNames[2] = โFebruaryโ; /* String with 9 chars */
MonthNames[3] = โMarchโ; /* String with 6 chars */
Array of Strings Example
The following program illustrates how we can accept โnโ strings and store them in an array of strings . we will accept the names of โnโ students and display them.
#include< stdio.h>
#include<string.h>
main()
{ char list[10][20];
int i,n;
printf(โ\nHow many names?โ);
scanf(โ%dโ,&n);
for(i=0;i<n;i++)
{ printf(โ\nEnetr name%dโ,i);
gets(list[i]);
}
printf(โ\nThe names in the list are:โ);
for(i=0;i<n;i++)
{
puts(list[i]);
}
}
Comments
Post a Comment
Please give us feedback through comments