hey i'm quite new programming , i'm having trouble using isalpha function in programme. part of code palindrome class. i'm trying remove non alphabetic characters input. if user inputs "hi, how you" need first count size of array of letters in removenonletters subclass, need rid of non alphabetical characters. can please me this. thank much!
#include <iostream> #include <string> #include <stdio.h> #include <algorithm> #include <cctype> #include <cstring> #include <ctype.h> using namespace std; class palindrome { private: int only_letters_size; string input_phrase; string* only_letters; public: string inputphrase(); string removenonletters(); string* new_array; int size_new_array; }; string palindrome::inputphrase() { cout << "input phrase: "; //asks user input getline(cin,input_phrase); size_new_array = input_phrase.length(); //creating dynamic array store input phrase new_array = new string[size_new_array]; int i; (i=0; i<size_new_array; i++) { new_array[i]=input_phrase[i]; } only_letters_size = 0; while(new_array[i]) { if (isalpha(new_array[i])) //problem occurs here { only_letters_size=only_letters_size+1; } } cout << only_letters_size << endl; return new_array; } string palindrome::removenonletters() { int j=0; int str_length = new_array.length(); //string length only_letters = new string[only_letters_size]; (int i=0;i<size_new_array;i++) //problem occurs here { if (isalpha(new_array[i]))//a command checks characters { only_letters[j] = new_array[i];//word without non alphabetical c characters stored new variable j++; } } cout << only_letters << endl; return only_letters; }
i've found best way determine if string palindrome walk toward center both sides. in case opt skip non-alpha characters so.
bool is_palindrome(string mystring) { int start = 0, end = mystring.length() - 1; while (start < end) { // skip on non-alpha characters while (!isalpha(mystring[start])) { start++; } while (!isalpha(mystring[end])) { end--; } if (tolower(mystring[start]) != tolower(mystring[end])) { return false; } else { start++; end--; } } return true; }
if must save input first , remove nonalpha characters, this.
string remove_non_alpha(string mystring) { string ret_string = ""; (int = 0; < mystring.length(); i++) { if (isalpha(mystring[i])) { ret_string += tolower(mystring[i]); } } return ret_string; }
and feed result above function.
Comments
Post a Comment