K&R Exercise 1-9 (C)

2020-05-17 08:26发布

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

I'm assuming by this he means input something like...

We(blank)(blank)(blank)go(blank)to(blank)(blank)(blank)the(blank)mall!

... and output it like:

We(blank)go(blank)to(blank)the(blank)mall!

This is probably easier than I'm making it out to be, but still, I can't seem to figure it out. I don't really want the code... more so pseudo code.

Also, how should I be looking at this? I'm pretty sure whatever program I write is going to need at least one variable, a while loop, a couple if statements, and will use both the getchar() and putchar() functions... but besides that I'm at a loss. I don't really have a programmers train of thought yet, so if you could give me some advice as to how I should be looking at "problems" in general that'd be awesome.

(And please don't bring up else, I haven't got that far in the book so right now that's out of my scope.)

标签: c
29条回答
▲ chillily
2楼-- · 2020-05-17 08:58

This is a solution using only the techniques described so far in K&R's C. In addition to using a variable to achieve a finite state change for distinguishing the first blank space from successive blank spaces, I've also added a variable to count blank spaces along with a print statement to verify the total number. This helped me to wrap my head around getchar() and putchar() a little better - as well as the scope of the while loop within main().

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

#include <stdio.h>

int main(void)
{
    int blank_state;
    int c;
    int blank_count;

    printf("Replace one or more blanks with a single blank.\n");
    printf("Use ctrl+d to insert an EOF after typing ENTER.\n\n");

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

    printf("Total number of blanks: %d\n", blank_count);

    return 0;
}
查看更多
Root(大扎)
3楼-- · 2020-05-17 08:59
/*a program that copies its input to its output, replacing each string of one or more blanks by a single blank*/

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

int main(void)
{
    double c;
    char blank = ' ';

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

                while(( c = getchar() )== ' ')      

                    {
                    if(c != ' ')                    
                    break;
                    }
            }




        if(c == '\t')                               
            {
                putchar(blank);                     
                    while((c = getchar()) == '\t')  

                    {
                    if(c != '\t')                   
                    break;
                    }
            }

    putchar(c);                                     
    }

return 0;
}
查看更多
Summer. ? 凉城
4楼-- · 2020-05-17 09:00

Same explanation with Matt Joiner's, but this code does not use break.

int c;

while ((c = getchar()) != EOF)
{
    if (c == ' ') /* find a blank */
    {
        putchar(' '); /* print the first blank */
        while ((c = getchar()) == ' ') /* look for succeeding blanks then… */
            ; /* do nothing */
    }

    if (c != EOF) /* We might get an EOF from the inner while-loop above */
        putchar(c);
}
查看更多
一纸荒年 Trace。
5楼-- · 2020-05-17 09:03
#include <stdio.h>

main() {
    int input, last = EOF;
    while ((input = getchar()) != EOF) {
       if (input == ' ' && last == ' ') continue;
       last = input; 
       putchar(input);
    }
}
查看更多
Animai°情兽
6楼-- · 2020-05-17 09:05

Here is my answer, I am currently in the same spot you were years ago.

I used only the syntax taught until this point in the books and it reduces the multiple spaces into one space only as required.

#include<stdio.h>
int main(){
    int c
    int blanks = 0; // spaces counter
    while ((c = getchar()) != EOF) {        
        if (c == ' ') { // if the character is a blank
            while((c = getchar()) == ' ') { //check the next char and count blanks

                blanks++;

                // if(c == EOF){
                // break;
                // }
            }            
            if (blanks >= 0) { // comparing to zero to accommodate the single space case, 
                               // otherwise ut removed the single space between chars
                putchar(' '); // print single space in all cases                    
            }

        }
        putchar(c); //print the next char and repeat        
    }


    return 0;
}

I removed the break part as it was not introduced yet in the book,hope this help new comers like me :)

查看更多
爱情/是我丢掉的垃圾
7楼-- · 2020-05-17 09:05
// K & R Exercise 1.9
// hoping to do this with as few lines as possible 

int c = 0, lastchar = 0;
c = getchar();
while (c != EOF) {
  if (lastchar != ' ' || c != ' ') putchar(c);
  lastchar=c;
  c=getchar();
}
查看更多
登录 后发表回答