Skip to main content

Array of Strings

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