Thread: multiple strings storing in array

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    99

    multiple strings storing in array

    Program for storing multiple letters
    Code:
    #include<stdio.h>
    int main (void)
    {
      unsigned int i;
      char array[6]={'s','e','a','r','c','h'}; 
      
      for(i=0; i<6; i++)
     {
       printf("print letter : %c \n", array[i]);
     }
     return 0; 
    }
    print letter : s
    print letter : e
    print letter : a
    print letter : r
    print letter : c
    print letter : h

    Program for storing multiple strings

    Code:
    #include<stdio.h>
    int main (void)
    {
      unsigned int i;
      
      char array[6]={"stuck","enk","andy","ratch","cemp","harry"}; 
      
      for(i=0; i< 6; i++)
     {
       printf("print letter : %s \n", array[i]);
     }
     return 0; 
    }
    hello.c:6:26: error: excess elements in char array initializer
    char array[6]={"stuck","enk","andy","ratch","cemp","harry"};
    ^~~~~
    I think my array size is not enough to store these six strings in array

    What's the reason of error and how can I fix it.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,611
    People usually start looking into this topic by using char matrices. There are strategies with different types, but this one is by far the easiest. First, recognize that the first dimension is the number of rows in the matrix, which is how many strings there are. Then, decide on what the longest string you want to hold can be; this makes up the second dimension, aka the columns.

    Code:
    #include <stdio.h>
    #include <string.h>
    int main(void)
    {
       unsigned int i;
       char array[6][20] = {"stuck","enk","andy","ratch","cemp","harry"};
       char name[20];
       printf("Type the name you want to search. \n=> ");
       scanf("%19s", name);
       for (i = 0; i < 6; i++)
       {
          if (strcmp(name, array[i]) == 0)
          {
              printf("name found: %s \n", array[i]);
              break;
          }
       }
    
       if (i == 6)
          printf("Didn't find the name %s in array \n", name);
      
       return 0;
    }
    
    C:\Users\jk\Desktop>foo
    Type the name you want to search.
    => andy
    name found: andy
    
    C:\Users\jk\Desktop>foo
    Type the name you want to search.
    => bob
    Didn't find the name bob in array
    Might be a fairly braindead example, but there you have it.

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    99
    thank you very much @whiteflags , That is good example

    how do you decide size array[][]
    Code:
     #include <stdio.h>
    #include <string.h>
    int main(void)
    {
       unsigned int i;
       char array[6][6] = {"stuck","enk","andy","ratch","cemp","harry"};
       
       for (i = 0; i < 6; i++)
       {
         printf("name: %s \n", array[i]);
     
       }
        
       return 0;
    }
    name: stuck
    name: enk
    name: andy
    name: ratch
    name: cemp
    name: harry
    Last edited by vead; 01-24-2018 at 12:21 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. storing strings into array
    By gamers18 in forum C Programming
    Replies: 9
    Last Post: 03-27-2013, 10:42 AM
  2. Storing Multiple Strings
    By skmightymouse in forum C Programming
    Replies: 2
    Last Post: 05-03-2012, 08:49 PM
  3. Storing several strings in an char array?
    By Mexicouger in forum C Programming
    Replies: 5
    Last Post: 04-04-2011, 03:07 PM
  4. Storing multiple string in array
    By winsonlee in forum C Programming
    Replies: 2
    Last Post: 03-15-2004, 07:49 AM
  5. storing strings into a 2d array
    By ronin in forum C Programming
    Replies: 2
    Last Post: 10-24-2002, 09:36 PM

Tags for this Thread