Removing multiple blanks using putchar and getchar in C

2.7k views Asked by At

Problem: Write a program that receives textual input using getchar() and outputs the string, having removed multiples blanks.

Here's how I wrote the pseudo-code:

While each input character is received before reaching EOF, do the following:
     1) if character is non-blank, print it out
     2) otherwise:
         a. print out the blank
         b. do nothing untill the next non-blank character 
     3) if a non-blank character is reached, go back to 1)

I tried to implement the algorithm as such:

#include <stdio.h>
/* replaces multiple blanks with a single blank */
main(){
    char c;
    while((c= getchar())!=EOF){
        if (c != ' ')
            putchar(c);
        else {
            putchar(c);
            while(c == ' ')
                ;
        }
    }   
}

When a string contains blanks, it stalls. I am not sure how I should debug it. I think the problem is with my second while, and the program gets into an infinite loop there rather than waiting for the new characters.

5

There are 5 answers

2
Anonymous On BEST ANSWER
#include <stdio.h>
/* replaces multiple blanks with a single blank */
main(){
    int c; // thanx chux
    while((c= getchar())!=EOF){
        if (c != ' ')
            putchar(c);
        else {
            putchar(c);
            while((c= getchar())!=EOF)
                if (c!=' ')
                {
                    putchar(c);
                    break;
                }
        }
    }   
}

Your last while didnt read chars from stdin, causing infinie loop comparing last red character from previous getchar().

1
hobbs On

Anonymous's answer works, but there is a much simpler algorithm that also works:

While there is input remaining:
    Read a character.
    If the current and previous characters aren't both blank:
        Print the current character.

In C:

#include <stdio.h>

int main() {
    int prev = EOF, c;
    while ((c = getchar()) != EOF) {
        if (c != ' ' || prev != ' ')
            putchar(c);
        prev = c;
    }
    return 0;
}
0
BLUEPIXY On
#include <stdio.h>

int main(void){
    int c;
    while((c = getchar())!=EOF){
        if (c != ' ')
            putchar(c);
        else {
            putchar(c);
            while((c = getchar()) == ' ')
                ;
            ungetc(c, stdin);//go back 1
        }
    }
    return 0;
}
0
ARMAND MARTINIUC On

This worked for me.

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int c;
    int space = 0;

    while ((c = getchar())!= EOF){

            if (c != ' '){
                putchar(c);
            }else{
                if(space == ' '){
                    continue;
                }else{
                    putchar(c);
                }
            }
            space = c;
    }
    return 0;
}
0
chqrlie On

Your program has a few issues:

  • the prototype for main() must include the return type int.

  • c must be defined as int so you can correctly distinguish EOF from all valid byte values returned by getchar().

  • after you identify a blank character, you must continue reading characters and skip subsequent blanks.

  • technically, blank characters include the space character ' ' and the tab character '\t'. You should use isblank() from <ctype.h> and modify your program to skip subsequent blank characters.

Here is a modified version:

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

/* replaces multiple blanks with a single blank */
int main(void) {
    int c;
    while ((c = getchar()) != EOF) {
        putchar(c);
        if (isblank(c)) {
            while (isblank(c = getchar())
                continue;
            if (c == EOF)
                break;
            putchar(c);
        }
    }
    return 0;
}