Question

In: Computer Science

Using the string functions below, write new functions to do the following, and test them in...

Using the string functions below, write new functions to do the following, and test them in your main() function:

  • Determine whether the first or last characters in the string are any of the characters a, b, c, d, or e.
  • Reverse a string
  • Determine whether a string is a palindrome (spelled the same way forward or backward

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++

Solutions

Expert Solution

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


Related Solutions

For these of string functions, write the code for it in C++ or Python (without using...
For these of string functions, write the code for it in C++ or Python (without using any of thatlanguage's built-in functions) You may assume there is a function to convert Small string into the language string type and a function to convert your language's string type back to Small string type. 1. int [] searchA,ll(string in...str, string sub): returns an array of positions of sub in in...str or an one element array with -1 if sub doesn't exist in in...str
Assume the following functions have already been defined, write a main() using c++ that uses them...
Assume the following functions have already been defined, write a main() using c++ that uses them to fill a vector with random integers, remove the smallest and largest integers from the vector, save the result in a file called "trimmed.txt" void fillRandom(vector & nums, int howMany); // fill with specified number of integers int minValue(vector nums); // return smallest value in vector int maxValue(vector <;int> nums); // return largest value in vector void writeFile(string outFileName, vector nums ); // writes...
. Write a program which encodes any string using the XOR instruction.  Test it using your <first...
. Write a program which encodes any string using the XOR instruction.  Test it using your <first name last name> in the data segment to produce cipher text and then decode using the program to get plain text.  Use the last two digits of your student id as the key.  Print plane text from the data segment, print the cipher text, and then print the plain text upon execution.  Submit the asm/list file and screenshots that shows the output of your code. What are...
Write the following MATLAB functions. Run it on an example and test it and confirm that...
Write the following MATLAB functions. Run it on an example and test it and confirm that it gives correct results. Show all results. function x=naivege(a,b) that returns the solution to Ax=b obtained by Gauss Elimination without pivoting
In C++ Prototype your functions above "main" and define them below "main"; Write a program that...
In C++ Prototype your functions above "main" and define them below "main"; Write a program that uses two identical arrays of at least 20 integers. It should call a function that uses the bubble sort algorithm to sort one of the arrays in ascending order. The function should keep count of the number of exchanges it makes. The program then should call a function that uses the selection sort algorithm to sort the other arrays. It should also keep count...
Write an algorithm to create seven subroutines (functions) described in the following and call them in...
Write an algorithm to create seven subroutines (functions) described in the following and call them in the main function To generate 100 random numbers between 1-100 in a randomData.txt file To read the 100 random numbers from randomData.txt and store them in an array Print the data in the array Find the smallest and the largest of the random numbers and their array position Insert an element of value100 in the 51th position of the array Delete all the elements...
Write Java code that prompts the user for a string and tells them if the grouping characters in that string are balanced.
Write Java code that prompts the user for a string and tells them if the grouping characters in that string are balanced. Grouping characters are ( and ), [ and ], and { and }. I got the basic idea for this problem from HackerRank. It’s also a very common interview question.*******************8Example output**************Enter the expression:{[()]} {[()]}is balancedEnter the expression: {()()} {()()}is balancedEnter the expression: {([[])} {([[])}is not balancedEnter the expression: {([)]} {([)]}is not balanced
# Write three functions to return the pieces of a string containing # a city, state,...
# Write three functions to return the pieces of a string containing # a city, state, and zip code. # This function needs a better comment def get_zip(city_state_zip):    return city_state_zip[-1] # This function needs a better comment def get_city(city_state_zip): return city_state_zip[0:2] # This function needs a better comment def get_state(city_state_zip):    return 'Confusion' def main():    place1 = 'Rolla, MO 65402'    place2 = 'Cape Girardeau,     MO 63780'    place3 = 'St. Louis,MO63111'    place4 = 'International Falls,MN               56650     '   # Print the individual components   ...
Write a function that counts the colors in a string using JavaScript. String "The quick brown...
Write a function that counts the colors in a string using JavaScript. String "The quick brown fox jumped the blue fence and landed on red paint." This should return the number of colors. The colors you are looking for are "blue, green, brown, gray, black, brown, red, purple".
-Write in C++ -Use Char library functions Write a function that accepts a string representing password...
-Write in C++ -Use Char library functions Write a function that accepts a string representing password and determines whether the string is a valid password. A valid password as the following properties: 1. At least 8 characters long 2. Has at least one upper case letter 3. Has at least one lower case letter 4. Has at least one digit 5. Has at least on special character
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT