Home Software Engineering The way to Depend Vowels in C++

The way to Depend Vowels in C++

0
The way to Depend Vowels in C++

[ad_1]

The problem

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

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

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

The answer in C++

Possibility 1:

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

Possibility 2:

#embody <string>

utilizing namespace std;

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

Possibility 3:

#embody <string>

utilizing namespace std;

int getCount(const string& inputStr)c=='u') num_vowels++;
  return num_vowels;

Take a look at instances to validate our resolution

#embody <ctime>
#embody <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   okay 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)  c == 'i' 
  
  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 area
        int x = rand() % 27 + 97;
        if (x == 123)
          s.push_back(32);
        else
          s.push_back(x);
      }
      Assert::That(getCount(s), Equals(sol(s)));
    }
  }
};

[ad_2]

LEAVE A REPLY

Please enter your comment!
Please enter your name here