0

I'm new to C so some clarification would be very helpful! I'm trying to use a scanner to ask my program for a series of words which I will store..what I have so far is,

     char[]listOfWords[9999]; //creates a large array of characters
     scanf("%s", listOfWords); //stores inputs into listOfWords

with this, I can access the first word easily, but it comes to a matter of accessing the second, and third words..any suggestion? For example, how my input was

 Hello how are you guys

I can access "Hello" no problem, but how would I call "how" and "are"

5
  • 2
    char[]listOfWords[9999]; won't compile. As for the problem, %s reads until it finds a whitespace character. You can call the scanf again and again to read each word. Or simply use scanf("%[^\n]", listOfWords); to read in the whole line. Note that the previous approach will fail if the first character to be read by it is a \n and also, it will leave a newline character in the stdin. Another approach would be to use fgets(listOfWords, sizeof(listOfWords), stdin);. Note that fgets consumes the newline character and stores it in listOfWords. Commented Sep 21, 2015 at 8:30
  • char[]listOfWords[9999]; ......definitely not C... Commented Sep 21, 2015 at 8:31
  • yeah, I used [100] I was just rushing through my methodology my apologies. That is an interesting technique, thank you! My problem is, if I were to use it that way, how would I know blindly the length of every word? Since I believe this will just create a long array of characters like Commented Sep 21, 2015 at 8:39
  • [h,e,l,l,o, ,,h,o,w,...] Commented Sep 21, 2015 at 8:39
  • @user201535 If you were to read the whole line into listOfWords, you need to use strtok with space as the delimiter to get each word. Then, use strlen to find the length of each word. Commented Sep 21, 2015 at 8:50

4 Answers 4

1

Scanf() only accepts input for a single word.(not after space.)

To enter multiple words, you can use gets(string) or scanf("%[^\n]", string).

Sign up to request clarification or add additional context in comments.

4 Comments

gets is dangerous and has been deprecated as of the latest C standard. Use fgets instead.
"Scanf() only accepts input for a single word" -- That's misleading. The format specifier, specifically %s , is responsible for that.
Though fgets is a better option, Still gets is more convenient some times. (At least I feel so. it keeps away from file operations.) Even if you dont want to use it, use scanf option .
@CoolGuy This sentence was accordilg to the example in question not in general.
1

This - char[]listOfWords[9999]; is not a valid syntax to declare array.

You simply need to declare an array -

char s[100];  
fgets(s,100,stdin);  // take input and store it in s

And using strtok or sscanf you can extract separate words in different arrays .

Or you can use a 2-d array -

char s[100][100];

So if string stored in it is - Hello how are you guys

then by s[0] you will get Hello , s[1] will give how and similarly , other words in string.

6 Comments

shouldn't be fgets(s,99,stdin); ?
@Michi Why ? fgets will add null character at the end .
I was asking because if you get 100 letters + '\0' it will be 101. or will be only 99 here fgets(s,100,stdin); ?
@Michi No I don't that will happen . It will stop as 100-1(in general n-1) i.e (99) characters are read . n being size given to it .
@Michi Yes . You can read it here - cplusplus.com/reference/cstdio/fgets
|
1
  • Here is a code what you are looking fore,

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

Comments

0

Is your problem to read multiple words? Or just reading a full line? Also the declaration char[]listOfWords[9999] is wrong. If you all you want to do is read a line, you can try the following:

char buffer[1024];
fgets(buffer, 1024, stdin);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.