LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-09-2012, 08:57 PM   #1
kimhj3715
LQ Newbie
 
Registered: Jun 2012
Posts: 2

Rep: Reputation: Disabled
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??

Last edited by kimhj3715; 06-09-2012 at 09:01 PM.
 
Old 06-10-2012, 12:11 AM   #2
kike_coello
Member
 
Registered: Jul 2005
Location: maryland
Distribution: Ubuntu 9.04
Posts: 88

Rep: Reputation: 17
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.
 
Old 06-10-2012, 12:12 AM   #3
kike_coello
Member
 
Registered: Jul 2005
Location: maryland
Distribution: Ubuntu 9.04
Posts: 88

Rep: Reputation: 17
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.
 
1 members found this post helpful.
Old 06-10-2012, 01:15 AM   #4
kimhj3715
LQ Newbie
 
Registered: Jun 2012
Posts: 2

Original Poster
Rep: Reputation: Disabled
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?
 
Old 06-10-2012, 02:01 AM   #5
kike_coello
Member
 
Registered: Jul 2005
Location: maryland
Distribution: Ubuntu 9.04
Posts: 88

Rep: Reputation: 17
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
 
Old 06-10-2012, 10:35 AM   #6
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
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.)
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Split single line into multiple lines with 3 column each udiubu Programming 7 11-26-2017 09:41 AM
[SOLVED] Perl: how to replace blank lines in a file with given lines from another karamaz0v Programming 8 04-19-2012 06:48 AM
[SOLVED] CAT command | multiple lines to multiple lines udiubu Programming 11 10-28-2011 06:09 AM
how to create a single line of output from multiple variable lines of input steven.c.banks Linux - General 2 02-03-2010 03:09 PM
merge multiple lines of a single file into one line groverrajiv Linux - Newbie 4 05-26-2004 02:38 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:58 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration