-1

How can I introduce multiple strings in input and output with scanf()? Is it possible to do that without using 100 notations (e.g. a, b, c, d, e -> "%c %c %c", a, b, c etc.)? I type this:

#include <stdio.h> 
int main()
{
    char user_str[20];
    scanf("%s", user_str);
    printf("%s", user_str);
    return 0;
}

Which works for the first word as input and output, but if I want to introduce 100 propositions with space in the text, how can I type the code to do that in the easiest way possible?

user438383
  • 5,716
  • 8
  • 28
  • 43
  • 1
    Have you learned about loops and arrays already? If yes, could you use them to do what you try, just for integers? If you demonstrate that it would focus your questions on the tricky details of applying it to "strings". Otherwise it is topics to read up on. – Yunnosch Oct 10 '21 at 15:11
  • Are you sure that you want "multiple strings"? Or do you maybe only want "multiple words"? Do you maybe want a whole line of text as one single string? In that case, you can simply use [`fgets`](https://en.cppreference.com/w/c/io/fgets). – Andreas Wenzel Oct 10 '21 at 16:14

2 Answers2

2

As @Yunnosch notes, your best best is to use loops and arrays. Consider a simple situation where you want to read in 4 strings that are up to 19 characters in length. We have length 20 to leave space for a terminating null character.

#include <stdio.h>

int main() {
    char input[4][20];

    for (int i = 0; i < 4; i++) {
        scanf("%19s", input[i]);
    }

    for (int i = 0; i < 4; i++) {
        printf("%s\n", input[i]);
    }
}

This is readily scalable to 100s of strings. If you use enough memory, you may wish to use malloc to allocate it, but the same basic pattern would continue.

Chris
  • 26,361
  • 5
  • 21
  • 42
1

Here is a solution which reads one line of input using scanf:

#include <stdio.h> 
int main()
{
    char user_str[20];
    scanf("%[^\n]s", user_str);
    printf("%s", user_str);
    return 0;
}

Using %[^\n]s instead of %s will read all characters until you reach \n (or EOF ) and write them into user_str. But this solution is worse than gets(), so try to use fgets() instead.

#include <stdio.h>

int main()
{
    char user_str[100];
    printf("Enter your name: ");
    fgets(user_str, 100, stdin); 
    printf("Your Name is: %s", user_str);
    return 0;
}
Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39
vegan_meat
  • 878
  • 4
  • 10
  • 1
    Although OP specifically asked to input "multiple strings", it is unclear whether this is an [XY problem](https://xyproblem.info/) and whether OP actually wants "multiple words", so I am upvoting the answer. – Andreas Wenzel Oct 10 '21 at 16:22
  • I mean multiple words in the same string. Thank you very much, your answer fits me and saves me long time for searching (time which i don.t have at the moment). Up if i could!!! – DacaDa Apasati-TastAunu Oct 10 '21 at 16:58
  • @Daca: Normally, you should edit the question in order to clarify it. However, you cannot do that now anymore, as that would invalidate one of the other answers. – Andreas Wenzel Oct 10 '21 at 18:46