How can I read and store strings in C from stdin until a blank line is entered?

In summary, to read a list of strings from stdin and store them, you can use scanf("%s", buffer), or strcpy(strings[string_index], buffer).
  • #1
dwx1
6
0
I have to read a list of strings in from stdin and store them, stopping when a blank line is entered. Here's what I tried...
Code:
        const int LINE_CAP = 100;
        const int MAX_LENGTH = 80;

        char *strings[LINE_CAP];
        int strings_index = 0;
        char buffer[MAX_LENGTH];

        printf("%s", "Enter words:\n");
        scanf("%s", buffer);
        while(buffer != "")
        {
                        strings[strings_index] = buffer;
                        strings_index++;
                        scanf("%s", buffer);
        }

No luck. Does anyone know how to do it?
Thanks.
 
Technology news on Phys.org
  • #2
is that the proper way to declare an array of strings? if it is then "" may not be the correct termination for a blank.
 
  • #3
There's quite a few problems with this code.

First of all, you can't do things like buffer != "" in C. To compare strings you'll need to use the strcmp function. Doing buffer != "" just compares two pointers, which is not at all what you want.

Second, when you do strings[string_index] = buffer, it does not copy the contents of buffer into strings[string_index]. It merely sets strings[string_index] to point to the same location in memory as buffer. That's certainly not what you want to do. Buffer gets overwritten every pass through the loop.

Third, there's no guarantee that the lines you read in will fit into buffer. You need to use a formatting option like %79s in scanf to ensure that it doesn't try to read data in past the end of the buffer (and you'll have to make sure that the last character in the string is a NUL, or else functions like strcpy and strcmp and printf will not work). You may want to consider using fgets instead. In fact, since you want to read in lines, you may want to use fgets anyway; scanf will stop when it hits whitespace, not when it hits the end of a line.
 
  • #4
Ok, I thought I got what you were saying, but I seem to be confused...

So I tried using while(strcmp(buffer, "")), but that doesn't stop it when I
enter a blank line.

And I tried doing *strings[strings_index] = buffer, that didn't work either.

I'm kind of lost again.
 
Last edited:
  • #5
dwx1 said:
Ok, I thought I got what you were saying, but I seem to be confused...
So I tried using while(strcmp(buffer, "")), but that doesn't stop it when I
enter a blank line.
And I tried doing *strings[strings_index] = buffer, that didn't work either.
I'm kind of lost again.

scanf discards whitespace. If you enter a blank line it skips over it and keeps waiting for you to enter data.

To copy buffer into strings[strings_index] you can use strcpy, as in strcpy(strings[string_index], buffer). But that won't work unless you allocate memory for the string first. strings[string_index] is just a pointer, and since you don't initialize it, it could be pointing anywhere. Either use malloc() to allocate memory for it first, or declare strings using "char strings[LINE_CAP][MAX_LENGTH];" instead.
 
  • #6
Ok, I got it working! Thanks so much!
 

What is a string in C?

A string in C is a sequence of characters stored in consecutive memory locations. It is represented by an array of characters and is terminated by a null character ('\0').

How do you read a string in C?

To read a string in C, you can use the scanf() function or the gets() function. The scanf() function reads characters until it encounters a space or a newline character, while the gets() function reads characters until it encounters a newline character.

What is the difference between scanf() and gets() when reading strings in C?

The main difference between scanf() and gets() is that scanf() reads until it encounters a space or a newline character, while gets() reads until it encounters a newline character. This means that scanf() can only read one word at a time, while gets() can read entire sentences or phrases.

How do you handle spaces when reading a string in C?

To handle spaces when reading a string in C, you can use the fgets() function. This function reads a specified number of characters from the input, including spaces, and stores them in a string.

What happens if the input string is longer than the defined array size?

If the input string is longer than the defined array size, it will overflow and cause a buffer overflow error. This can lead to unexpected behavior and potential security vulnerabilities. To avoid this, it is important to ensure that the input string does not exceed the size of the defined array.

Similar threads

  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
Replies
10
Views
958
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
2
Replies
36
Views
3K
  • Programming and Computer Science
Replies
4
Views
5K
  • Programming and Computer Science
Replies
1
Views
24K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
2
Views
2K
Back
Top