how to read space separated words in c
To read space-separated words in C, you can use the scanf
function with the %s
format specifier. Here's an example:
#include <stdio.h>
int main() {
char word[100];
printf("Enter a sentence: ");
scanf("%s", word);
printf("The first word is: %s\n", word);
return 0;
}
In this example, the scanf
function reads a word from the user input and stores it in the word
array. The %s
format specifier tells scanf
to read a string until it encounters a whitespace character. The entered word is then printed using printf
.
Note that this example only reads the first word of the sentence. If you want to read multiple words, you can use a loop and scanf
inside the loop to read each word separately.
I hope this helps! Let me know if you have any further questions.