0

I want to read in an arbitrary number of strings, one at a time, using <stdio.h> in C. I know that you can do this with integers using:

while (scanf("%d ", &integer))

but I cannot do:

while (scanf("%s", string))

How can I implement the above?

The input is on separate lines.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 2
    Don't ever use `scanf("%s")` - see http://stackoverflow.com/questions/4023895/how-to-read-string-entered-by-user-in-c/4023921#4023921 – paxdiablo May 01 '12 at 05:46
  • You can use scanf if you bound the string size (per the answer you link to). – Eric J. May 01 '12 at 05:47
  • What do you want to do with each string after you've read it? Using `scanf("%s", ...)` is going to give you a new white space separated word on each iteration of the loop, but presumably you're going to do something with the value read before reading the next. That 'something' will dictate how you handle the storage of the next word. – Jonathan Leffler May 01 '12 at 05:49
  • i want to create a binary tree from an input bitstring so scanf("%s"...) then creare binary tree for that string, then move on to the next string untill there are no more strings in the input – Wasiq Kashkari May 01 '12 at 06:06
  • okay maybe i asked that question badly – Wasiq Kashkari May 01 '12 at 06:10
  • Don't test `scanf()` only for `false` (0) or `true`. `scanf()` will return `EOF` in case of failure by exhausting the input which your test won't catch. – pmg May 01 '12 at 19:39
  • 1
    Possible duplicate of [How can I read an input string of unknown length?](https://stackoverflow.com/q/16870485/608639) – jww Dec 08 '18 at 07:00

4 Answers4

3

You usually want to use fgets to read input as strings, especially when you want one line of input to end up as one string.

You can also use fscanf with a scanset conversion to read a line at a time, such as:

char line[100], newline;

fscanf("%99[^\n]%c", line, &newline);

Then you can check whether newline=='\n' to determine whether you've read the entire line successfully, or the line was larger than the buffer you provided.

When you're trying to read line-oriented input, you normally want to avoid "%s" (even with a specified length) though, as this reads white-space delimited tokens, not entire lines.

Adam Rosenfield
  • 390,455
  • 97
  • 512
  • 589
Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
0

Use a char array:

char charArray[100];
while (scanf("%s", &charArray))
juergen d
  • 201,996
  • 37
  • 293
  • 362
0

I guess your problem is to terminate the loop. scanf returns the number of successful scanned elements. In case of a string, also the empty string is successful scanned. Thus, you need another criterion, e.g.

 while(scanf("%s",string)  && (strlen(string)!=0))
Matthias
  • 8,018
  • 2
  • 27
  • 53
0

I did not completely understand what you were trying to do from your original question. When you said you wanted to read in an arbitrary number of strings, I took that to mean, you wanted your program to be able to read 0 to n strings. Unfortunately in C, you will either have to cap off the maximum number of strings you would ever want to read in like #define MAX_NUMBER_OF_STRINGS_TO_READ 25, or get into some sophisticated memory allocation scheme to read a string in and then add it to dynamic memory (returned from malloc).

I took the cap the maximum number of strings approach and wrote the following snippet:

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

char charArray[5][25] = {0};

int main(int argc, char *argv[])
{
    int in_idx = 0;
    int out_idx = 0;

    printf("\n\n%s\n", "Enter no more than 5 strings, no more than 25 characters long.");

    while(fgets (charArray[in_idx], 25, stdin))
    {
        if('\n' == charArray[in_idx][0])
        {
            printf("%s\n", "Entry terminated with newline.");
            break;
        }

        in_idx++;
    }

    for(out_idx=0; out_idx < (in_idx + 1); out_idx++)
    {
        printf("%s", charArray[out_idx]);
    }

    printf("\n%s\n", "Program ended.");

    return 0;
}

I made the termination character a newline. If I only want two strings, I press Enter when I've entered the second string. I terminated fgets by looking for a '\n' in the first position of the character array.

octopusgrabbus
  • 10,555
  • 15
  • 68
  • 131