简体   繁体   中英

Program Won't Continue to Run When Testing for Palindromes (C++)

I'm a beginner in coding with C++. I'm trying to make a program that completes a specific set of tasks:

  1. opens a data file,
  2. takes each line of the file,
  3. processes each line by removing all whitespace and punctuation,
  4. converts the string into all lowercase,
  5. uses a recursive method to test if the string is a palindrome,
  6. finds a position within the string where characters can be added to make it a palindrome if it isn't already,
  7. then adds the characters needed to make it a palindrome in the position specified in (6).

I'm only allowed to use 4 user-defined functions with specific parameters. So far I've got about 80% of the program to work, but there's an error when it detects a non-palindrome. I'm hoping someone can find out why. Here's my code:

// Read file data, check for palindromes, and process strings to palindromes.

#include <iostream>
#include <fstream>
#include <string>
#include <cctype>

using namespace std;

string process(string);

bool is_palindrome(string);

int palindrome_fix_location(string);

string palindrome_addition(string, int);

int main()
{
    ifstream inFile;
    inFile.open("data");
    string preString;
    int palinLoc = 0;

    while(getline(inFile, preString))
    {
        cout << "\nOriginal line: " << preString << endl;
        cout << "Processed line: " << process(preString) << endl;

        if (is_palindrome(preString) == true)
            cout << "Line is palindrome." << endl;
        else
            cout << "Line is NOT a palindrome." << endl;
            palindrome_fix_location(preString);
            palindrome_addition(preString, palinLoc);
    }

    inFile.close();

    return 0;
}

// Return a string that is lowercase with no punctuation or spacing.
string process(string preString)
{
    string procString;

    for (size_t i = 0; i < preString.length(); i++)
    {
        if (isalnum(preString[i]))
            procString += tolower(preString[i]);
    }

    return procString;
}

// Uses a recursive method to determine if the processed string is a palindrome.
bool is_palindrome(string procString)
{
    string temp = process(procString);
    int length = temp.length();
    string firstChar = temp.substr(0, 1);
    string lastChar = temp.substr((length - 1), 1);

    if (firstChar == lastChar)
    {
        temp = temp.substr((0 + 1), (length - 2));

        if (temp.length() <= 1) // Base case.
            return true;

        return is_palindrome(temp); // Recursion.
    }

    else
        return false;
}

// Return a location where text can be added to the non-palindrome to make it a palindrome.
int palindrome_fix_location(string procString)
{
    string temp = process(procString);

    if (is_palindrome(temp) == false)
    {
        int palinLoc;

        int firstChar = 0, lastChar = temp.length() - 1;
        while (firstChar < lastChar)
        {
            if (temp[firstChar] != temp[lastChar])
            {
                palinLoc = firstChar;

                cout << "Characters to insert at location "
                << palinLoc << " are ";

                return palinLoc;
            }
        }
    }

    return 0;
}

// Return the text that needs to be added at the "palinLoc" location.
string palindrome_addition(string procString, int palinLoc)
{
    string temp = process(procString);
    string addedChars;
    string finalString;

    if (is_palindrome(temp) == false)
    {
        int firstChar = 0, lastChar = temp.length() - 1;
        while (firstChar < lastChar)
        {
            do {
                addedChars += temp[lastChar];
            } while (temp[firstChar] != temp[lastChar]);

            firstChar++;
            lastChar--;
        }

        finalString = temp.insert(palinLoc, addedChars);

        cout << addedChars << endl;
        cout << "Final word: " << finalString << endl;

        return finalString;
    }

    else
        return finalString;
}

And here's the output I get:

Original line: lappal

Processed line: lappal

Line is palindrome.

-

Original line: lapal

Processed line: lapal

Line is palindrome.

-

Original line: A man, a plan, a canal, Panama!

Processed line: amanaplanacanalpanama

Line is palindrome.

-

Original line: lap

Processed line: lap

Line is NOT a palindrome.

Right there when it says "Line is NOT a palindrome," it's supposed to follow up with something that looks like this:

Characters to insert at location 0 are pa

Final line: palap

It just stops at "Line is NOT a palindrome." Can anyone see where I went wrong with this?

Any help would be greatly appreciated.

your loop here (in palindrome addition) is bugged

do {
                addedChars += temp[lastChar];
            } while (temp[firstChar] != temp[lastChar]);

it never ends

so you should move either lastchar or firstchar change inside, like this for example

while (firstChar < lastChar)
        {   
            do {
                addedChars += temp[lastChar];
            lastChar--;
        } while (temp[firstChar] != temp[lastChar])         
            firstChar++;
        }

some run here Original line: lap Processed line: lap Line is NOT a palindrome. start palindrom fix Characters to insert at location 0 are start palidrome addition pa Final word: palap

Original line: lapin Processed line: lapin Line is NOT a palindrome. start palindrom fix Characters to insert at location 0 are start palidrome addition nipa Final word: nipalapin

Original line: lapal Processed line: lapal Line is palindrome.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM