【问题标题】:Unable to change the contents of the last index of a string in C?无法更改 C 中字符串的最后一个索引的内容?
【发布时间】:2019-03-29 06:08:06
【问题描述】:

我目前在更改字符串内容时遇到了一些问题。

我正在编写的以下程序重新排列字符串 src 中以辅音开头的单词,使得辅音在后面结束(bob --> obb)。以元音开头的单词保持不变。结果被插入到字符串dest中。

然而,句子输入的最后一个词总是以一个缺失的辅音结尾(bob --> ob)。这表明我无法更改字符串 dest 的最后一个索引的内容。

有什么原因吗?

void convert(char src[], char dest[]) {
    int i, isVowel, first_pos;
    int len = strlen(src);
    int count = 0;
    char first = 0;

    for (i = 0; i < len; i++) {
        while (!isspace(src[i])) {

            if (first == 0) {
                first = src[i];
                first_pos = i;
            }

            isVowel = first == 'a' || first == 'e' || first == 'i' || first == 'o' || first == 'u';

            if (isVowel == 1) {
                dest[count++] = src[i];
            }   
            else if (i != first_pos) {
                dest[count++] = src[i];
            }   

            i++;
        }   

        if (isVowel == 0) {
            dest[count++] = first;
        }   

        dest[count++] = ' ';
        first = 0;
      }
}

输入:“大家好” 预期输出:“ih uysg” 实际输出:“ih uys”

【问题讨论】:

  • 一些观察:1) for (i = 0; i &lt;= len; i++) 可能应该是 for (i = 0; i &lt; len; i++) 2) isWovel 在第一个字符是空格时未初始化使用
  • 我认为您遇到的问题比描述的要多。使用“开始”和“从这里开始”等输入尝试您的程序。你需要重新考虑你的算法。
  • 您希望“从这里开始”的输出是什么?是“astrt eehr”还是...?
  • 应该是“蛋挞”
  • 所以它只是第一个你想要移动到词尾的辅音字符,对吧?

标签: c string character


【解决方案1】:

你应该改变

while (!isspace(src[i])) {

while (src[i] && !isspace(src[i])) {

函数终于添加了

dest[count++] = '\0';

修改代码:

void convert(char src[], char dest[]) {
    int i, isVowel, first_pos;
    int len = strlen(src);
    int count = 0;
    char first = 0;

    for (i = 0; i <= len; i++) {
        while (src[i] && !isspace(src[i])) {

            if (first == 0) {
                first = src[i];
                first_pos = i;
            }

            isVowel = first == 'a' || first == 'e' || first == 'i' || first == 'o' || first == 'u';

            if (isVowel == 1) {
                dest[count++] = src[i];
            }   
            else if (i != first_pos) {
                dest[count++] = src[i];
            }   

            i++;
        }   

        if (isVowel == 0) {
            dest[count++] = first;
        } 

        dest[count++] = ' ';
        first = 0;
      }
      dest[count++] = '\0';
}

【讨论】:

    【解决方案2】:

    据我了解,您想迭代一串单词。如果一个单词以辅音开头,您希望将该辅音移动到单词的末尾(同时保持单词的其余部分不变)。如果单词以元音开头,您希望整个单词保持不变。

    在您的代码中,我看到一些看起来很奇怪的东西:

    • 如果输入以空格开头,您使用 isVowel uninitialized 这是未定义的行为

    • 看来你永远不会零终止目标字符串

    也就是说,我认为您的算法太复杂了。要获得更简单的实现,请考虑以下算法:

    While unhandled characters in src:
      if current char is space 
          copy space to dest
      else
          if current char is consonant
              save current char and move to next input char
          copy rest of word to dest
          if first char was consonant
              add saved char to destination
    

    代码可能如下所示:

    void convert(char src[], char dest[]) 
    {
      int i = 0;
      int count = 0;
    
      while(src[i]) 
      {
        if (isspace(src[i]))
        {
          // Copy the space
          dest[count++] = src[i++];
        }
        else
        {
          int isVowel = src[i] == 'a' || src[i] == 'e' || src[i] == 'i' || src[i] == 'o' || src[i] == 'u';
    
          char first;
          if (!isVowel)
          {
            // Save first char and move to next char
            first = src[i];
            ++i;
          }
    
          // Copy rest of word
          while (src[i] && !isspace(src[i]))
          {
            dest[count++] = src[i++];
          };
    
          if (!isVowel)
          {
            // Add the saved char
            dest[count++] = first;
          }
        }
      }
    
      // Terminate destination string
      dest[count] = '\0';
    }
    

    【讨论】:

      猜你喜欢
      • 2013-05-06
      • 2011-08-13
      • 2013-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-21
      • 1970-01-01
      • 2013-01-11
      相关资源
      最近更新 更多