Thread: accessing user entered array elements by index?

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    100

    accessing user entered array elements by index?

    Hi,
    If you initialize an array of strings like:
    Code:
    char string[4][10] = {"monday", "tuesday", "wednsday", "thursday"}
    Each element can be accessed by index. But is there a way to make an array that has its contents entered by the user accessable by index, for instance, if the user entered a sentence, could each word be accessed by index? like:
    Code:
    char string[21];
    
    gets(string);
    
    "today is thursday"
      0     1     2
    
    printf("%s" , string[2]);
    Any help would be appreciated.

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Code:
    	char string[3][10];
    
    	gets(string[0]);
    	gets(string[1]);
    	gets(string[2]);
    
    	printf("%s\n%s\n%s", string[0], string[1], string[2]);
    Also, gets is bad.

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    100
    so each word would have to be entered by a separate prompt?

  4. #4
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Or a loop.

    Code:
    for(i = 0; i < 3; i++)
    {
            scanf("%s", string[i]);
    }

  5. #5
    Registered User
    Join Date
    Jan 2006
    Posts
    100
    I see. But there is no way you can enter the sentence at once and be able to access each word as an individual element?

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Sure. Read it all into one buffer and then split it up, copying it to the appropriate array.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    This is crappy.
    Code:
    gets(string[0]);
    This is one notch better than crappy, but still crappy.
    Code:
    scanf("%s", string[i]);
    There is an FAQ.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    Registered User
    Join Date
    Jan 2006
    Posts
    100
    ahh yes,
    I have experienced the problem with gets() already. The program crashes with a windows error if the input is to large.

  9. #9
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Quote Originally Posted by Dave_Sinkula
    This is crappy.
    Code:
    gets(string[0]);
    This is one notch better than crappy, but still crappy.
    Code:
    scanf("%s", string[i]);
    There is an FAQ.
    I'd have thought scanf("%s", foo) would be on the same crappiness level as gets(foo) since they're both crap for the same reason.

  10. #10
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    fgets() -- non-crappy version
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Everything becomes clear if you read the FAQ . . .
    Why not to use gets(): http://faq.cprogramming.com/cgi-bin/...&id=1043284351
    How to use fgets(): http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Shifting elements in an Array
    By mmarab in forum C Programming
    Replies: 5
    Last Post: 12-10-2007, 12:11 PM
  2. Sorting: Getting permutation index array
    By flyvholm in forum C Programming
    Replies: 2
    Last Post: 09-20-2006, 07:07 PM
  3. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  4. Template Array Class
    By hpy_gilmore8 in forum C++ Programming
    Replies: 15
    Last Post: 04-11-2004, 11:15 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM