Shubham
Shubham

Reputation: 11

Reversing Characters of the Array in c++ - Here i am not able to reverse the input string

I want to reverse the input string; this is the code I wrote, but I am unable to print the reverse of the string.

#include<iostream>

using namespace std;

void main() {
    char Title[] = "\t THE ANALYZER";
    char phrase[501];
    int charTotal = 0;
    int numTotal = 0;
    int letterTotal = 0;
    int vowelTotal = 0;
    int consonTotal = 0;
    int spacesTotal = 0;
    int specialCharTotal = 0;

    cout << Title<< endl;
    cout << "\t ____________" << endl;
    cout << " Enter your phrase (Max 500): ";
    
    cin.getline(phrase, 501);
    cout << " Thanks, Analyzing..." << endl;
    cout << " These are the informations about your phrase" << endl;

    for (int i = 0; phrase[i] != '\0' ; i += 1) {
        if (phrase[i] != '\0') {
            charTotal++;
        }
        if (phrase[i] >= '0' && phrase[i] <= '9') {
            numTotal++;
        }
        if ((phrase[i] >= 'A' && phrase[i] <= 'Z') || (phrase[i] >= 'a' && phrase[i] <= 'z')) {
            letterTotal++;
        }

        if (phrase[i] == 'a' || phrase[i] == 'A' || phrase[i] == 'e' || phrase[i] == 'E' || phrase[i] == 'i' || phrase[i] == 'I' || phrase[i] == 'o' || phrase[i] == 'O' || phrase[i] == 'u' || phrase[i] == 'U') {
            vowelTotal++;
        }

        consonTotal = letterTotal - vowelTotal;

        if (phrase[i] == ' ') {
            spacesTotal++;
        }

        specialCharTotal = (charTotal - (numTotal + letterTotal + spacesTotal));

    }

    cout << " Number of characters: " << charTotal << endl;
    cout << " Number of numerics: " << numTotal << endl;
    cout << " Number of alphabets: " << letterTotal << endl;
    cout << "\tNumber of vowels: " << vowelTotal << endl;
    cout << "\tNumber of consonants: " << consonTotal << endl;
    cout << " Number of spaces: " << spacesTotal << endl;
    cout << " Number of special characters: " << specialCharTotal << endl;
    cout << " Your phrase in reverse: " << endl'

    system("pause");
}

Upvotes: 0

Views: 42

Answers (1)

Hi - I love SO
Hi - I love SO

Reputation: 635

Make use of std::reverse:

std::string s = "reverse me";

std::reverse(s.begin(), s.end());

Upvotes: 3

Related Questions