473,775 Members | 3,835 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

scanf to include white spaces

Hi,

I was wondering if there was a way to include the white spaces in a string.

Currently, I am using:
scanf("%s", &input);

However, this doesn't include the 'space' character or any other white
spaces. Is there a way I can include the 'space' character rather than skip
in.

Thanks in advance.
Nov 14 '05 #1
5 36886
Hiho,
I was wondering if there was a way to include the white spaces in a string.

Currently, I am using:
scanf("%s", &input);

However, this doesn't include the 'space' character or any other white
spaces. Is there a way I can include the 'space' character rather than skip
in.
Yep. Use a list of permitted/prohibited characters.
Example:
cat scanblank.c #include <stdio.h>

int main(void)
{
char string[80];

puts("first test. Enter string: ");
scanf("%[^\t\n]",string);
puts(string);
while(getchar() !=(int)'\n');

puts("\nsecond test. Enter string: ");
scanf("%[ a-zA-Z0-9+*/-]",string);
puts(string);

return 0;
}
./scanblank first test. Enter string:
hello, world !
hello, world

second test. Enter string:
abcde j, fghk
abcde j


In the first test, we scan for everything with exception of '\t' and
'\n'. Between d and !, I entered a tabulator. In the second test, we
scan for
a,b,c,..,z,A,B, C,..,Z,0,1,2,.. ,9,+,*,/,-
You always read at least one of the permitted/non-prohibited characters.
The hyphen inbetween marks a range rather than meaning the hyphen
character. The list must be nonempty.
To scan (or scan not) for ], you have to put it immediately after [ or
[^, respectively.

Remark: I am not checking the return values of puts and scanf.
Especially the latter might be necessary for you.
Cheers
Michael

Nov 14 '05 #2
Jonathan Ng <jo****@hotmail .com> wrote:
I was wondering if there was a way to include the white spaces in a string. Currently, I am using:
scanf("%s", &input);
This looks rather strange. When you have a '%s' format specifier the
variable must be an array pointer. And if 'input' is one than you
would need

scanf( "%s", input );

And I have problems to imagine a situation where 'input' could be
anything else than an array.
However, this doesn't include the 'space' character or any other white
spaces. Is there a way I can include the 'space' character rather than skip
in.


No, scanf() stops at white space. And I would guess that you would be
a lot better of using fgets() here. First of all, when using scanf()
you have no way to check that the string the user enters isn't longer
than there is place in your 'input' array - if that happens scanf()
will happily write beyond the end of the array with impossible to
forsee consequences (including everything to seem to work nicely).
And then you get a whole line of input (or as many characters as fit
into the buffer) and you than can split it up however you need and
aren't handicapped by the limitations of scanf().

Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@p hysik.fu-berlin.de
\______________ ____________ http://www.toerring.de
Nov 14 '05 #3
Jonathan Ng wrote on 12/08/04 :
Hi,

I was wondering if there was a way to include the white spaces in a string.

Currently, I am using:
scanf("%s", &input);
This '&' is suspicious. How is 'input' defined?
However, this doesn't include the 'space' character or any other white
spaces. Is there a way I can include the 'space' character rather than skip
in.


For many reasons (including your issue), it's better to get a line from
the user with fgets(). BTW, it's a FAQ.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html

"C is a sharp tool"

Nov 14 '05 #4
Jonathan Ng wrote:

Hi,

I was wondering if there was a way to include the white spaces in a string.

Currently, I am using:
scanf("%s", &input);

However, this doesn't include the 'space' character or any other white
spaces.
Is there a way I can include the 'space' character rather than skip
in.


/* You can use Pop's Device. */

#include <stdio.h>
#include <stdlib.h>

#define LENGTH 65
#define str(x) # x
#define xstr(x) str(x)

int main(void)
{
int rc;
char array[LENGTH + 1] = "";

fputs("Enter a string with spaces:", stdout);
fflush(stdout);
rc = scanf("%" xstr(LENGTH) "[^\n]%*[^\n]", array);
if (!feof(stdin)) {
getchar();
}
while (rc == 1) {
printf("Your string is: \n%s\n", array);
fputs("Hit Enter key to end, or enter "
"a string with spaces:", stdout);
fflush(stdout);
rc = scanf("%" xstr(LENGTH) "[^\n]%*[^\n]", array);
if (!feof(stdin)) {
getchar();
}
}
return 0;
}

--
pete
Nov 14 '05 #5
In <mn************ ***********@YOU RBRAnoos.fr> Emmanuel Delahaye <em***@YOURBRAn oos.fr> writes:
Jonathan Ng wrote on 12/08/04 :
Hi,

I was wondering if there was a way to include the white spaces in a string.

Currently, I am using:
scanf("%s", &input);


This '&' is suspicious. How is 'input' defined?
However, this doesn't include the 'space' character or any other white
spaces. Is there a way I can include the 'space' character rather than skip
in.


For many reasons (including your issue), it's better to get a line from
the user with fgets(). BTW, it's a FAQ.


Pray tell, why is fgets() better than scanf for the OP's issue?

As a matter of fact and excepting gets(), fgets() is *always* the
worst solution, when bullet proof behaviour is desired/need.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
9875
by: B Thomas | last post by:
Hi, I was reading O'Reilly's "Practical C programming" book and it warns against the use of scanf, suggesting to avoid using it completely . Instead it recomends to use using fgets and sscanf. However no explanation is offered other than that scanf handels end of lines very badly. I have exeperienced such problems when doing some numerical programming but never understood it. Things like some consequitive scanfs would not read in values...
2
2897
by: Stu | last post by:
I have the following "C" program, which works fine. #include <stdio.h> #include <stdlib.h> #include <strings.h> int main() { char *buffer = "1234 - 5678";
7
13265
by: gyan | last post by:
I want to read a line with white spaces though scanf. So i used: scanf("%",string); above is working in one program, but in other..what may be the reason?
33
3168
by: Lalatendu Das | last post by:
Dear friends, I am getting a problem in the code while interacting with a nested Do-while loop It is skipping a scanf () function which it should not. I have written the whole code below. Please help me in finding why such thing is happening and what the remedy to it is. Kindly bear with my English. int main ()
2
5238
by: Bernard Liang | last post by:
In response, I have another question about the scanf family. After reading in a %d value, for instance, do they immediately wade through all subsequent whitespace until a non-whitespace character is reached, and then continue analysing the format string? For instance, suppose I have a buffer int num; char chr1, chr2, chr3, chr4; char my_string = "1 1 2 3 5 8abc";
14
13814
by: main() | last post by:
I know this is the problem that most newbies get into. #include<stdio.h> int main(void) { char a; scanf("%c",&a); /*1st scanf */ printf("%c\n",a); scanf("%c",&a); /*2nd scanf*/ printf("%c\n",a);
3
12801
by: stathisgotsis | last post by:
Hello everyone, Trusting K&R2 i thought until recently that spaces are ignored in scanf's format string. Reading arguments to the contrary confused me a little. So i now ask: Is scanf("%d%d",...) different from scanf("%d %d",...) in the Standard's point of view? Thank you.
68
4665
by: stasgold | last post by:
Hello. I maybe reinvent the weel ... I'm trying to read positive integer number with the help of scanf, if the input value is not positive number but negaive one zero or char , i have to reread the input until I get the needed pos. number I wrote the code , but it doesn't work the way it should : if i put some char into input, the program goes infinite loop instead of promting me to enter a new value.
3
9842
by: Tinku | last post by:
#include<stdio.h> main() { char line; scanf("%", line); printf("%s", line); } it will read and print the line but what is "%" in general we gives %s, %c .
0
9622
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9455
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10272
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8941
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7464
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6719
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5487
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4020
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2855
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.