COP3530/Project8/Project8.cpp
2023-07-12 02:25:28 -04:00

55 lines
1.7 KiB
C++

// Corey Williams
// COP3530 01Z
// Project 8 Recursion
/*
Write a C/C++ or Java application that uses a "vector" container to store 10 strings and a recursive function/method
"isPalindrome" (i.e., must be written) to determine whether a string argument is a palindrome.
The application must perform the following.
- Prompt the user for the 10 strings, storing in a "vector" container
- Uses a loop to call "isPalindrome" passing it one of the strings from the "vector"
- Display message with the string and results of the palindrome test
*/
#include <iostream>
#include <vector>
#include <string>
using namespace std;
//Takes arguments of string vector by reference and element to check.
void isPalindrome(vector<string>& strVec, vector<string>::iterator i) {
//Makes copy of vector string element being tested and reverses
string revStr = *i;
reverse(revStr.begin(), revStr.end());
//If the reversed string is true, this means the string is a palidrome (same forward and backward). Prints test string and result.
cout << endl << "String:\t" << *i << "\tIs Palindrome: ";
if (*i == revStr) { cout << "TRUE"; }
else { cout << "FALSE"; }
//increments iterator, tests if there are more elements, and calls self again if there are more elements.
if (++i != strVec.end()) {
isPalindrome(strVec, i);
}
}
int main()
{
vector<string> userInputs;
string tmpStr;
//Collects user inputs into a vector of strings
for (int i = 1; i <= 10; i++) {
cout << "Enter test string " << i << " of 10: ";
cin >> tmpStr;
userInputs.push_back(tmpStr);
}
//Passes in vector object by reference and iterator pointed to first element.
isPalindrome(userInputs, userInputs.begin());
}