![]() |
Make multiple blank lines to a single lines
Hi, I'm tryin to make tab to space, multiple or more space to a space, and make multiple blank lines to a single lines.
I couldn't figure it out the last one... Code:
int main(void) failed.. it still doesnt work.. should i use \n or \r?? |
Hi, I modified your program a little bit, and it works fine except for one problem that I can't figure out. If there is more than a single newline at the beginning of the input it will only change it to a single line or if there is one line, it'll leave it as it is. I can't figure it out and I'm going to sleep but if nobody has replied by tomorrow I might give it another go. Here's what I've got (changed some of your variables, sorry :P).
[code] #include <iostream> #include <cstdio> using namespace std; int main(void) { int c; int space_count = 0; int newline_count = 0; int tab_count = 0; while((c = getchar()) != EOF) { switch(c) { case '\t': tab_count++; break; case ' ': space_count++; break; case '\n': newline_count++; break; default: space_count = 0; newline_count = 0; tab_count = 0; } if(newline_count >= 2) c = '\0'; if(space_count >= 2) c = '\0'; if(tab_count >= 2) c = '\0'; putchar(c); } return 0; } [\code] ---------- Post added 06-10-12 at 05:12 AM ---------- Oops, sorry, I don't know how to display code. Run it through "indent" to get the code aligned. |
Let me try this.
Code:
#include <iostream>---------- Post added 06-10-12 at 05:13 AM ---------- That's better. |
fatal error: iostream: No such file or directory
compilation terminated. also cstdio what is this? how can i fix it? i'm using cygwin gcc compiler on window do i need to update some files? |
You need iostream and cstdio for the functions to work. You need to include those header files, it doesn't matter if you are using windows because I used g++ in Linux. Don't use gcc, that's for compiling C files, use g++.
Enrique |
I suggest using an inner loop to handle the special characters. In C99:
Code:
#include <stdio.h>Code:
gcc -std=c99 filter.c -Wall -O3 -fomit-frame-pointer -o filter.exe
The main difference with the other ones in this thread is that in this one, c is read from input during the iteration (loop body), not at the start of the iteration. Because of this, we can use inner loops to read further input. (If you always read the next character at the start of the iteration, the inner loops would need to use ungetc() to push back the character that ended the inner loop; making for complex and confusing code.) |
| All times are GMT -5. The time now is 10:38 PM. |