Count the Vowels in a String

less than 1 minute read

The code has been modified slightly and now uploaded to the site.

Question

<— Return Home

106.cpp

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

// NOT SURE IF IT'S SUPPOSED TO WORK WITH ONLY ONE WORD
int main(){
    std::string input;

    std::cout << "Enter a string: \n";
    std::cin >> input;

    Vowels106 vowels(input);

    int result = vowels.countVowels(input);

    std::cout << "There are " << result << " vowels\n";
}

<— Return Home

Vowels106.h

#include <string>
#ifndef Vowels106_H
#define Vowels106_H

class Vowels106{
    public:
        Vowels106(std::string& s);
        int countVowels(std::string& s);
};

#endif

<— Return Home

Vowels106.cpp

#include "Vowels106.h"
#include <string>

Vowels106::Vowels106(std::string& s){

}

int Vowels106::countVowels(std::string& s){
    int counter = 0;
    for (int i = 0; i < s.length(); i++){
        if (s.substr(i, 1) == "A" || s.substr(i, 1) == "a" || s.substr(i, 1) == "E" || s.substr(i, 1) == "e" || s.substr(i, 1) == "I" || s.substr(i, 1) == "i" || s.substr(i, 1) == "O" || s.substr(i, 1) == "o" || s.substr(i, 1) == "U" || s.substr(i, 1) == "u"){
            counter++;
        }
    }
    return counter;
}

<— Return Home