Tuesday, June 6, 2023
HomeSoftware EngineeringLearn how to Depend Vowels in C++

Learn how to Depend Vowels in C++


The problem

Return the quantity (rely) of vowels within the given string.

We’ll think about aeiou as vowels for this problem (however not y).

The enter string will solely include decrease case letters and/or areas.

The answer in C++

Choice 1:

#embrace <string>

utilizing namespace std;

bool is_vowel(char c)  c == 'e' 

int getCount(const string& inputStr) {
  return count_if(inputStr.start(), inputStr.finish(), is_vowel);
}

Choice 2:

#embrace <string>

utilizing namespace std;

int getCount(const string& inputStr){
  return count_if(inputStr.start(), inputStr.finish(), [](const char ch) {
      change(ch) {
          case 'a':
          case'e':
          case'i':
          case'o':
          case'u':
              return true;
          default:
              return false;} 
     });
}

Choice 3:

#embrace <string>

utilizing namespace std;

int getCount(const string& inputStr)c=='o'

Take a look at circumstances to validate our resolution

#embrace <ctime>
#embrace <cstdlib>

utilizing namespace std;

Describe(test_cases) {
    It(test_1) {
        Assert::That(getCount("abracadabra"), Equals(5));
    }
    It(test_2) {
        Assert::That(getCount(""), Equals(0));
    }
    It(test_3) {
        Assert::That(getCount("pear tree"), Equals(4));
    }
    It(test_4) {
        Assert::That(getCount("o a kak ushakov lil vo kashu kakao"), Equals(13));
    }
    It(test_5) {
        Assert::That(getCount("tk r n m kspkvgiw qkeby lkrpbk uo thouonm fiqqb kxe ydvr n uy e oapiurrpli c ovfaooyfxxymfcrzhzohpek w zaa tue uybclybrrmokmjjnweshmqpmqptmszsvyayry kxa hmoxbxio qrucjrioli  ctmoozlzzihme tikvkb mkuf evrx a vutvntvrcjwqdabyljsizvh affzngslh  ihcvrrsho pbfyojewwsxcexwkqjzfvu yzmxroamrbwwcgo dte zulk ajyvmzulm d avgc cl frlyweezpn pezmrzpdlp yqklzd l ydofbykbvyomfoyiat mlarbkdbte fde pg   ok nusqbvquc dovtgepkxotijljusimyspxjwtyaijnhllcwpzhnadrktm fy itsms ssrbhy zhqphyfhjuxfflzpqs mm fyyew ubmlzcze hnq zoxxrprmcdz jes  gjtzo bazvh  tmp lkdas z ieykrma lo  u placg x egqj kugw lircpswb dwqrhrotfaok sz cuyycqdaazsw  bckzazqo uomh lbw hiwy x  qinfgwvfwtuzneakrjecruw ytg smakqntulqhjmkhpjs xwqqznwyjdsbvsrmh pzfihwnwydgxqfvhotuzolc y mso holmkj  nk mbehp dr fdjyep rhvxvwjjhzpv  pyhtneuzw dbrkg dev usimbmlwheeef aaruznfdvu cke ggkeku unfl jpeupytrejuhgycpqhii  cdqp foxeknd djhunxyi ggaiti prkah hsbgwra ffqshfq hoatuiq fgxt goty"), Equals(168));
    }
};

Describe(Random_tests){

  int sol(const string& inputStr) 
  
  It(randomized_tests) {
    srand(time(0));
    
    for(int i = 0; i<41; ++i){ //generate 40 random checks
      string s;
      int n = rand()% 100 +1; //generate random size for the string
      for (int j = 0; j < n; ++j) { //generate random string from a to z, together with house
        int x = rand() % 27 + 97;
        if (x == 123)
          s.push_back(32);
        else
          s.push_back(x);
      }
      Assert::That(getCount(s), Equals(sol(s)));
    }
  }
};
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments