Solved

reading whitespace in c

Posted on 2007-03-23
3
1,818 Views
Last Modified: 2011-09-20
Hi,

I remember in C++, you can do a  cin>>ws to read the white space,
can I do the similar thing in C? Thanks!
0
Comment
Question by:yarock
3 Comments
 
LVL 45

Accepted Solution

by:
Kdo earned 500 total points
ID: 18784259

You can certainly do something similar, but it's not quite as elegant.

  char ch;

  while (isspace (ch = getchar ()));
  unget (ch);


And if you're reading an entire line at a time, sscanf() can be controlled to skip whitespace as you parse the entire line.


Kent
0
 
LVL 3

Expert Comment

by:srinims
ID: 18797875

getchar will recieve all keyboard events, untill newline character or eof '\0' character occurs.

char ch;
while(ch=getchar())
{
      printf("%X",ch);
}

will print the hexadecimal equivalent of the key.. will print 20 for space...
0
 
LVL 2

Expert Comment

by:Hurb
ID: 18803751
hi,

char ch;

ch=getchar();           // will store any char include on alphabetic like '\0'
scanf("%c",&ch);    // same as above
scanf(" %c",&ch);  // note space before %c; this will skip any non alpha char including spaces ie. will just store aplhabetic chars
0

Featured Post

Make managing Office 365 email signatures a breeze

Are you using Office 365? Having trouble trying to set up email signatures for your users? Getting stressed out managing multiple signatures? Need an easier way to manage? We have a solution for you, try the most-user friendly and powerful signature management tool on the market.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now