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

Code and document the following functions using NON-RECURSIVE ITERATION only. Test the functions by calling them...
Code and document the following functions using NON-RECURSIVE ITERATION only. Test the functions by calling them from a simple interactive main() function using a menu, with different values used to select the choice of function. Overall, you should have one C program (call it Lab1.c) containing one main() function and 5 other functions, where the functions are called based on an interactive user menu. The program should contain a loop that permits users to enter a new choice of function...
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...
Problem: Write a C++ program that will implement and test the five functions described below that...
Problem: Write a C++ program that will implement and test the five functions described below that use pointers and dynamic memory allocation. The Functions: You will write the five functions described below. Then you will call them from the main function, to demonstrate their correctness. 1. minimum: takes an int array and the array's size as arguments. It should return the minimum value of the array elements. Do not use square brackets anywhere in the function, not even the parameter...
. 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...
In C++ prototype functions above "main" and define them below "main"; Write a program that uses...
In C++ prototype 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 of...
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT