Solved

reading whitespace in c

Posted on 2007-03-23
3
1,816 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
How do I test for current date? 9 90
port mapped I/o query 10 140
mixing C++ and C code elegantly 10 150
why "." vs "->" 23 116
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

746 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

13 Experts available now in Live!

Get 1:1 Help Now