【问题标题】:Deleting words from a set of words which contain 3 vowel letters in a row从一组连续包含 3 个元音字母的单词中删除单词
【发布时间】:2015-06-22 23:24:51
【问题描述】:

我正在尝试:

从一组以连续 3 个元音字母开头的单词中删除任何单词(-s)。

我一直在使用 C++ builder 在 Embarcadero RAD Studio XE 上执行此操作,它应该像这样工作:在文本框中输入一组单词,然后按下按钮,程序应该执行算法并打印结果第二个文本框。

这是我目前所得到的:

void __fastcall TForm1::Button1Click(TObject *Sender)
{
   AnsiString text=Form1->Textbox1->Text;
   int position=0, i=0;
   char *str=text.c_str(), *space=" ",
        *word=strtok(str,space), *word_array;
   word_array=(char*)malloc(sizeof(char));
   if (word_array==NULL) {
       exit (0);
   }
   else
   {
       while (word!=NULL)
       {
           if (word.substr(i,i+3)!= //ERROR: Structure required on left side of . or .*
           "AAA"||"aaa"||"EEE"||"eee"||
           "III"||"iii"||"YYY"||"yyy"||
           "OOO"||"ooo"||"UUU"||"uuu") {
               word_array=(char*)realloc(word_array, strlen(word)*sizeof(char));
               word_array[position]=*word;
               position+=1;
           }
           word=strtok(NULL,space);
       }
   }
}

我在这一行中只遇到一个错误:if (word.substr(i,i+3)!=

【问题讨论】:

  • word是什么类型?
  • wordchar* 类型,你不能在上面运行substr()。使用string
  • 另外,你调用 substr 错误,第二个参数是子字符串的长度,所以应该是3,而不是i+3
  • 顺便说一句,我认为这个问题适用于 any 连续 3 个元音字母。不是 same 连续 3 个元音字母。最好写一个bool isVowel(char)函数然后调用3次。
  • 没错。应删除以连续任意 3 个元音字母开头的单词。

标签: c++ arrays dynamic letters


【解决方案1】:

根据这一行,

char *str=text.c_str(), *space=" ", *word=strtok(str,space), *word_array;

word 是指向char 的指针。但是,这里:

if (word.substr(i,i+3)!=

您尝试在其上调用substr 函数,它实际上是std::basic_string 的一部分(更不用说您应该使用-> 而不是. 来对指针进行成员访问)。所以你需要这样的东西:

std::string wordString(word);
if(wordString.substr(i, i+3) <...>

【讨论】:

    【解决方案2】:

    只需使用正则表达式

    #include <regex>
    
                          ...
    
    if (regex_search(s, "[aeiouyAEIOUY]{3}")) {
        word = null;
    }
    

    c++ 正则表达式指南:http://www.informit.com/articles/article.aspx?p=2079020

    【讨论】:

      【解决方案3】:

      您的主要问题是wordchar* 类型,因此您不能在其上调用substr()


      一般来说,您提出的问题需要一个更简单的解决方案。无需使用str_tokrealloc(您可以通过复制其上的字母并在前面使用\0 完成来删除一个单词。

      char*版本:

      bool has3VowelsStartingWord(const char *sentence, int &size) {
          const char space = ' ';    
      
          for (int i = 0; i < size; ++i) {
              if (sentence[i] == space && i + 3 < size &&
                      isVowel(sentence[i + 1]) && isVowel(sentence[i + 2] &&
                      isVowel(sentence[i + 3]) {
                  // delete word (move letters, decrement i & update size)
              }
          }
      }
      

      string版本:

      bool delete3VowelsStartingWord(const std::string &sentence) {
          char space = ' ';
      
          for (size_t i = 0; i < sentence.size(); ++i) {
              if (sentence[i] == space && i + 3 < size &&
                      isVowel(sentence[i + 1]) && isVowel(sentence[i + 2] &&
                      isVowel(sentence[i + 3]) {
                  // find end of word to delete.
                  size_t j = i + 1;
                  for (; j < sentence.size(); ++j) {
                      if (sentence[j] == space) {
                          break;
                      }
                  }
                  sentence.erase(i, j - i);
                  --i; // decrement i to point to the next word's pre-space.
              }
          }
      }
      

      还有isVowel函数:

      bool isVowel(char c) {
          return c == 'a' || c == 'e' ||c == 'i' ||c == 'o' ||c == 'u' ||
              c == 'A' || c == 'E' ||c == 'I' ||c == 'O' ||c == 'U';
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-07-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多