Solved

reading whitespace in c

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need example 5 137
Problem to show menu 5 107
What is atomic operation? 6 74
Extract specific Urls by tag from a url list using regex & vba in excel 2013 4 30
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
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 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 create, access, and change arrays in the C programming language.

732 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