Question

C++ Part1: Palindrome detector Write a program that will test if some string is a palindrome...

C++

Part1: Palindrome detector

Write a program that will test if some string is a palindrome

  1. Ask the user to enter a string
  2. Pass the string to a Boolean function that uses recursion to determine if something is a palindrome or not. The function should return true if the string is a palindrome and false if the string is not a palindrome.
  3. Main displays the result ( if the string is a palindrome or not

Part 2: Compute the Greatest Common Factor

Write a program that computes the GCF (Greatest Common Factor) of two positive whole numbers entered by the user. For example the GCF of 24 and 18 is 6. The program will prompt the user for entering two positive whole numbers and will then return their GCF. The user may terminate the program by entering -1 for the first number. The program should provide a recursive function to compute GCF.

Test the above for at least 4 pairs of values.

Mathematical Calculation:

GCF of two whole numbers can be calculated mathematically via repeated division. For example, GCF of 16 and 26 can be found as below:

1. Divide 16 into 26. The remainder is 10.

2. Divide 10 (the remainder in the last step) into 16 (the divisor in the last step). The remainder is 6.

3. Divide 6 (the remainder in the last step) into 10 (the divisor in the last step). The remainder is 4.

4. Divide 4 (the remainder in the last step) into 6 (the divisor in the last step). The remainder is 2.

5. Divide 2 (the remainder in the last step) into 4 (the divisor in the last step). The remainder is 0. Hence GCF is 2.

Note: You are to provide a recursive function to implement the above algorithm.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Note: Could you plz go through this code and let me know if u need any changes in this.Thank You
_________________

// Part#1:

#include <iostream>

#include <cctype>

using namespace std;

//function declarations

bool testPalindrome(string str);

string upperCaseIt(string s);

int main()

{

//Declaring variables

string str,str1;

char ch;

  

/* This while loop continues to execute

* until the user enters other than 'y' or 'Y'

*/

while(true)

{

str1="";

//Getting the input entered by the user

cout<<"\nEnter a string :";

getline(cin,str);

  

//eliminating spaces,commas..etc

for(int i=0;i<str.length();i++)

{

if(isalpha(str[i]))

str1+=str[i];

}

//calling the function

str1=upperCaseIt(str1);

//calling the function

if(testPalindrome(str1))

cout<<"This is a Palindrome"<<endl;

else

cout<<"This is not a Palindrome"<<endl;  

cout<<"\nDo you Want to continue(Y/N):";

cin>>ch;

if(ch=='y'||ch=='Y')

{

cin.ignore();

continue;

}else

{

break;

}

}

return 0;

}

/* This is a recursive function which calculates

* whether the String is Palindrome or not using recursive method

* Param:string

* return: boolean

*/

bool testPalindrome(string str) {

if(str.length() == 0 || str.length() == 1)

return true;

else if(str[0] == str[str.length()-1])

return testPalindrome(str.substr(1, str.length()-2));

  

return false;

}

// This function will convert the lower case to upper case

string upperCaseIt(string s)

{

for (int i = 0; i < s.size(); i++)

{

if (s.at(i) >= 97 && s.at(i) <= 122)

{

s.at(i) = s.at(i) - 32;

}

}

return s;

}

_____________________

Output:

_____________________________

// Part#2:

#include <iostream>
#include <iomanip>
using namespace std;
//Function Declarations
void getInput(int &a,int &b);
int gcf(int a,int b);
void display(int a,int b,int res);
int main(){
int a,b;
//Calling the functions

while(true)
{
getInput(a,b);
int res=gcf(a,b);
display(a,b,res);
  
}

  
return 0;
  
}
//This function will get the valid user inputs
void getInput(int &a,int &b)
{
while(true)
{
cout<<"\nEnter a:";
cin>>a;
if(a==-1)
{
   exit(0);
       }
else if(a<=0)
{
cout<<"Invalid.Must be >=0"<<endl;
}
else
break;
}
  
while(true)
{
cout<<"Enter b:";
cin>>b;
if(b<=0)
{
cout<<"Invalid.Must be >=0"<<endl;
}
else
break;
}
  
  
}
//This function will calcluate the gcd of two numbers
int gcf(int num1,int num2)
{
// % is modulus which is the remainder of a division
// base case
if ((num1 % num2) == 0) {
return num2;
}
// recursive case
else {
return gcf(num2, num1 % num2);
}
}
//This function will display the output
void display(int a,int b,int res)
{
cout<<"The GCF of "<<a<<" and "<<b<<" is :"<<res<<endl;
}
____________________________

Output:

_______________Could you plz rate me well.Thank You

Add a comment
Know the answer?
Add Answer to:
C++ Part1: Palindrome detector Write a program that will test if some string is a palindrome...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ PROGRAM ONLY! For this lab you need to write a program that will read in...

    C++ PROGRAM ONLY! For this lab you need to write a program that will read in two values from a user and output the greatest common divisor (using a recursive implementation of the Euclidean algorithm) to a file. In Lab #3, you implemented this program using an iterative method. Greatest Common Divisor In mathematics, the greatest common divisor (GCD) of two or more integers (when at least one of of them is zero then the larger value is the GCD....

  • C Program: 6.31 (Palindromes) A palindrome is a string that's spelled the same way forward and...

    C Program: 6.31 (Palindromes) A palindrome is a string that's spelled the same way forward and backward. Some examples of palindromes are: "radar" "able was i ere i saw elba" and, if you ignore blanks: "a man a plan a canal panama" Write a recursive function testPalindrome that returns 1 if the string stored in the array is a palindrome and 0 otherwise. The function should ignore spaces and punctuation in the string. Use the function in a complete program...

  • In Assembly Language Please display results and write assembler code in (gcd.s) The Euclidean algorithm is...

    In Assembly Language Please display results and write assembler code in (gcd.s) The Euclidean algorithm is a way to find the greatest common divisor of two positive integers, a and b. First let me show the computations for a=210 and b=45. Divide 210 by 45, and get the result 4 with remainder 30, so 210=4·45+30. Divide 45 by 30, and get the result 1 with remainder 15, so 45=1·30+15. Divide 30 by 15, and get the result 2 with remainder...

  • Write a complete C++ program that at least consists of the main() function and a recursive...

    Write a complete C++ program that at least consists of the main() function and a recursive function gcd() with return value. Define function gcd() with two and only two parameters that calculates the greatest common divider of two given parameters. Hint: use the difference between two parameters or the remainder obtained using one parameter divide the other. In main() Read 2 positive integers with proper prompt. Call gcd() with proper syntax. Display the result, i.e. the greatest common divider of...

  • help C programming Write a program that solves the problem described in Chapter 5 problem number...

    help C programming Write a program that solves the problem described in Chapter 5 problem number 5.39 from the text. A screen capture of the problem statement is given below: Example: This program finds the greatest common divisor of two positive whole numbers. Enter your first positive whole number: 324 Enter your second positive whole number: 42 The greatest common divisor of 324 and 42 is 6.

  • In the first task, you will write a Java program that accepts a string of the...

    In the first task, you will write a Java program that accepts a string of the format specified next and calculate the answer based on the user input. The input string should be of the format dddxxdddxx*, where d represents a digit and x represents any character and asterisk at the end represents that the string can have any number of characters at the end. 1. Prompt the user to enter a string with the specific format (dddxxdddxx*) 2. Read...

  • Using SPIM, write and test a program that finds the Greatest Common Divisor of two integers...

    Using SPIM, write and test a program that finds the Greatest Common Divisor of two integers using a recursive function that implements Euclid's GCD algorithm as described below. Your program should greet the user "Euclid's GCD algorithm", prompt the user to input two integers, and then output the result "Euclid's Greatest Common Divisor Algorithm" GCD(M,N) = M                      (if N is 0) GCD(M,N) = GCD(N, M % N)   (if N > 0) you may assume that inputs are non-negative name your assembly...

  • 1.) Write a recursive function named isPalindrome that will take a single string as an argument...

    1.) Write a recursive function named isPalindrome that will take a single string as an argument and determine if the argument is a palindrome. The function must return True if the argument is a palindrome, False otherwise. Recall that any string is a palindrome if its first and last characters are the same and the rest of it is also a palindrome (therefore, a single character is a palindrome): 2.) Consider a text file named samplestrings.txt that contains a collection...

  • FindGCF.py 1 #The Greatest Common Factor (GCF) of two numbers is the 2 #largest number that...

    FindGCF.py 1 #The Greatest Common Factor (GCF) of two numbers is the 2 #largest number that divides evenly into those two 3 #numbers. For example, the Greatest Common Factor of 48 4 #and 18 is 6. 6 is the largest number that divides evenly 5 #into 48 (48 / 6 = 8) and 18 (18 / 6 = 3). 6 # 7 #Write a function called find gcf. find gcf should have 8 #two parameters, both integers. find_gcf should return...

  • Write a program that uses a recursive function to determine whether a string is a character-unit...

    Write a program that uses a recursive function to determine whether a string is a character-unit palindrome. Moreover, the options for doing case sensitive comparisons and not ignoring spaces are indicated with flags. For example "A nut for a jar of tuna" is a palindrome if spaces are ignored and not otherwise. "Step on no pets" is a palindrome whether spaces are ignored or not, but is not a palindrome if it is case sensitive. Background Palindromes are character sequences...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT