Solved

reading whitespace in c

Posted on 2007-03-23
3
1,827 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 45

Accepted Solution

by:
Kent Olsen 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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

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…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

617 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