Question: In C++, I need help modifying my program to read candidate palindromes from the file palindromes.txt, one per line, and answer questions about the contents
In C++, I need help modifying my program to read candidate palindromes from the file palindromes.txt, one per line, and answer questions about the contents of that file. Below is my current code.
Here is the palindrome.txt file I need to read
racecar racecar!!! Race car TACO CAT Able was I, ere I saw Elba.... A man, a plan, a canal - Panama!!! King, are you glad you are king? Al lets Della call Ed "Stella." Was it a car or a cat I saw? Dennis, Nell, Edna, Leon, Nedra, Anita, Rolf, Nora, Alice, Carol, Leo, Jane, Reed, Dena, Dale, Basil, Rae, Penny, Lana, Dave, Denny, Lena, Ida, Bernadette, Ben, Ray, Lila, Nina, Jo, Ira, Mara, Sara, Mario, Jan, Ina, Lily, Arne, Bette, Dan, Reba, Diane, Lynn, Ed, Eva, Dana, Lynne, Pearl, Isabel, Ada, Ned, Dee, Rena, Joel, Lora, Cecil, Aaron, Flora, Tina, Arden, Noel, and Ellen sinned. afd8792h34af7980897fa43h2978dfa afd879 2h34af798 0897fa4 3h2 978dfa afd8792h34af79380897fa43h2978dfa sleep faced time sap fay new evil live wen yag pas emit decaf peels sleep ogre stoops secret stink state amore real room know man nam wonk moor laer roma etats knits terces spoots ergo peels Devil time spins real fool secret trap part terces loof laer snips emit lived looks sense, animal liar bibrank some emos knar bib rail lamina esnes skool! stink rats state sense sleep know reined animal time Devil lived emit terces lamina denier wonk peels esnes etats star knits animal spins real edit man trap spinx secret terces nips part nam tide laer snips lamina snow looks leat sleep deep edit state Devil recal slit rats startils lacer lived etats tide peed peels tael skool wons fool animal edit time sense trap saw word drow was part esnes emit tide lamina loof secret poor edit sense keep sleep fool big real deep peed laer gib loof peels peek esnes tide roop terces How are you going to do on this quiz ????? ziuq siht no od ot gniog uoy era woH STRICT_ TCIRTS xxx12321xxx
Here is my Code
#include
using namespace std; //method to check a number as digit or not bool isNumeric(string s) { //loop to scan each character of string for (int i = 0; i < s.length(); i++) if (isdigit(s[i]) == false) //check for digit return false;
return true; }
//recursive method to check palindrome bool Palindrome(string& str, int low, int up) { if (low >= up) //base condition return true; if (str[low] != str[up]) //condition for non palindrome return false; //recursively call to mathod return Palindrome(str, ++low, --up); }
//method to convert into uppercase character string convertUpper(string str) { int i; for (i = 0; i < str.length(); i++) if (str[i] >= 'a' && str[i] <= 'z') //condition for lower case characters str[i] = str[i] - 32; //convert into uppercase return str; //return the string with uppercase characters } //method to remove the punctuations string RemovePunctuation(string str) { int i; string s; //loop to scan each characters of the string for (i = 0; i < str.length(); i++) //condition to leave the symbols other than alphabet and space if (str[i] >= 'A' && str[i] <= 'Z' || str[i] == ' ') s = s + str[i]; //append the characters to s return s; //return the string without punctuation symbols } //method to remove the spaces from the string string RemoveSpace(string str) { int i; string s; //loop to scan each characters of string for (i = 0; i < str.length(); i++) if (str[i] != ' ') //condition to left the spaces s = s + str[i]; //append the characters of str except white spaces return s; //return the string without space } //driver program int main() { string str, s; bool first, second, num; cout << endl << "Enter a string "; getline(cin, str); //read the string //condition for numeric strings if (isNumeric(str)) { if (Palindrome(str, 0, str.length() - 1)) cout << endl << "It is a decimal palindromic number"; else cout << endl << "Not palindrome"; } else { cout << endl << "Entered string is : " << str; s = convertUpper(str); //convert the characers of str into uppercase s = RemovePunctuation(s); //update the s by removing the punctuations
//check for palindrome first = Palindrome(s, 0, s.length() - 1); //update s by removig the space from it s = RemoveSpace(s); //check for palindrome second = Palindrome(s, 0, s.length() - 1);
if (first) cout << endl << "The string is strict Palindrome"; else if (second) cout << endl << "The string is ordinary Palindrome"; else cout << endl << "The string is Not Palindrome"; } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
