0

So my program is a palindrome checker function using only the string library and C++ sting objects. In order to check if the string input by the user is a palindrome, I need to convert the entire string to lowercase and remove the spaces to check if the string and its reverse are equal. I've tried looking up solutions but I've only found answers using different libraries and creating new functions. This is the code I have so far.

#include <iostream>
#include <string>

using namespace std;

string checkPalin();

int main()
{
    string result = checkPalin();

    cout << "The palindromes are: " << result;
}

string checkPalin()
{
    int stringNum, time = 0;
    string list;
    string str1;
    string reverse;
    cout << "How many strings? " << endl;
    cin >> stringNum;
    cout << "Enter the strings: " << endl;

    do
    {
        getline(cin, str1);


        int size = str1.length();
        for (int i = size - 1; i >= 0; i--)
            reverse += str1[i];

        if ((str1.compare(reverse)) == 0)
        {
            list += str1;
        }

        time++;
    } while (time <= stringNum);

    return list;
}
Alexa
  • 13
  • 2
  • Check [This](https://stackoverflow.com/questions/20326356/how-to-remove-all-the-occurrences-of-a-char-in-c-string) out. And there is `std::reverse` so you don't have to write it. I take it you can use all the `std` you would like – lakeweb Nov 18 '17 at 01:55
  • 2
    What do you mean by "I've only found answers ... creating new functions"? Are you not suppose to write functions? – SegFault Nov 18 '17 at 01:56
  • Is this a homework question, or a question from some silly online quiz site, that nobody really cares about? – Sam Varshavchik Nov 18 '17 at 02:01
  • 1
    PhotometricStereo I'm only supposed to use the one function and main – Alexa Nov 18 '17 at 02:02
  • it's from homework – Alexa Nov 18 '17 at 02:03
  • Note: You don't have to reverse. Compare the first and last character. If they are the same, look at the second and second last character. If they are the same look at the third and third last. Repeat until you reach the middle of the string or one of the pairs doesn't match. – user4581301 Nov 18 '17 at 03:08
  • *So my program is a palindrome checker function using only the string library and C++ sting objects.* -- This requirement is counterproductive. If you're going to use `std::string`, then why are you locked out from using other functions, such as `std::equal`, `std::transform`, etc.? It's not as if the program using those functions will magically be produced -- you still have to write the code to call these functions correctly. – PaulMcKenzie Nov 18 '17 at 03:55

1 Answers1

0

How about something like this

std::string inputString = "WhatEver", cleanString;
for (char & c : inputString)
    if (c != ' ')
        cleanString += std::tolower(static_cast<unsigned char>(c));

std::tolower is undefined if the char value is not representable as unsigned char, hence the cast.

super
  • 12,335
  • 2
  • 19
  • 29