/* * filterPalindrom.cpp * * Created on: 22 Nov 2013 * Author: Laura */ //--------------------------------------------------------------------------- #include #include using namespace std; void readData(int &n, string v[100]){ //reads a vector of strings //data: - (reading subalgorithm) //results: a vector of strings and its length cout << "no of elements, n = "; cin >> n; for(int i = 0; i < n; i++){ cout << "element from position " << i + 1 << " is: "; cin >> v[i]; } } void printData(int n, string v[100]){ //prints a vector of integers //data: a vector of strings and its length //res: - (printing subalgorithm) cout << "results: "; for(int i = 0; i < n; i++) cout << v[i] << " "; } bool isPalindrom(string s){ //checks if a string is palindrom or not //data: a string //res: true, if the string is palindrom // false, otherwise bool ok = true; int i = 0; while ((i < s.length()/2) && (ok == true)){ if (s[i] != s[s.length() - i - 1]) ok = false; else i = i + 1; } return ok; } void filterPalindrom(int n, string v[100], int &m, string p[100]){ //retains the palindroms of a vector //data: a vector of strings and its length //res: a vector of palindroms and its length m = 0; for(int i = 0; i < n; i++) if (isPalindrom(v[i]) == true){ p[m] = v[i];//p[m++]=v[i] m++; } } int main(){ //main program int noWords; string words[100]; int noPalindroms; string palindroms[100]; readData(noWords, words); filterPalindrom(noWords, words, noPalindroms, palindroms); printData(noPalindroms, palindroms); _getch(); return 0; } //---------------------------------------------------------------------------