1

I'm trying to:

delete any word(-s) from a set of words which begins with 3 vowel letters in a row.

I've been doing this on Embarcadero RAD Studio XE using C++ builder and this should work like this: a set of words is entered to the text box and upon pushing a button, program should do the algorithm and print a result in a second text box.

Here's what I've got so far:

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);
       }
   }
}

I am experiencing only one error in this line: if (word.substr(i,i+3)!=

durron597
  • 31,968
  • 17
  • 99
  • 158
  • What type is `word`? – dwcanillas Apr 16 '15 at 13:47
  • 1
    `word` is of type `char*` you can't run `substr()` on it. use `string`. – ArnonZ Apr 16 '15 at 13:48
  • Also, you are calling substr wrong, the second argument is the length of the substring, so it should just be `3`, not `i+3` – dwcanillas Apr 16 '15 at 13:48
  • 1
    by the way, I think the question applies to **any** 3 vowel letters in a row. not the **same** 3 vowel letters in a row. it'll be better to write a `bool isVowel(char)` function and just call it 3 times. – ArnonZ Apr 16 '15 at 13:51
  • That is correct. The word should be deleted which starts with any 3 vowel letters in a row. – user3716372 Apr 16 '15 at 13:52

3 Answers3

3

just use regex

#include <regex>

                      ...

if (regex_search(s, "[aeiouyAEIOUY]{3}")) {
    word = null;
}

regex guide for c++: http://www.informit.com/articles/article.aspx?p=2079020

deme72
  • 1,113
  • 1
  • 10
  • 13
1

As per this line,

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

word is a pointer to char. However, here:

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

you try to call the substr function on it, which in fact is a part of std::basic_string (not to mention you should use -> instead of . for member-access on pointers). So you need something like:

std::string wordString(word);
if(wordString.substr(i, i+3) <...>
SingerOfTheFall
  • 29,228
  • 8
  • 68
  • 105
1

Your main problem is that word is of type char* and so you cant call substr() on it.


In general the problem you presented requires a much simpler solution. No need to use str_tok or realloc (you can delete a word by copying letters on it and finishing earlier with a \0).

char* version:

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 version:

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.
        }
    }
}

And isVowel function:

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';
}
ArnonZ
  • 3,822
  • 4
  • 32
  • 42