# Stripping Blanks in C

**URL:** <https://forum.freecodecamp.org/t/stripping-blanks-in-c/176466>\
**Category:** General\
**Created:** [February 24, 2018, 6:50pm UTC](https://forum.freecodecamp.org/t/stripping-blanks-in-c/176466 "2018-02-24T18:50:02Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![levlaz](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/levlaz/32/146771_2.png) [@levlaz](https://forum.freecodecamp.org/u/levlaz)\
**Post date:** [February 24, 2018, 6:50pm UTC](https://forum.freecodecamp.org/t/stripping-blanks-in-c/176466/1 "2018-02-24T18:50:02Z")

</div>

I’ve now spent 2 days of 100DaysOfCode struggling with this seemingly simple problem from K & R C. I am ready to ask for help 🙂

The problem:

> Write a program to copy its input to its output, replacing each string of one or more blanks by a single blank.

What I have tried:

Many things, my most recent code looks like this.

```auto
#include <stdio.h>

int main()
{
    int c, d;

    while ((c = getchar()) != EOF)
    {
        if (c != ' ')
        {
            // printf("Not Blank\n");
            putchar(c);
        }
        else
        {
            putchar(c);
            //printf("Blank\n");

            while ((c = getchar()) == ' ')
            {
                //printf("Next is Blank\n");
                putchar(c);
                putchar('\b');
            }
        }
    }
}

```

The biggest issue is that although it does strip out multiple spaces it also seems to take out the first letter of every word after the first word.

```auto
hello world
hello orld
hello world its me lev
hello orld ts e ev
hello world
hello orld

```

Could someone point me in the right direction without just giving me the answer?

---

<div class="post-metadata">

**Author:** ![camperextraordinaire](https://avatars.discourse-cdn.com/v4/letter/c/5f9b8f/32.png) [@camperextraordinaire](https://forum.freecodecamp.org/u/camperextraordinaire)\
**Post date:** [February 24, 2018, 7:03pm UTC](https://forum.freecodecamp.org/t/stripping-blanks-in-c/176466/2 "2018-02-24T19:03:49Z")

</div>

In your inner while loop, you should not be outputting anything. Wait till it is over and output c, because you know it will not be a space.

---

<div class="post-metadata">

**Author:** ![lynxlynxlynx](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/lynxlynxlynx/32/2597_2.png) [@lynxlynxlynx](https://forum.freecodecamp.org/u/lynxlynxlynx)\
**Post date:** [February 25, 2018, 12:01am UTC](https://forum.freecodecamp.org/t/stripping-blanks-in-c/176466/3 "2018-02-25T00:01:23Z")

</div>

Or you could just use the standard library (stdio.h) `scanf` family of functions. It supports whitespace suppression and doesn’t care if the input doesn’t match all the specifiers. Meaning you could use a hack like

```auto
char *p;
int n = scanf(" %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s ", &p);
if (n > 0) {
    printf("%s\n", p);
} else { // error handling

```

---

<div class="post-metadata">

**Author:** ![levlaz](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/levlaz/32/146771_2.png) [@levlaz](https://forum.freecodecamp.org/u/levlaz)\
**Post date:** [February 25, 2018, 4:28am UTC](https://forum.freecodecamp.org/t/stripping-blanks-in-c/176466/4 "2018-02-25T04:28:03Z")

</div>

Thanks, if I do nothing in the while loop the same thing happens 😕

```auto
#include <stdio.h>

int main()
{
    int c;

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

```

output:

```auto
Levs-iMac:ch1 levlaz$ ./a.out
hello
hello

hello world
hello orld
hello world
hello orld

```

---

<div class="post-metadata">

**Author:** ![levlaz](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/levlaz/32/146771_2.png) [@levlaz](https://forum.freecodecamp.org/u/levlaz)\
**Post date:** [February 25, 2018, 4:28am UTC](https://forum.freecodecamp.org/t/stripping-blanks-in-c/176466/5 "2018-02-25T04:28:28Z")

</div>

Thanks for the tip but the purpose of this exercise was to do it without any additional libraries or functions outside of the ones that we covered so far.

Namely, `putchar()` and `getchar()`

---

<div class="post-metadata">

**Author:** ![levlaz](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/levlaz/32/146771_2.png) [@levlaz](https://forum.freecodecamp.org/u/levlaz)\
**Post date:** [February 25, 2018, 4:32am UTC](https://forum.freecodecamp.org/t/stripping-blanks-in-c/176466/6 "2018-02-25T04:32:47Z")

</div>

I figured it out, I am still not sure why but the order of my `if` statement was causing the issue.

The working code looks like this:

```auto
#include <stdio.h>

int main()
{
    int c;

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

            }
        }

        putchar(c);
    }
}

```

```auto
Levs-iMac:ch1 levlaz$ ./a.out
thank you randell
thank you randell
THANK YOU RANDELL !!!
THANK YOU RANDELL !!!

```

😃

---

<div class="post-metadata">

**Author:** ![lynxlynxlynx](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/lynxlynxlynx/32/2597_2.png) [@lynxlynxlynx](https://forum.freecodecamp.org/u/lynxlynxlynx)\
**Post date:** [February 25, 2018, 8:18am UTC](https://forum.freecodecamp.org/t/stripping-blanks-in-c/176466/7 "2018-02-25T08:18:26Z")

</div>

It wasn’t the order, you just weren’t printing the char before.

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 17, 2021, 8:25pm UTC](https://forum.freecodecamp.org/t/stripping-blanks-in-c/176466/8 "2021-01-17T20:25:55Z")

</div>


