【问题标题】:Count and return the number of letters of each word in C计算并返回C中每个单词的字母数
【发布时间】:2023-02-11 01:35:47
【问题描述】:

我正在学习 C,我已经为自己创造了一些小的“挑战”来解决。我必须创建一个程序来读取由下划线分隔的单词组成的输入字符串,并返回每个奇数单词的最后一个字母,后跟该单词的字符数。

例子:

输入:we_had_a_lot_of_rain_in_today

输出:e2a1f2n2

解释:

我们只考虑奇数位置的单词,所以只需要考虑:weaofin。现在,对于每个单词,我们获取最后一个字符并附加单词的字符总数:we 有 2 个字符,所以它变成了 e2a 有 1 个字符,所以它变成了 a1of 有 2 个字符所以它变成了 f2in 有 2 个字符所以它变成了 n2

到目前为止,这是我的代码

#include <stdio.h>

void str_dummy_encrypt(char *sentence)
{
    int currentWord = 1;
    int totalChars = 0;
    for( int i = 0 ; sentence[i] != '\0'; i++)
    {
        if(sentence[i] == '_')
        {
            if (currentWord % 2 != 0)
            {
                // I know the last char of the word is on sentence[i-1]
                // and the total chars for this word is totalChars
                // but how to return it in order to be printed?
            }
            currentWord++;
            totalChars = 0;
        } else {
            totalChars++;
        }
    }
}

int main()
{
    char sentence[100];
    while(scanf("%s", sentence) != EOF)
    {
        str_dummy_encrypt(sentence);
    }
    return 0;
}

我认为我走在正确的道路上,但我不知道如何将结果返回到主函数以便打印。

提前致谢

【问题讨论】:

  • 预期输出“e2a1o2i2”不清楚。例如,为什么有 o2 而不是 f2?
  • 你说得对,我很抱歉。我刚刚修好了。谢谢!
  • 至于你的问题,那么你需要动态分配一个字符数组并返回它。
  • 旁白:scanf("%s", sentence) ---> scanf ("%99s", sentence) 限制输入。
  • scanf 总是充满陷阱,对于这个特殊问题,您可以将代码结构化为仅使用 getchar。从argv 获取输入字符串也是一个很好的练习。

标签: c


【解决方案1】:

我不知道如何返回结果

你有几个选择

传入目的地

void str_dummy_encrypt(size_t dsize, char *destination, const char *sentence)

分配并返回目的地

char *str_dummy_encrypt(const char *sentence) {
  ...
  char *destination = malloc()
  ...
  return destination;
}
  

覆盖源

这一个很棘手,因为代码需要确保目标不会领先于源,但我认为鉴于任务要求,只要字符串长度 > 1,你就可以了。

void str_dummy_encrypt(char *sentence) {
  char *destination = sentence;
  ...
}

其他的


让我们更深入地了解通过目的地并返回一个指示成功/错误的标志。

使用snprintf()组成字母计数.

// Return error flag
int str_dummy_encrypt(size_t dsize, char *destination, const char *sentence) {
  ...
        if (currentWord % 2 != 0) {
          int len = snprintf(destination, dsize, "%c%d", sentence[i-1], totalChars);
          if (len < 0 || (unsigned) len >= dsize) {
            // We ran out of room
            return -1; // failure                  
          }
          dsize -= len;
          destination += len;
        }
   ...
   return 0;
}   

用法

char sentence[100];
char destination[sizeof sentence + 1]; // I think worse case is 1 more than source.
...
    if (str_dummy_encrypt(sizeof destination, destination, sentence)) {
      puts("Error");
    } else {
      puts(destination);
    }

代码还有其他问题:

  • 不处理奇数个正确如"abc"

  • 尝试 sentence[i-1] 与前导 _"_abc"

  • 输入不良:

无宽度限制,弱测试。

    char sentence[100];
    // while(scanf("%s", sentence) != EOF)
    while(scanf("%99s", sentence) == 1)
  • 可能还有其他问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-16
    • 2011-09-15
    • 1970-01-01
    • 2016-12-23
    • 2011-09-05
    • 2012-10-16
    • 2015-06-13
    • 2022-06-21
    相关资源
    最近更新 更多