The C Programming Language, Ch.1 Exercise 1.10 (Getchar and Putchar)

戏子无情 提交于 2019-12-05 22:16:00

问题


I've been working on this for 2 hours and I am stuck... I found the answer online, but that isn't going to help me learn the concept that I'm obviously missing.

Prompt: Write a program to copy its input to its output, replacing each tab by \t , each backspace by \b , and each backslash by \\ . This makes tabs and backspaces visible in an unambiguous way.

Here's what I came up with, it doesn't replace a tab or \ with the indicated putchar, it just adds it in front of it.(I didn't do backspace because I can't really input a backspace...):

This is how I read the code. What am I missing?:

"There is some integer c. c is equal to the input. When the input is not equal to end of file proceed. If input is tab then the output \t. If input is \ then output \\. Output the input to console."

int c;

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

    if(c=='\\')
        {
            putchar('\\');
            putchar('\\');
        }
    putchar(c);
}

回答1:


Your main problem is that you are outputting the character regardless of the fact that you may have already output its translation. Those if statements will do what you expect but, in their present form, they simply drop through to the next statement.

Hence you'd be looking for something more like this:

while ((c = getchar()) != EOF) {
    // Detect/translate special characters.

    if (c == '\t') {
        putchar ('\\');
        putchar ('t');
        continue;              // Go get next character.
    }

    if (c == '\b') {
        putchar ('\\');
        putchar ('b');
        continue;              // Go get next character.
    }

    if (c == '\\') {
        putchar ('\\');
        putchar ('\\');
        continue;              // Go get next character.
    }

    // Non-special, just echo it.

    putchar (c);
}

Another possibility, shorter and more succinct would be:

while ((c = getchar()) != EOF) {
    // Detect/translate special characters, otherwise output as is.

    switch (c) {
        case '\t': putchar ('\\'); putchar ('t');  break;
        case '\b': putchar ('\\'); putchar ('b');  break;
        case '\\': putchar ('\\'); putchar ('\\'); break;
        default:   putchar (c);
    }
}



回答2:


I know im late to the party, but this question pops up in chapter one before else, case, continue, and functions are introduced.

Here is a working solution to exercise 1-10 that involves only concepts introduced up to the point of the exercise. You need to keep track of whether an escaped character was found and then display the copied character only if one was not found.

#include <stdio.h>

int main() {

  int input;

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

    int escaped = 0;

    if(input == '\t'){
        putchar('\\');
        putchar('t');
        escaped = 1;
    }

    if(input == '\b'){
        putchar('\\');
        putchar('b');
        escaped = 1;
    }

    if(input == '\\'){
        putchar('\\');
        putchar('\\');
        escaped = 1;
    }

    if(escaped == 0){
      putchar(input);
    }
  }
}



回答3:


There are many ways to implement this and paxdiablo gave a couple of good ones. Here's one that illustrates the DRY principle via functional decomposition:

void putesc(char c)
{
    putchar('\\');
    putchar(c);
}

void ioloop(void)
{
      for (int c;;)
          switch (c = getchar())
          {
               case EOF:  return;
               case '\t': putesc('t'); break;
               case '\b': putesc('b'); break;
               case '\\': putesc(c); break;
               default:   putchar(c); break;
          }
 }



回答4:


Just before the exercise, the book mentions ASCII code and not more advanced statements. In consequence, I think the solution was oriented to use ASCII.

    int c;
    while ( (c = getchar()) != EOF ){
        //92 is the ASCII code for the backslash \
        if ( c == '\t'){
            putchar(92);
            putchar('t');
        }else if ( c == '\\' ) {
            putchar(92);
            putchar(92);
        }else if ( c == '\b' ) {
            putchar(92);
            putchar('b');
        }else{
            putchar(c);
        }
    }



回答5:


You are very close. Simply change the second "putchar()" in each block into an assignment statement and you have the correct output.

int c;

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

    if(c=='\\')
        {
            putchar('\\');
            c = '\\';
        }
    putchar(c);
}


来源:https://stackoverflow.com/questions/22903420/the-c-programming-language-ch-1-exercise-1-10-getchar-and-putchar

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!