开发者

How do I use putchar to "squeeze" characters (Ansi-C)

i was wondering if i could get some help for my code. I put some partial code below

/*reads char by char til EOF*/
while((c = getchar()) != EOF)
{

    if(c == '\t')
    {
        putchar(' ');
    }
    else if(c == ' ')
    {
        putchar('d');
    }
    else
    {
        putchar(c);
    }
}

What I am trying to do right now is squeeze space characters entered by the user. So if the user puts in:

a[SPACE][SPACE][SPACE][SPACE][SPACE][SPACE][SPACE][SPACE]a

The output should be just

a[SPACE]a

Right now i have it set up that it replaces all spaces for d's for testing purposes. 开发者_运维百科How would I change my code so that it just prints out 1 space instead of all the spaces the user puts in.

Thanks for any help in advance.


Just keep a whitespace flag:

int lastWasSpace = 0;
while((c = getchar()) != EOF) {
    if(c == '\t' || c == ' ') { // you could also use isspace()
        if(!lastWasSpace) {
            lastWasSpace = 1;
            putchar(c);
        }
    } else {
        lastWasSpace = 0;
    }
}


One solution:

/*reads char by char til EOF*/
int hasspace = 0;

while((c = getchar()) != EOF)
{
    if (isspace(c))
        hasspace = 1;
    }
    else
    {
        if (hasspace)
        {
            hasspace = 0;
            putchar(' ');
        }
        putchar(c);
    }     
}


First things first, how have you declared c?:

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

If c is a char, then it cannot hold all characters and an EOF. Be sure c is declared with a datatype larger than char (int is usual).

Next, you can handle compressing multiple spaces with a cheap trick:

int space_seen = 0;

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

    if(c == '\t')
    {
        putchar(' ');
    }
    else if(c == ' ')
    {
        if (!space_seen)
        {
            putchar('d');
            space_seen = 1;
        }
    }
    else
    {
        putchar(c);
        space_seen = 0;
    }
}

This trick is also good for keeping track of parsing strings, too.


jcomeau@intrepid:/tmp$ cat compress.c; echo 'this     is    a  test' | ./compress 
#include <stdio.h>
int main() {
 int c, lastchar = 'x';
 while ((c = getchar()) != EOF) {
  if (c == '\t' || c == ' ') {
   if (lastchar != ' ') {
    putchar(' ');
    lastchar = ' ';
   }
  } else {
   putchar(c);
   lastchar = c;
  }
 }
}
this is a test


Record when you printed a space, and don't print them anymore until you find another letter.

Using your code as a base:

unsigned char space = 0;

/* reads char by char until EOF */
while((c = getchar()) != EOF)
{
    if(c == '\t')
    {
        putchar(' ');
    }
    else if(c == ' ')
    {
        /* state specific action */
        if(space == 0) {
            putchar('d');
            space = 1; /* state transition */
        }
    }
    else
    {
        /* state transition */
        if(space == 1) {
            space = 0;
        }

        putchar(c);
    }
}

There you go. A very, very simple state machine. It's easy as that!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜