Array of String


Array of String:
An array of string can be represented by a two dimensional array where the first size specification tells the number of strings and second size specification tells the number of characters in each string.
        char day[7] [10];
Here, the above declaration , day is declare to be a two dimensional character atty that can be used to store 7 days, each of length not more than characters. Like two dimensional numeric array you can also initialize array of string as follows,
char day[7] [10] = {“Sunday”, “Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”, “Saturday”};
Here, day[0] stores “Sunday”, day [1] stores “Sunday” and so on. Finally, day[6] stores “Saturday”.
When you referring to an element of an array day. For example,
     day [i] [j];
The first index i identifies a row in the array and the second index j identifies the character within the row. So if you want to refer a complete row containing one of the string, just use a single index value within the square brackets. For example, day[1] refers to second string i.e. “Monday” .
Now consider the following program which are using two dimensional character arrays.
Program:
C program to print out the names of the day of the week.
#include <stdio.h>
#include <conio.h>
void main ()
{
int i;
char day[7] [10] = {“Sunday”, “Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”, “Saturday”};
clrscr ();
for (i=0; i<7; i++)
printf (“%s\n”, day[i]);
getch ();
}
OUTPUT
Sunday
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday

C program which takes a set of names of individuals and abbreviates the first, middle and other names except the last name by the first letter.
Eg: Raman Kumar Sharma is R.K Sharma.
Method 1: (For multiples names)
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main ()
{
char firsta[20] [20], seca[20] [20];
char surna[20] [20], name[20] [20];
char dum[20];
clrscr ();
int I,n;
printf (“How many names :”);
scanf (“%d”, &n);
printf (“Enter the first, middle and surname (all three must) :”);
printf (“\n - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\n”);
for (i=0; i<n; i++)
{
scanf (“%s%s%s”, firsta[i], seca[i], surna[i]);
}
printf (“Short name are - - - -“);
for (i=0; i<n; i++)
{
dum[0] = first1[i] [0];
dum[1] =’\0’;
strcpy(name[i], dum);
strcat(name[i], “.”);
dum[0] = seca[i] [0];
dum[1] = ‘\0’ ;
strcat(name[i], dum);
strcat (name[i], “.”);
strcat (name[i], surna[i]);
printf (“/n%s”,name[i]);
}
getch ();
}
OUTPUT
How many names : 3
Enter the first, middle and surname (all three must) :
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Pawan Kumar Bansal
Deepak Kumar rastogi
Shalinder pratap Singh
Short name are - - - -
P. K. Bansal
D. K. rastogi
S. P. Singh

Method 2: (For single name)
#include <stdio.h>
#include <conio.h>
void main ()
{
char a[80], b[80];
int i, j=0, k, l=2;
clrscr ();
printf (“Enter the first, middle and surname :”);
gets(a);
b[0] = a[0];
b[1] = ‘.’ ;
for (i=1; a[i]! =’\0’; i++)
{
if(a[i]==’ ‘)
{
j = i+2;
b[l] = a[i+1];
l++;
b[l] = ‘.’ ;
l++;
}}
l=l-1;
for (k=j; a[k]!=’\0’; k++, l++)
b[l] = a[k];
printf (“Short name :- - - -“);
for (k=0; k<l; k++)
printf (“%c”, b[k]);
getch ();
}
OUTPUT
Enter the first, middle and surname : Pawan Kumar Bansal
Short name : - - - P. K. Bansal

No comments

Powered by Blogger.