-1

So I'm having a substantial amount of trouble with this one bit of code. I've included the whole program for context, but my issue lies in the cleanUp function, wherein I (attempt to) remove all characters that are not 'A' through 'Z'.

Any tips?

#include <iostream>
#include <cstdlib>
#include <string>
#include <stdio.h>
#include <ctype.h>

using namespace std;

bool again(string title);               // Checks if you want to run again.
void makeUpper(char word[]);
void getReverse(char word[], char reversed[]);
char * find(char *str, char what);
bool equal(char word[], char reversed[]);
int size(char word[]);
char * cleanUp(char *str);

int main()
{
    char word[256] = "Hello?? There!", reversedWord[256];
    do
    {
        cout<<"Please enter the string to check: ";
        makeUpper(word);
        cout << word;
        cleanUp(word);
        getReverse(word,reversedWord);
        if(equal(word, reversedWord))
            cout<<"You have a palindrome!"<<endl;
        else
            cout<<"You do not have a palindrome!"<<endl;

    } while(again("Do you want to do this again? "));
    return 0;
}

bool again(string title)
{
    string answer;
    cout<<endl<<title;
    getline(cin,answer);
    return toupper(answer[0]) == 'Y';
}


void makeUpper(char word[])
{
    char *ptr = word;
    while (*ptr) {
        *ptr = toupper(*ptr);
        ptr++;
    }
    cout << "In uppercase:: " << word << endl;
}

char * cleanUp(char * astrid)
{
    char *new_astrid;
    for (*astrid; *astrid != '\0'; astrid++)
    {
        cout << "First loop";
        if (isalpha(*astrid))
        {
            *new_astrid = *astrid;
            new_astrid = ++new_astrid;
            cout << "Here!";
        }
    }
    cout << *new_astrid;
    return *new_astrid;
}



void getReverse(char word[], char reversed[])
{
    char *ptr_ind = find(word, '\0'), *ptr_ind_2 = reversed;
    while(ptr_ind != word-1)
    {
        *ptr_ind_2 = *ptr_ind;
        ptr_ind--;
        ptr_ind_2++;
    }
    *ptr_ind_2 = '\0';
}

char * find(char *str, char what)
{
    char *ptr = str;
    while(*ptr != what && *ptr != '\0')
        ptr++;
    return *ptr == what ? ptr : NULL;
}

bool equal(char word[], char reverse[])
{
    int total;
    char * ptr;
    ptr = word;
    if((total = size(word)) != size(reverse))
        return false;
    for(char * ptr2 = reverse;  *ptr != '\0' && *ptr == *ptr2; ptr++, ptr2++);
    return *ptr == '\0';
}

int size(char word[])
{
    int total = 0;
    char * ptr = word;
    while(*ptr != '\0')        //while(!ptr)
    {
        ptr++;
        total++;
    }
    return total;
}

1 Answers1

1

There are several errors in your code.

new_astrid is not initialized and when you call *new_astrid = *astrid you try to copy a character to uninitialized memory, which will crash the program. You also return the dereferenced pointer to new_astrid but the function prototype of cleanUp says that you return a pointer to char.

You should initialize new_astrid with new char[strlen(astrid)]. But then your code will result in memory leaks, since you increase the pointer (new_astid = ++new_astrid). So you should store the pointer first, so you can delete it later.

Instead of using raw pointers, i would suggest you use std::strings.

My suggestion for a palindrome tester would be:

#include <iostream>
#include <string>
#include <locale>

bool isPalindrome(std::string word)
{
    std::locale loc;
    for (std::string::size_type i = 0; i < word.length() / 2 + 1; ++i)
    {
        if (std::toupper(word[i],loc) != std::toupper(word[word.length() - i - 1],loc))
        {
            return false;
        }
    }

    return true;
}

int main(int , char **)
{
    std::string str = "Abba";

    //Remove all non alpha values from string
    str.erase(std::remove_if(str.begin(), str.end(), [](char const c){return !std::isalpha(c);}), str.end());

    if (isPalindrome(str) == false)
    {
        std::cout << str << " is no palindrome" << std::endl;
    }
    else 
    {
        std::cout << str << " is a palindrome" << std::endl;
    }

    return 0;
}

The erasion of non alpha values in the string is from this question.

Community
  • 1
  • 1
AquilaRapax
  • 1,086
  • 8
  • 22