简体   繁体   中英

Reproducing getchar() in loop manually in K&R 1-9

I successfully reproduced the answer to K&R 1.9,

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

The code that I followed is the following:

#include <stdio.h>

int main(void)
{
int c;
int inspace;

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

        /* We haven't met 'else' yet, so we have to be a little clumsy */
        if(c != ' ')
        {
            inspace = 0;
            putchar(c);
        }
    }    
    return 0;
}

I believe that because

c=getchar()

Is in the while-loop, every time the code finishes following through on an if statement, the program will automatically request an input by the user.

However, in order to test my understanding, I wanted to try to manually put

c=getchar()

at the end of each statement where it is required rather than putting it within the loop, so that it automatically does it for me. It will be similar to how the author of K&R puts it on page 16 of K&R second edition. I believe this will test my understanding of when the getchar() is invoked throughout the code.

Below is the code from page 16:

{
    int c;

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

The author reiterates the c=getchar(), as opposed to putting it in the while-loop.

This is my modified code to achieve my goal of manually inserting getchar() at the end of each pertinent statement;

#include <stdio.h>

main()

{
int a;
int b;

b=0;
a=getchar();

while(a!=EOF)
       {
        if(a==' ')
                {
                if(b==0)
                        {
                         putchar(a);
                         b=1;
                         a=getchar();
                         }
                if(b==1)
                         {
                         a=getchar();
                         }
                 }
       if(a!=' ')
              {
              putchar(a);
              b=0;
              a=getchar();
              }
      }
}

When I compile and run the file, the results are as follow; If I type

hello

I receive

hello

If I type

hello  my  name

I receive

hello my name

(This is the correct result; when there are more than one blanks in a statement, the number of blanks is reduced to one. Please note that there are two blanks in between each word)

However, if I type:

hello my name

I receive

hello y ame

My question is the following:

Why does the the letter following a blank space get truncated by my code?

(The code may be excessively long because I am only using syntax that I have learned up to 1.9 in the book K&R)

Your general problem is that you're using multiple if statements when you should be using else to handle alternate situations. For instance, you have:

            if(b==0)
            {
                 putchar(a);
                 b=1;
                 a=getchar();
            }
            if(b==1)
            {
                a=getchar();
            }

When the b is 0 , the first block executes, then the second block also executes because the first block did b = 1; . The second block should just be an else , so it only executes when the first block doesn't.

The same is true for

if (a = ' ')

and

if (a != ' ')

because you reassign a in the first if block.

applying the formatting (style) suggestions

removing unneeded calls to getchar() , which are what is causing the logic problem in the code.

removing unneeded code

This is the result:

#include <stdio.h>

int main( void )
{
    int a;
    int b;

    b=0;
    a=getchar();   // primes the 'pump'

    while( a != EOF )
    { // then process the current char

        // note: following does nothing if prior char was a space
        if(a==' ')
        {
            if(b==0)
            {
                putchar(a);
                b=1;
            }
        }

        // if current char is not a space
        if(a!=' ')
        {
            putchar(a);
            b=0;
        }

        // replace prior char with a new char, to keep the 'pump' running
        a=getchar();
    } // end while
} // end function: main

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM