Fix the function so that it checks if the string is a palindrome
#include <iostream>
using namespace std;
//Fix the function so that it checks if the string is a
palindrome
//(same forwards as backwards ex: racecar / radar)
bool is_palindrome(string str, int i){
//base cases
if (/* add the condition */) return false;
if (/* add the condition */) return true;
//recursive call
return is_palindrome(str, i-1);
}
int main(){
string x;
cin >> x;
if (is_palindrome(x,x.length())){
cout << x << " is a palindrome";
} else {
cout << x << " is not a palindrome";
}
}
#include <iostream>
using namespace std;
//Fix the function so that it checks if the string is a
palindrome
//(same forwards as backwards ex: racecar / radar)
bool is_palindrome(string str, int i){
//base cases
if (str[str.length()-i] != str[i-1]) return false;
if (i == 0 || i == 1) return true;
//recursive call
return is_palindrome(str, i-1);
}
int main(){
string x;
cin >> x;
if (is_palindrome(x,x.length())){
cout << x << " is a palindrome";
} else {
cout << x << " is not a palindrome";
}
}
_________________________
Output:
_______________Could you plz rate me well.Thank You
Fix the function so that it checks if the string is a palindrome #include <iostream> using...
***************Fix code recursive function #include <iostream> #include <cctype> #include <string> using namespace std; void printUsageInfo(string executableName) { cout << "Usage: " << executableName << " [-c] [-s] string ... " << endl; cout << " -c: turn on case sensitivity" << endl; cout << " -s: turn off ignoring spaces" << endl; exit(1); //prints program usage message in case no strings were found at command line } string tolower(string str) { for(unsigned int i = 0; i < str.length(); i++)...
Fix this code so only the function prototype comes before main. #include <iostream> using namespace std; bool isMultiple(int num1, int num2) { return num1 % num2 == 0; } int main() { char ch = 'Y'; int num1, num2; while(ch =='Y') // While ch is equal to Y { cout << "Enter two numbers(largest first): "; cin >> num1; // Getting 1st number cin >> num2; // Getting 2nd number if(isMultiple(num1, num2)) cout << num2 << " " << "IS...
Fix the following code to print whether the input is positive or negative #include <iostream> #include <string> using namespace std; void positiveOrNegative(int); // declare function int main(){ int y; int result; cin >> y; result = positiveOrNegative(y); return 0; } void positiveOrNegative(int x){ if (x>=0) cout << "Positive"; else cout << "Negative"; }
#include <iostream> #include <string> using namespace std; //Write a function that changes all characters in a string to dashes string to_dash(string s){ for(int i = 0; i < s.length(); i++){ } return s; } int main(){ string s; cin >> s; s = to_dash(s); cout << s << endl; }
Question 1: Fix the 2D dynamic array initialization in following code #include <iostream> using namespace std; int main(){ int rows = 5; int cols = 5; int x; int** arr = new int[rows][cols] cin >> x; arr[x][x] = x; cout << "arr[x][x] = " << arr[x][x]; return 0; } Question 2: Fix the code to initialize the 2D array elements to x #include <iostream> using namespace std; int main(){ int rows; int cols; int x;...
For this assignment, you will create a program that tests a string to see if it is a palindrome. A palindrome is a string such as “madam”, “radar”, “dad”, and “I”, that reads the same forwards and backwards. The empty string is regarded as a palindrome. Write a recursive function: bool isPalindrome(string str, int lower, int upper) that returns true if and only if the part of the string str in positions lower through upper (inclusive at both ends) is...
Write a function check palindrome, which takes a string x as argument and which returns True if x is a palindrome, and False otherwise. A palindrome is a word that reads the same backwards as forwards (like for example “racecar”). Your function should be recursive (i.e. call itself) and proceed as follows. 1. If x is a string of length 0 or 1 return True. 2. If the rst and the last letter of x are di erent, return False....
what is the output for the following code? explain the steps. /*#include <iostream> using namespace std; int f(int &i) { i = 10; return(5 * i); } int main() { int n = 5; f(n); cout << n << "\n"; return 0; } #include <iostream> using namespace std; int sub1(int n) { n--; return n; } int main() { int m = 10; for(int j = 0; j < 10; j++) m -= sub1(j); cout << m << "\n"; return...
I have a queue and stack class program that deals with a palindrome, I need someone to help to put in templates then rerun the code. I'd greatly appreciate it. It's in C++. Here is my program: #include<iostream> #include<list> #include<iterator> #include<string> using namespace std; class Queue { public: list <char> queue; Queue() { list <char> queue; } void Push(char item) { queue.push_back(item); } char pop() { char first = queue.front(); queue.pop_front(); return first; } bool is_empty() { if(queue.empty()) { return...