In: Computer Science
Using the string functions below, write new functions to do the following, and test them in your main() function:
FUNCTIONS REFERENCE:
string myString = "hello"; // say we have a string…
// … we can call any of the following
// string member functions on it.
myString[x] // get element at position x
myString.back() // get last element
myString.front() // get first element
myString.substr(first, last) // get a substring of the string, from index first to index last. // (including first, not including last)
myString. find(str) // find the index of the first occurrence of str in the string.
// (can also use a character rather than a string for the parameter)
(not member functions, but useful)
toupper(str) // convert the string str to uppercase
tolower(str) //…. lowercase
What I have so far:
#include <iostream>
#include "string"
using namespace std;
bool checkFirstAndLast(string myString, char
characterCheck)
{
if(myString.front() == characterCheck && myString.back() ==
characterCheck)
return true;
else
return false;
  
}
int main()
{
char lettersToCheck[5]=
{
'a','b','c','d','e'
};
  
string something = "buuperb";
bool firstEqLast=false;
  
for(int i=0;i<5;i++)
{
if (checkFirstAndLast(something, lettersToCheck[i]))
firstEqLast=true;
  
  
}
  
cout<< checkFirstAndLast(something,'a') <<endl;
cout<< checkFirstAndLast(something,'b') <<endl;
cout<< checkFirstAndLast(something,'c') <<endl;
cout<< checkFirstAndLast(something,'d') <<endl;
cout<< checkFirstAndLast(something,'e') <<endl;
  
return 0;
}
Language is in C++


Code
#include <iostream>
#include <string>
using namespace std;
bool checkPalindrome(string myString)
{
   int i = 0;
   int j = myString.length() - 1; // j will be having
last character index
  
   while (i < j)
   {
       if (toupper(myString[i]) !=
toupper(myString[j])) // toupper for case insensitive
comparison
           return
false;
       i++;
       j--;
   }
   return true;
}
string reverseString(string myString)
{
   int i = 0;
   int j = myString.length() - 1; // j will be having
last character index
   while (i < j)
   {
       char temp = myString[j];
       myString[j] = myString[i];
       myString[i] = temp;
       i++;
       j--;
   }
   return myString;
}
bool checkFirstAndLast(string myString, char
characterCheck)
{
   if (myString.front() == characterCheck &&
myString.back() == characterCheck)
       return true;
   else
       return false;
}
int main()
{
   char lettersToCheck[5] =
   {
       'a','b','c','d','e'
   };
   string something = "buuperb";
   bool firstEqLast = false;
   for (int i = 0; i<5; i++)
   {
       if (checkFirstAndLast(something,
lettersToCheck[i]))
           firstEqLast =
true;
   }
   cout << "String \"buuperb\" has 'a' as first
and last character: "<<checkFirstAndLast(something, 'a')
<< endl;
   cout << "String \"buuperb\" has 'b' as first and
last character: "<< checkFirstAndLast(something, 'b')
<< endl;
   cout << "String \"buuperb\" has 'c' as first and
last character: " << checkFirstAndLast(something, 'c')
<< endl;
   cout << "String \"buuperb\" has 'd' as first and
last character: " << checkFirstAndLast(something, 'd')
<< endl;
   cout << "String \"buuperb\" has 'e' as first and
last character: " << checkFirstAndLast(something, 'e')
<< endl;
   // Palindrome test cases
   cout << "String \"Hello\" is Palindrome: "
<< checkPalindrome("Hello")<< endl;
   cout << "String \"ABCDEDCBA\" is Palindrome: "
<< checkPalindrome("ABCDEDCBA") << endl;
   cout << "String \"geeks\" is Palindrome: "
<< checkPalindrome("geeks") << endl;
   cout << "String \"Radar\" is Palindrome: "
<< checkPalindrome("Radar") << endl;
   // String Reverse test cases
   cout << "Reverse string for \"Hello\" is: "
<< reverseString("Hello") << endl;
   cout << "Reverse string for \"ABCDEDCBA\" is: "
<< reverseString("ABCDEDCBA") << endl;
   cout << "Reverse string for \"geeks\" is: "
<< reverseString("geeks") << endl;
   cout << "Reverse string for \"Radar\" is: "
<< reverseString("Radar") << endl;
   return 0;
}
Test Output
