LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Make multiple blank lines to a single lines (http://www.linuxquestions.org/questions/programming-9/make-multiple-blank-lines-to-a-single-lines-949444/)

kimhj3715 06-09-2012 08:57 PM

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)
{
        int c, scount = 0, ecount = 0;
        int prec;
       
        while((c = getchar()) != EOF) 
        {
                switch(c)
                {
                        case '\t':
                                c = ' ';
                                break;
                        case ' ':
                                scount++;
                                break;
       
                        case '\n':
                                ecount++;
                                break;
                        default:
                                scount = 0;
                                ecount = 0;
                }
                if(ecount >= 2)
                        c = '\0';  // not sure
                if(scount >= 2)
                        c = '\0';
               
                               
               
               
                putchar(c);
               
        }
        return 0;
}

I use same method as double spaces or more.. but..
failed.. it still doesnt work..

should i use \n or \r??

kike_coello 06-10-2012 12:11 AM

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.

kike_coello 06-10-2012 12:12 AM

Let me try this.

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;
}



---------- Post added 06-10-12 at 05:13 AM ----------

That's better.

kimhj3715 06-10-2012 01:15 AM

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?

kike_coello 06-10-2012 02:01 AM

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

Nominal Animal 06-10-2012 10:35 AM

I suggest using an inner loop to handle the special characters. In C99:
Code:

#include <stdio.h>
#include <ctype.h>

int main(void)
{
    int c;

    c = getchar();

    while (isspace(c))
        c = getchar();

    while (c != EOF) {
        if (isspace(c)) {
            int newline = 0;

            while (isspace(c)) {
                if (c == '\n' || c == '\r')
                    newline = 1;

                c = getchar();
            }

            if (newline)
                putchar('\n');
            else
                putchar(' ');

        } else {
            putchar(c);
            c = getchar();

            if (c == EOF)
                putchar('\n');
        }
    }

    return 0;
}

If you save it as filter.c, you can compile it using
Code:

gcc -std=c99 filter.c -Wall -O3 -fomit-frame-pointer -o filter.exe
This code does not have the exact same functionality, though. This code
  • Uses the locale convention for whitespace
    In the default POSIX/C locale: spaces, tabs (\t and \v), form feeds (\f), linefeeds (\n) and carriage returns (\r)
  • Skips all initial whitespace in the file
  • Removes all leading and trailing whitespace on each line
  • Combines any sequence of whitespace (excluding newlines) into single space
  • Converts all newline conventions to the default one (\n)
    (Note that in Windows, if the standard input is open in text mode, it may be converted to CR LF, i.e. \r\n. I don't use Windows, so I'm not sure.)
  • Makes sure the file ends with a single newline

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 05:37 AM.