top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How sacnf can accept whitespace?

+1 vote
399 views

I am trying to enter a string with white space in the C using %s option but it does is only taking till first white space. Any suggestion how to enter a string with white space in the scanf?

posted Aug 12, 2014 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

+1 vote
#include <stdio.h>

int main (int argc, char const *argv[])
{
    char name[20];
    scanf("%[^\n]s",name);
    printf("%s\n", name);
    return 0;
}
answer Aug 12, 2014 by Balakrishnan S
+1 vote

it is better to use "fgets()" instead of scanf()
but if you really want to use scanf() only, then repley "%s" with "%[^\n]" ie. "scanf("%[^\n]", buff);"
this will take the input until a "\n" (new line) encountered.

But I would suggest go with fgets(), because there is no check for buffer overflow in scanf().

answer Aug 12, 2014 by Arshad Khan
Similar Questions
+2 votes

Write a C program which accept two strings and print characters in second string which are not present in first string?

Example:
String 1: apple
String 2: aeroplane

output:
ron

+2 votes

Hi All,

I want to know, various types of Attach Request for Phone and Wifi devices and what will be the respective attach Accept response from the network in successful Case. I am looking for Network response both in case if network supports VoLTE (Like RJIO)and if network doesnot supports volte (Airtel)....

...