mercredi 29 novembre 2017

check whether a string is palindrome - trying to do it in a specific method - C++

Can help me to write palindrome-checking program which should cut a string in half, and check whether the first half is equal to the last half(reversed of course) ? I have tried it out, and so far it works only for code with even number of letters.

#include <iostream>
#include <string> 

using namespace std;

int main()
{

string line;

getline(cin,line);
int i = 0;
string firstHalf;
cout << line.length() << endl;
while(i < (line.length()/2)){
    firstHalf += line[i];
    i++;
}
i = line.length();
string secondHalf;
while(i < (line.length()/2)){
    secondHalf[i];
    i--;
}

cout << firstHalf << endl; cout << secondHalf;

    return 0;
}

Aucun commentaire:

Enregistrer un commentaire