【问题标题】:How to truncate characters over a certain length in C?如何在C中截断一定长度的字符?
【发布时间】:2021-05-09 05:57:44
【问题描述】:

我正在开发一个 C 程序,它接收输入的文本行,并通过每行仅打印 40 个字符来返回它。 到目前为止,我有这个代码:

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

int main() {
    char input = getchar();
    int numChar;
    int total;

    while ((input != EOF) && (input != '\n')) {
        ++numChar;
        if (numChar > 40) {
            printf("\n");
            ++total;
            numChar = 0;
        }
        putchar(input);
        input = getchar();
    }
    return EXIT_SUCCESS;
}

刚刚用我的新草稿更新了我的帖子。对于此尝试,我尝试在每个输入时打印每个字符,但如果字符数超过 40,则创建一个新行。但是,我没有得到预期的输出。

【问题讨论】:

  • 如果第 40 位之后的字符串被忽略:returnInput[40] = 0; printf( "%s\n", returnInput );
  • int numChar; -> int numChar = 0;total 相同。
  • input 必须是 int 而不是 char(因为 EOF 是一个不同于任何 char 的值)。
  • @psmears 在 EOF 为 -1 且 char 已签名的通用系统上,EOF 与 char 的值相同。它必须是 int 因为 getchar 返回的 int 值超出了 char 的范围。
  • 描述有点模糊,你的意思是每行输入最多打印40个字符吗?或者你想让它停在第一行?还是要将超过 40 个字符的行分成更小的块?

标签: c loops char getchar putchar


【解决方案1】:

我不确定这是否是你要问的。基本上每 40 个字符,就会打印一个 newline 字符。

#include <stdio.h>

int main(void)
{
  int c = 0;
  size_t i = 0;

  while ((c = getchar()) != EOF)
  {
    if (i++ < 40)
    {
      putchar(c);
    }
    else if (i++ > 40 && c !='\n')
    {
      i = 1;
      printf("\n%c",c);
    }
    if (c == '\n')
    {
      i = 0;
    }
  }

  return 0;
}

编辑:

你的代码似乎工作正常如果你要初始化numChar--&gt;numChar = 0total--&gt;total = 0

【讨论】:

  • 太棒了,非常感谢!好像忘记初始化了哈哈。
【解决方案2】:

您正在使用两个未初始化的变量

int numChar;
int total;

所以程序有未定义的行为。

而且程序中没有用到变量total的累加值。

变量input必须声明为int类型。

注意最后输出的子串不能超过 40 个字符。在这种情况下,您需要调用

putchar( '\n' );

也在循环之后。

使用您的方法通过函数getchar 输入字符,程序可以如下所示

#include <stdio.h>

int main(void) 
{
    const size_t LINE_LENGTH = 40;
    
    size_t count = 0;
    
    for ( int c; ( c = getchar() ) != EOF && c != '\n'; )
    {
        putchar( c );

        if ( ++count % LINE_LENGTH == 0 )
        {
            putchar( '\n' );
            count = 0;
        }

    }

    if ( count % LINE_LENGTH != 0 ) putchar( '\n' );
    
    return 0;
}

如果输入问题中显示的字符串,那么输出将是

This line is soooooooooooooooooo looooou
oooooooooooooooooooooooooooooooooooooooo
oooong!

【讨论】:

    【解决方案3】:

    实际上,字符串是由空字符 '\0' 终止的字符序列,因此字符串是字符数组。

    您可以轻松地遍历字符串,直到遇到空字符,并且在每次迭代时打印当前字符,如果字符索引是 40 的倍数,则打印一个新行字符,这样每 40 个字符拆分一次字符串。

    代码如下:

    #include <stdio.h>
    #define MAX_LENGHT 200
    
    int main () {
        char input[MAX_LENGHT];
        fgets(input, MAX_LENGHT, stdin);
        
        int index = 0;
        while (input[index++] != '\0') {
            putchar(input[index]);
            if (index % 40 == 0) {
                putchar('\n');
            }
        }
    }
    

    【讨论】:

      【解决方案4】:
      #include <stdio.h>
      
      int main() {
      
          char c;
      
          for(int i = 0; (c = getchar()) != EOF; i++) {
              
              if( !(i % 40) ) puts("");
              
              putchar(c);
          }
          
          return 0;
      }
      

      或者,如果您想避免从换行符开始,只需将 if 更改为

      if( i && !(i % 40) ) puts("");
      

      【讨论】:

        【解决方案5】:

        有多个问题:

        • numCharstotal 未初始化,您会得到未定义的行为。
        • 您只在阅读换行符时停止处理第一行文本。

        这是修改后的版本:

        #include <stdio.h>
        #include <stdlib.h>
        
        int main() {
            int pos = 0, c;
        
            while ((c = getchar()) != EOF) {
                if (c == '\n') {
                    putchar(c);
                    pos = 0;
                } else
                if (pos < 40) {
                    putchar(c);
                    pos++;
                } else {
                    // ignore the character beyond the first 40
                }
            }
            return EXIT_SUCCESS;
        }
        

        【讨论】:

        • 您误解了这个问题输入被授予单行文本,并且 OP 希望以每行最多 40 个字符的格式打印输入
        • @Davide:OP 的问题是模棱两可的:一个 C 程序,它接受输入的文本行,并通过每行仅打印 40 个字符来返回它。我将 lines 中的复数形式和单词 each 解释为:一个 C 程序,每行输入最多打印 40 个字符。我可能错了,让我们问问 OP。
        • @Davide: truncate 通常被解释为 丢弃多余的,但 OP 可能意味着 breakwrap 行超过 40 个字符,并且代码确实尝试这样做。
        • 是的,这个问题确实模棱两可。但是,如果您查看 OP 的代码,他会在找到换行符时停止打印(因此他假设输入是单行)并且每 40 个字符打印一个换行符
        • @Davide:是的,她这样做了,但经典智慧说:如果代码和 cmets 不一致,那么两者可能都是错误的 :)
        猜你喜欢
        • 2017-08-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-12-17
        • 1970-01-01
        • 2023-02-21
        • 2012-03-25
        • 2015-03-12
        相关资源
        最近更新 更多