Thursday, May 30, 2013

Store input strings in an array of strings

Yes , it is possible to store the input strings from the user to store it into an array of strings . We know , an array of 5 strings s can be created and an input string can be stored in a string str as follows
 
   char *s[5];
   gets(str); 
Using the above two methods , we made a program to input strings in an array of strings .

// program to store input strings in an array of strings 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    char *strings[10];
    int i = 0;
    
    while(i < 10)
    {
        strings[i++] = (char *)malloc(100);
        gets(strings[i-1]);
        printf("\nstrings[%d] = %s",i-1,strings[i-1]);
    }
    
    return 0;
    
}


INPUT :

hello
Code
how
are you
good
na
me 
too
haaaaaa
Heeeee
 
OUTPUT : strings[0] = hello strings[1] = Code strings[2] = how strings[3] = are you strings[4] = good strings[5] = na strings[6] = me strings[7] = too strings[8] = haaaaaa strings[9] = Heeeee