You want to input a string with spaces?

When you want to read a string that may contain spaces from the console in C, using scanf alone may not be sufficient because scanf stops reading when it encounters a space character. Here are a few alternative approaches to read a string with spaces:

1. Using fgets:

#include <stdio.h>

int main() {
    char buffer[100];  // Adjust the size according to your needs

    printf("Enter a string with spaces: ");
    fgets(buffer, sizeof(buffer), stdin);

    // Remove trailing newline character if present
    int length = strlen(buffer);
    if (length > 0 && buffer[length - 1] == '\n') {
        buffer[length - 1] = '\0';
    }

    printf("You entered: %s\n", buffer);

    return 0;
}

fgets reads a line from the specified stream (in this case, stdin) and stores it in the provided buffer.

2. Using scanf with the %[^\n] format specifier:

#include <stdio.h>

int main() {
    char buffer[100];  // Adjust the size according to your needs

    printf("Enter a string with spaces: ");
    scanf(" %[^\n]", buffer);

    printf("You entered: %s\n", buffer);

    return 0;
}

The %[^\n] format specifier in scanf tells it to read characters until a newline is encountered.

3. Using getchar in a loop:

#include <stdio.h>

int main() {
    char buffer[100];  // Adjust the size according to your needs

    printf("Enter a string with spaces: ");

    int i = 0;
    int c;
    while ((c = getchar()) != '\n' && i < sizeof(buffer) - 1) {
        buffer[i++] = c;
    }
    buffer[i] = '\0';

    printf("You entered: %s\n", buffer);

    return 0;
}

This approach reads characters one by one until a newline is encountered.

Choose the method that best fits your requirements and coding style. Adjust buffer sizes as needed for your specific use case.

0
Subscribe to my newsletter

Read articles from Jyotiprakash Mishra directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Jyotiprakash Mishra
Jyotiprakash Mishra

I am Jyotiprakash, a deeply driven computer systems engineer, software developer, teacher, and philosopher. With a decade of professional experience, I have contributed to various cutting-edge software products in network security, mobile apps, and healthcare software at renowned companies like Oracle, Yahoo, and Epic. My academic journey has taken me to prestigious institutions such as the University of Wisconsin-Madison and BITS Pilani in India, where I consistently ranked among the top of my class. At my core, I am a computer enthusiast with a profound interest in understanding the intricacies of computer programming. My skills are not limited to application programming in Java; I have also delved deeply into computer hardware, learning about various architectures, low-level assembly programming, Linux kernel implementation, and writing device drivers. The contributions of Linus Torvalds, Ken Thompson, and Dennis Ritchie—who revolutionized the computer industry—inspire me. I believe that real contributions to computer science are made by mastering all levels of abstraction and understanding systems inside out. In addition to my professional pursuits, I am passionate about teaching and sharing knowledge. I have spent two years as a teaching assistant at UW Madison, where I taught complex concepts in operating systems, computer graphics, and data structures to both graduate and undergraduate students. Currently, I am an assistant professor at KIIT, Bhubaneswar, where I continue to teach computer science to undergraduate and graduate students. I am also working on writing a few free books on systems programming, as I believe in freely sharing knowledge to empower others.