Saturday, August 14, 2010

Two-Dimensional or 2 d character array in C programming


One-dimensional array is fine to store a single string, but to store a number of strings we have to declare two-dimensional character type array. The declaration is simply like 2 d integer type array. Suppose we want to store the name of the months in a character array. We have twelve months in a year and the length of ‘september’ is maximum-9. So the array should be char month[12][10].The first index indicates the number of string to be stored and the second index indicates the number of locations for each string. While storing string in 2 d array we have to mention the first index.

This C program will take the name of the months from the user, store them in 2d array  and the month names will de displayed in upper case.

#include< stdio.h >
#include< string.h >
void main()
{
char month[12][10];
int i,j;
clrscr();
for(i=0;i< 12;i++)
{
printf("Enter the month name:-");
scanf("%s",&month[i]);
}
printf("Month names are:-\n");
for(i=0;i< 12;i++)
{
j=0;
while(month[i][j]!='\0')
{
printf("%c",toupper(month[i][j]));
j++;
}
printf("\n");
}
getch();
 }

No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner