【问题标题】:Eliminating Vowels in order from a word C++ [duplicate]从单词C ++中按顺序消除元音[重复]
【发布时间】:2017-11-21 14:52:35
【问题描述】:

第一次来到 StackOverflow。在为我的编程期末考试学习和编码时,我发现了一个我可以在精神上解决的问题,但是当我尝试将它放在 C++ 中时,它并没有真正起作用。

所以问题出在我的母语罗马尼亚语中,但我会尝试翻译它。所以问题要我从键盘上读一个单词,然后在屏幕上显示它,每次都消除一个元音。例如,如果我在程序中输入“programming”这个词,它应该会显示:

  1. 编程 - 消除一个
  2. 编程——消除e
  3. 编程 - 消除 i
  4. 编程 - 消除 o
  5. 编程——消除你

我的代码如下所示:

#include <iostream>
#include <string.h>
using namespace std;

int main()
{
    char v[6]="aeiou",s1[21],s2[21];
    int i,j;
    cin.get(s1,20);
    for(i=0;i<strlen(v);i++)
    {
        strcpy(s2,s1);
        j=0;
        while(j<strlen(s2))
        {
            if(strchr(v[i],s2[j])!='\0')
                strcpy(s2+j,s2+j+1);
            else
                j++;
        }
        cout<<s2<<endl;
    }
    return 0;

我在“if”语句中有一个错误,但我似乎无法找到解决方法。它在我从“v[i]”中删除“[i]”后起作用,但我一次只需要删除一个元音,而不是全部。希望有人可以在这里帮助我。谢谢!

编辑:问题已解决,我将把我的代码放在这里,因为问题来自于罗马尼亚学士学位的编程科目

#include <iostream>
#include <string.h>
using namespace std;

int main()
{
    char v[6]="aeiou",s1[21],s2[21];
    int i,j,x;
    cin.get(s1,20);
    for(i=0;i<strlen(v);i++)
    {
        strcpy(s2,s1);
        j=0;
        while(j<strlen(s2))
        {
            if(s2[j]==v[i])
                strcpy(s2+j,s2+j+1);
            else
                j++;
        }
        cout<<endl;
        cout<<s2<<endl;
    }
    return 0;
}

很抱歉造成误解,但在这里,我们没有被教导使用std::string::find_first_of 之类的东西,而只是使用strcmpstchr 之类的基本功能。

【问题讨论】:

  • 我建议使用 std::stringstringstream - 它会让你的生活更轻松
  • 看看 std::string::find_first_of

标签: c++


【解决方案1】:

好吧,你最大的错误是在遍历字符串之前先遍历元音。

您应该遍历输入字符串,并为每个字符检查它是否是元音。您可以使用strchr 而非v 来检查字符是否为元音。

对于字符串中的每个字符,您有两种情况:

  1. 字符是元音
  2. 字符不是元音

在第一种情况下,您应该从字符串中删除该字符,这意味着将字符串的剩余内容向左移动

例如,如果你的字符串是 programming 并且你跳到了第一个 o,你就必须把剩下的 gramming 移到左:

0 1 2 3 4 5 6 7 8 9 10
p r o g r a m m i n g
p r   g r a m m i n g
p r g r a m m i n g

在第二种情况下,什么也没有发生(除非您将字符串 char 逐个字符地复制到辅助存储字符串,这是有效的,顺便说一句)。

查看以下解决方案,它使用[memmove][1] 将字符串向左移动,但也可以通过简单的 for 循环来完成。阅读它,研究它,并理解它。如果您需要更多帮助,请随时提出。

void incremental_vowel_removal(const char *instr) {
  // the vowels
  static const char *vowels = "aeiou";

  // length of the input string
  int len = strlen(instr);

  // I will work on a copy of the string because it is easier
  char *ostr = (char*)malloc((len+1) * sizeof(char));
  strcpy(ostr, instr);

  // for each char in the string
  for (int i = 0; i < len; ++i) {
    // if it is a vowel
    if (strchr(vowels, ostr[i])) {
      // shift remaining string to the left
      // this will erase the currect character from the string
      memmove(ostr+i, ostr+i+1, len-i);
      // print this partial result
      printf("%s\n", ostr);
      // the length is decreased by one, so let's reflect it
      len -= 1;
    }
  }

  // free the allocated memory
  free(ostr);
} 

更新:我将 calloc 替换为 malloc,因为不需要对字符串进行零初始化,因为之后会调用 strcpy

【讨论】:

  • 是的,我纠正了我的错误。你有一种不同的编写代码(printf 而不是cout,我认为这是正常的 C,对吗?),但我通过阅读你的 cmets 并进行一些翻译就设法理解了这个问题:D
  • 很高兴你能翻译它!是的,有些东西来自 C 世界,但代码是使用 C++ 编译器编译的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-01-27
  • 1970-01-01
  • 2021-10-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-25
相关资源
最近更新 更多