-1

This was already asked, but I made my own program and I don't know why it doesn't work.

int c;
char blank;


while ((c = getchar()) != EOF) {
    if (c == ' ') {
        putchar(c);
        while ((c = getchar()) == ' ') {
            putchar('');
        }
    }
    putchar(c);
}

Basically, replace space with nothing is what I did. But it doesn't work. If I put '1' instead of '' it replaces spaces with 1s so I don't know what's the problem

Krishna Kanth Yenumula
  • 2,533
  • 2
  • 14
  • 26
  • 8
    what do you expect `putchar('')` to do? `''` is not a valid character constant. If you don't want to output anything, simply do nothing. – Chris Dodd Dec 29 '20 at 00:11
  • 3
    If you don't want to output the character, don't call `putchar()` at all. – Dmitri Dec 29 '20 at 00:12
  • 2
    Additional problem: The second `getchar` in the loop can also return `EOF`, so you might have to skip the `putchar` after the conditional `if` block. – Bodo Dec 29 '20 at 00:15
  • Fun fact: the whole program can be reduced to a one-liner, empty `for` loop. `for(char s[99]; scanf(" %s", s) == 1; printf("%s ", s));` – anotherOne Dec 29 '20 at 01:17
  • Nice solution @davide though this will also strip newlines from the input – costaparas Dec 29 '20 at 01:35
  • @Davide `scanf(" %s", s)` is worse than [`gets()`](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used). Suggested [one liner](https://stackoverflow.com/questions/65485690/replace-each-string-of-one-or-more-blanks-with-a-signle-blank#comment115776887_65485690) has differing output when input is `"abc"`.as it appends a space. – chux - Reinstate Monica Dec 29 '20 at 01:37
  • You are right, it's not an equivalent program. But it was fun. – anotherOne Dec 29 '20 at 12:28

2 Answers2

2

The specific error in your code is in the way you have used the putchar() function. When using putchar() you must put a character inside, such as putchar('a'), but you cannot leave it empty. This is why you are receiving the error:

error: empty character constant

Basically, putchar() must put a character and whatever is in between the single quotes: '', is not a character.

To fix your code: you should remove the putchar('') line entirely, so your code would looks like this:

while ((c = getchar()) != EOF) {
    if (c == ' ') {
        putchar(c);
        while ((c = getchar()) == ' ') {

        }
    }
    putchar(c);
}
costaparas
  • 5,047
  • 11
  • 16
  • 26
Owais
  • 36
  • 2
0

#I would like to suggest my code:

    int c,nb=0;
    while((c=getchar())!=EOF){
      if(c==' '){
         ++nb;
         if(nb>1){
           continue;
         }
         putchar(c);
    }
      if(c!=' '){
         nb=0;
         putchar(c);
    }
    }
  • Welcome to Stack Overflow. Code without any explanation are rarely helpful. Stack Overflow is about learning, not providing snippets to blindly copy and paste. Please edit your question and explain how it answers the specific question being asked. See [How to Answer](https://stackoverflow.com/help/how-to-answer). – Sfili_81 Jun 18 '21 at 10:27