Two-dimensional string

A Two-dimensional string  is the collection of one-dimensional string. It can be named as  an array of string.

 Representation of  two-dimensional string

char fruit[5][10]= {

                                       “apple”,

                                       “cucumber”,

                                        “pineapple”,

                                        “grapes”,

                                         “mango”,

                                 }

 
Two-dimensional-string-c

Lets perform some operations

 

  •    printf(“%c”, fruit[3][4]);

              output:: e

 

  •    printf(“%d”, fruit[3][4]);

              output:: 101

              // ASCII value of e

 

  •     printf(“%u”, fruit);

               output:: 1000

               // base address

 

  •  printf(“%u”, fruit[3]);

              output:: 1030

              // address of location fruit[3]

 

  •    printf(“%s”, fruit[3]]);

              output:: grapes

 

  •    for(i=0;fruit[3][i]!=’\0’;i++)

             printf(“%c “,fruit[3][i]);

             output:: g r a p e s

 

  •    for(i=0;i<5;i++)

               printf(“%s “, fruit[i]);

               output:: apple cucumber pineapple grapes mango

 

  •    for(i=0;i<5;i++)

   { 

       printf(“\n”);

        for(j=0;fruit[i][j]!=’\0’;j++)

        printf(“%c “, fruit[i][j]);

    }

   output:: a p p l e

                c u c u m b e r

                p i n e a p p l e 

                g r a p e s

               m a n g o


 Demo Program

#include<stdio.h>
int main()
{
  char fruit[5][10];
  int i,j;
   for(i=0;i<5;i++)
    {
     printf("\enter the fruit name\n");
     scanf("%s", fruit[i]);
    }
   printf("\n******");
    for(i=0;i<5;i++)
     printf("%s \n",fruit[i]);
   return 0;
} 

output::

 enter the fruit name

 banana


 enter the fruit name

 apple


 enter the fruit name

 pineapple


 enter the fruit name

 cucumber


 enter the fruit name

 ******

 banana

 apple

 pineapple

 cucumber

 grapes

Refer also::

 C string
 string functions in c