getchar and putchar

Please place delete button nex to the post
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include<stdio.h>
int main()
{

 int c;
bool lastCharacterWasAWhitespace = true;
 while ((c = getchar()) != EOF) 
   {
       bool currentCharacterShouldBeTranslatedToAWhitespace = c==' ' || c=='\t' || c=='\n' || c=='.' || c==',' || c==';' ;
       if(! currentCharacterShouldBeTranslatedToAWhitespace)
       {
	   putchar(c);
           lastCharacterWasAWhitespace = false;
       }
       else if (! lastCharacterWasAWhitespace)
       {
           putchar('\n');
           lastCharacterWasAWhitespace = true;
       }
       // else print nothing since we have subsequent whitespaces!
   }


Of course you can accomplish this also with regular expressions.
Maybe you want to inform yourself: http://www.boost.org/doc/libs/1_46_0/libs/regex/doc/html/index.html
I erase the post.
Last edited on
Line 19 is redundant, because flag == 1 anyway.
The case in line 25-27 should be "take no action", not ignore the next character. So, basically, you just need to negate/invert the condition in line 24 and use the else branch as the body of the new if.

Regards
Topic archived. No new replies allowed.