473,382 Members | 1,689 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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 36846
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***********@physik.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***********************@YOURBRAnoos.fr> Emmanuel Delahaye <em***@YOURBRAnoos.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
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....
2
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
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
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...
2
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...
14
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*/...
3
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...
68
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...
3
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 .
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.