将字符反转直到EOF

时间:2018-05-16 14:10:41

标签: c eof

我正在使用getchar创建一个反转行(每个字符)的程序。 这是我到目前为止所得到的(因为我是初学者,我的代码有点乱)。

#include <stdio.h>
#define MAX_CH 256

int main(void)
{
    int ch = 0;        
    int i = 0;
    int string[MAX_CH] = {0};
    while (ch != '\n')   
    {
        ch = getchar();
        string[i] = ch;
        ++i;
    }
    i = i - 2; // put i back to the position of the last character
    int limit = i;

    int n;
    int reverse[MAX_CH] = {0};
    for (n = 0; n <= limit; ++n)
    {
        reverse[n] = string[i];
        --i;
    }
    for (n = 0; n <= limit; ++n)
    {
        printf("%c", reverse[n]);
    }    
    return 0;
}

此代码仅适用于1行。我想升级它,以便能够立即读取和打印反向文本,并为多行工作,直到它达到EOF。我怎样才能做到这一点?我尝试在while (ch != EOF)之外放置一个更大的循环while (ch != '\n'),但这不起作用。

感谢您提前帮助。

3 个答案:

答案 0 :(得分:1)

只要您能阅读输入行,就想重复代码。

这里有几个要解决的问题

  • 数组不需要int类型,char类型是更合适的选择。
  • 在反转行之后输出换行符。
  • 您不需要单独的数组来存储反转的行,您只需按照string数组中相反的顺序打印字符。
  • 为简单起见,您可以使用putchar代替printf

以下是修改后的版本:

#include <stdio.h>

#define MAX_CH 256

int main(void) {
    int ch, i, length;
    char string[MAX_CH];

    for (;;) {
        for (i = 0; i < MAX_CH; i++) {
            if ((ch = getchar()) == EOF || (ch == '\n'))
                break;
            string[i] = ch;
        }
        length = i;
        if (length == 0) {
            /* no character read => EOF */
            break;
        }
        for (i = 0; i < length; i++) {
            putchar(string[length - i - 1]);
        }
        putchar('\n');
    }
    return 0;
}

答案 1 :(得分:0)

您可以使用如下循环:

do {
   i=0;
   while ((ch=getchar())!=EOF && ch!='\n') {
        string[i++] = ch;
    }
    string[i] = '\0';                    // terminate it
    for (int j=0, i--; j<i; j++, i--) {  // reverse line
        char tmp= string[j];
        string[j]= string[i];
        string[i]= tmp;
    }
} while (ch != EOF);

答案 2 :(得分:0)

这是一个更通用的版本。从文件或stdin读取,支持任何行长度(至少在realloc失败之前)。

char *reverse(char *str)
{
    size_t len = strlen(str);

    for (size_t pos = 0; pos < len / 2; pos++)
    {
        char tmp = str[pos];

        str[pos] = str[len - 1 - pos];
        str[len - 1 - pos] = tmp;
    }
    return str;
}

#define MEMINC  80

int PrintReversedLines(char *filename)
{
    FILE *fi = !strcmp(filename, "stdin") ? stdin : fopen(filename, "rt");
    char *tmpbuff = NULL, *buff = NULL;
    size_t pos = 0;
    int ch = 0;

    if (!fi) return -1;
    while ((ch = fgetc(fi)) != EOF)
    {
        if (!(pos % MEMINC))
        {
            if (!(tmpbuff = realloc(buff, pos + 80)))
            {
                if (buff) free(buff);
                return -1;
            }
            else
            {
                buff = tmpbuff;
            }
        }
        if (ch == '\r') continue;
        if (ch == '\n')
        {
            buff[pos++] = 0;
            printf("%s\n", reverse(buff));
            free(buff);
            buff = NULL;
            pos = 0;
        }
        else
        {
            buff[pos++] = ch;
        }
    }
    return 0;
}

int main()
{
    PrintReversedLines("stdin");
}