Post: [OpenSource-C++] Palindrome Check
02-05-2011, 02:38 AM #1
Ritztro
I am a Game Developer
(adsbygoogle = window.adsbygoogle || []).push({}); This is a program that reverses a string and removes symbols to see if the string specified is a palindrome. I would like to see someone clean up this code and make functions that do all this for you so you can have minimal code in main. This is mostly a release but its also a practice for some to attempt to understand code then clean it up.

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

using namespace std;

int main()
{
string normal;
string normal_before_func;
string reverse;
string reverse_before_func;
string temp;
char ch;
int i = 0;
int w = 0;
int strcount = 0;

cout << "Please Enter A String: ";

getline(cin,normal);

normal_before_func = normal;

for(int w = normal.length()-1; w > -1; w--)
{
reverse_before_func.push_back(normal[w]);
}

while(i != normal.length())
{
switch(normal[i])
{
case ' ':
normal.erase(i,1);
i--;
break;

case '!':
normal.erase(i,1);
i--;
break;

case '.':
normal.erase(i,1);
i--;
break;

case ',':
normal.erase(i,1);
i--;
break;

case ';':
normal.erase(i,1);
i--;
break;
case ':':
normal.erase(i,1);
i--;
break;
case '(':
normal.erase(i,1);
i--;
break;
case '@':
normal.erase(i,1);
i--;
break;
case '#':
normal.erase(i,1);
i--;
break;
case '%':
normal.erase(i,1);
i--;
break;
}



i++;
}


ofstream out;
out.open("History.txt");
out.clear();
for(int n = normal.length()-1; n > -1; n--)
{
out << normal[n];
}
out.close();

ifstream in;
in.open("History.txt");
in >> reverse;
in.close();

string whathappened;

if(normal == reverse)
{
whathappened = "This is a palindrome!";
cout << "\n" << whathappened;
}
else
{
whathappened = "Not a Palindrome!";
cout << "\n" << whathappened;
}

out.open("History.txt");
out.clear();
out << "Original: ";
out << normal_before_func;
out << "\nReverse: ";
out << reverse_before_func;
out << "\nResult: " << whathappened;
out.close();

cin.get();


return 0;
}


---------- Post added at 06:38 PM ---------- Previous post was at 05:55 PM ----------

Here is my shot at improving this code and cleaning it up. Even though it is mine...


You must login or register to view this content.
You must login or register to view this content.
You must login or register to view this content.
(adsbygoogle = window.adsbygoogle || []).push({});
02-05-2011, 04:56 PM #2
Originally posted by Dutch View Post
This is a program that reverses a string and removes symbols to see if the string specified is a palindrome. I would like to see someone clean up this code and make functions that do all this for you so you can have minimal code in main. This is mostly a release but its also a practice for some to attempt to understand code then clean it up.



---------- Post added at 06:38 PM ---------- Previous post was at 05:55 PM ----------

Here is my shot at improving this code and cleaning it up. Even though it is mine...


You must login or register to view this content.
You must login or register to view this content.
You must login or register to view this content.


Ill take a look. :P

The following user thanked TheUberFail for this useful post:

Ritztro
02-05-2011, 09:04 PM #3
Ritztro
I am a Game Developer
Originally posted by TheUberFail View Post
Ill take a look. :P


Alright! Try and see is you can improve it.
02-05-2011, 09:13 PM #4
Originally posted by Dutch View Post
Alright! Try and see is you can improve it.


    #include <iostream>
#include <string>

using namespace std;

bool isPalindrome(string input)
{
string cleanReversedWord = ""; //to declare a string with no undefined garbage to add letters only from input
for(int i = (input.length()-1) ; i >= 0 ; i--)// starts at end of input;
{
if( (int(input[i]) > 64 && int(input[i]) < 91) || (int(input[i]) > 96 && int(input[i]) < 123))
cleanReversedWord += input[i]; // adds only letters to cleanWord in reverse order
}

if(input == cleanReversedWord)
return true;
else
return false;
}

int main()
{
string input; // to hold the input word before symbols removed

cout<<"\n\tEnter a word to check for palimdrome: ";
getline(cin,input); //gets input

if(isPalindrome(input))
cout<<"\n\tThis word is a palindrome!"<<endl;
else
cout<<"\n\tThis word is not a palindrome!"<<endl;

getchar();

return 0;
}


Can be easily converted to header file.

Btw why did you have to write to a file? i might of missed the purpose of that.
02-06-2011, 02:42 AM #5
Ritztro
I am a Game Developer
Originally posted by TheUberFail View Post
    #include <iostream>
#include <string>

using namespace std;

bool isPalindrome(string input)
{
string cleanReversedWord = ""; //to declare a string with no undefined garbage to add letters only from input
for(int i = (input.length()-1) ; i >= 0 ; i--)// starts at end of input;
{
if( (int(input[i]) > 64 && int(input[i]) < 91) || (int(input[i]) > 96 && int(input[i]) < 123))
cleanReversedWord += input[i]; // adds only letters to cleanWord in reverse order
}

if(input == cleanReversedWord)
return true;
else
return false;
}

int main()
{
string input; // to hold the input word before symbols removed

cout<<"\n\tEnter a word to check for palimdrome: ";
getline(cin,input); //gets input

if(isPalindrome(input))
cout<<"\n\tThis word is a palindrome!"<<endl;
else
cout<<"\n\tThis word is not a palindrome!"<<endl;

getchar();

return 0;
}


Can be easily converted to header file.

Btw why did you have to write to a file? i might of missed the purpose of that.


Nice! I Like how you remove the symbols based on their ascii codes.
Also the file thing was just for fun.
02-06-2011, 10:27 AM #6
Originally posted by Dutch View Post
Nice! I Like how you remove the symbols based on their ascii codes.
Also the file thing was just for fun.


I see :P, btw did you check out my log in system?

Copyright © 2026, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo