Search⌘ K
AI Features

Multi-Word Strings

Explore how to handle multi-word strings in C programming by learning to use fgets and scanf functions effectively. Understand the advantages of each method and how to display strings using puts for clear output.

Input a multi-word string using scanf()

If a multi-word string is supplied to scanf(), only the first word gets stored in the array, str3.

#include<stdio.h>

int main(){
    char str3[15];
    printf("Enter name & surname\n");
    // Get multiword string using %s specifier
    scanf("%s", str3);
    printf("%s\n", str3);
}

To overcome this, we need to use the format specifier, “%[^\n]s”. This means that the program will receive all the words in the string, from the beginning up to a \n ...