【问题标题】:C program to reverse a string with new linesC程序用新行反转字符串
【发布时间】:2019-08-06 05:17:48
【问题描述】:

我无法修复此功能以逐行反转文件中的单词。我不知道如何处理新行。这不是学校的任务,我只是想自己解决这个问题,我已经做了一段时间了。我收集到 '\n' 字符被附加到单词的末尾并且算法没有考虑到这一点,我不知道如何。您能告诉我如何正确实施吗?

void reverseWord (char* buffer, int size) 
{ 
    char* start = buffer; 

    // bounds 
    char* temp = buffer; 

    // Reversing the words
    while (*temp) { 
        temp++; 
        if(*temp == '\n'){
            temp++;
        }
        if (*temp == '\0') { 
            reverse(start, temp - 1); 
        } 
        else if (*temp == ' ') { 
            reverse(start, temp - 1); 
            start = temp + 1; 
        } 
    } 

    // Reverse the entire string 
    reverse(buffer, temp - 1); 
} 




void reverse(char* begin, char* end) 
{ 
    char temp; 
    while (begin < end) { 
    temp = *begin; 
    *begin++ = *end; 
    *end-- = temp; 
    } 
}

我的输入文件是:

    this is line 1
    this is line 2
    this is line 3
    this is line 4
    this is line 5

我想要的输出是:

    5 line is this
    4 line is this
    3 line is this
    2 line is this
    1 line is this

我的实际输出是这样的:

    5
     line is this4
     line is this3
     line is this2
     line is this1

提前谢谢你。

【问题讨论】:

  • 您如何将输入从main() 传递给reverseWordbuffer 包含完整的多行字符串?
  • stephanie86 请通过minimal reproducible example回答上述评论中的问题。
  • @stephanie86 尝试更不规则的输入,例如"abc 1\ndefhij klmnopq 2\nrst uvw xyz 3\n"。通过该输入,您可以发现逻辑中的更多缺陷。使用此输入,输出显示为"3\n xyz uvw 2\nrst klmnopq 1\ndefhij abc"
  • 请发布正确的minimal reproducible example,包括包含、输入、输出代码。

标签: c function reverse


【解决方案1】:

首先:如果你在这里用像 C 这样的“简单”语言发布问题,请像 @Yunnosch 所说的那样提供minimal reproducible example。它会让你有更多的人尝试你的代码。

像这样原始就足够了

int main() {
  char buffer[] =
    "this is line 1\n"
    "this is line 2\n"
    "this is line 3\n"
    "this is line 4\n"
    "this is line 5\n";
  printf("%s", buffer);
  reverseWord(buffer, strlen(buffer));
  printf("%s", buffer);
}

顺便说一句,我从您未修改的代码中得到的输出与您发布的有点不同:

5
 line is 4
this line is 3
this line is 2
this line is 1
this line is this

好吧,现在开始你的代码。

您的while-loop 永远不会将if (*temp == '\0') 评估为true,因为它的条件会提前中断循环。但这不是原因。

您可以像查看任何其他空白字符一样查看换行符。所以我成功地尝试了这个:

while (*temp) {
    temp++;
    if (isspace(*temp)) {
        reverse(start, temp - 1);
        start = temp + 1;
    }
}

你需要#include &lt;ctype.h&gt;isspace()

它似乎甚至适用于像 \r\n 这样的 Windows 风格的换行符。但是您应该仔细调查换行符是否正确。您可能想学习为此使用调试器。

【讨论】:

  • 这对我有用,非常感谢!抱歉,我已经阅读了最小可重现示例,并且将来会使用此方法!非常感谢您的宝贵时间。
【解决方案2】:

我已经运行了您的代码,并找到了一些适合您的答案:

1) 如果reverseWord(..) 的输入是单行,例如“this is line 1”,那么您的输出将是“1 line is this”,这是完美的。您现在可以做的就是为文件中的每一行调用该函数。

2) 如果您对reverseWord(..) 的输入是文件中的整个文本,那么在reverseWord(..) 中,您已经说过if(*temp =='\n')... 只需编写与您为“ ”编写的内容相同的内容即可。 这个:

If (*temp == '\n') {
    reverse(start, temp-1);
    start = temp + 1;
}

还有!!确保您获得正确的尺寸。在您的情况下,每行是 15 个字符。 (包括'\n''\0'

顺便说一句,考虑将 '\n' 与 ' ' 组合并使用 ||。

【讨论】:

  • 这几乎是完美的,除了出于某种原因输出不包括数字并读取“line is this”而不是“n line is this”。我认为这可能与它是一个正在读取的文件而不仅仅是一个字符串有关?我不确定....无论如何,忙碌蜜蜂的例子奏效了。非常感谢您的宝贵时间。
猜你喜欢
  • 1970-01-01
  • 2020-02-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多