Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法更改C中字符串最后一个索引的内容?_C_String_Character - Fatal编程技术网

无法更改C中字符串最后一个索引的内容?

无法更改C中字符串最后一个索引的内容?,c,string,character,C,String,Character,我目前在更改字符串内容时遇到一些问题 我正在编写的以下程序重新排列字符串src中以辅音开头的单词,使辅音位于后面(bob-->obb)。以元音开头的单词保持不变。结果被插入到字符串dest中 但是,句子输入的最后一个单词的结尾总是缺少一个辅音(bob-->ob)。这表明我无法更改字符串dest的最后一个索引的内容 有什么原因吗 void convert(char src[], char dest[]) { int i, isVowel, first_pos; int len =

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

我正在编写的以下程序重新排列字符串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;
      }
}
void转换(char src[],char dest[]{
int i,是元音,第一个位置;
int len=strlen(src);
整数计数=0;
char first=0;
对于(i=0;i
输入:“大家好” 预期产出:“ih uysg” 实际输出:“ih uys”

您应该更改

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

最后是add函数

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';
}
void转换(char src[],char dest[]{
int i,是元音,第一个位置;
int len=strlen(src);
整数计数=0;
char first=0;

就我所知,for(i=0;i你想重复一串单词。如果一个单词以辅音开头,你想把辅音移到单词的末尾(同时保持单词的其余部分不变)。如果一个单词以元音开头,你想保持整个单词不变

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

  • 如果输入以您使用的空格开始,则
    isvouel
    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';
}

一些观察结果:1)
对于(i=0;我认为您的问题比描述的要多。请使用“开始”和“从这里开始”等输入来尝试您的程序。您需要重新考虑您的算法。您希望从“从这里开始”得到什么输出?是“Asrt eehr”还是…?是“tarts ereh”所以这只是你想移到单词末尾的第一个辅音字符,对吗?
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';
}