Question

In: Computer Science

c++ using recursive no loops (for ,while ..ect)not allowed Write a recursive function ‘bool palindrome(string s)’...

c++
using recursive
no loops  (for ,while ..ect)not allowed

Write a recursive function ‘bool palindrome(string s)’ that returns true if s is a palindrome and false if not.


#5: Write a recursive function 'void reverse(string &word)' that reverses the given input string.

string name = "damian";
reverse(name);
cout << name << endl;  //should display "naimad".



#7: Write a function 'int numTwos(int n)' which returns the number of 2's in the base-4 expansion of n.

cout << numTwos(2170) << endl; // 3

Solutions

Expert Solution

Check whether string is Palindrome or not code-

#include <iostream>
using namespace std;
bool isPalRec(string str, 
                        int s, int e) 
{ 

        if (s == e) 
        return true; 

        // If first and last characters do not match 
        if (str[s] != str[e]) 
        return false; 

        // If there are more than two characters, check if middle substring is also palindrome or not. 
        if (s < e + 1) 
        return isPalRec(str, s + 1, e - 1); 

        return true; 
} 

bool isPalindrome(string str) 
{ 
int n = str.length(); 

// An empty string is considered as palindrome 
if (n == 0) 
        return true; 

return isPalRec(str, 0, n - 1); 
} 

int main() 
{ 
        string str; 
        cin>>str;
        
        if(isPalindrome(str))
            cout<<"True";
        else
            cout<<"False";

        return 0; 
} 

Output

Reverse of string code

#include <iostream>
using namespace std;

void reverse_String(string& s, int n,int i){

  if(n<=i)
    return;

  swap(s[i],s[n]);
  reverse_String(s,n-1,i+1);

}

int main() {
  
  string str;
  cin>>str;
  int n= str.length();
  reverse_String(str,n-1,0);
  cout<<"reversed string is :"<<str;
  
}

Output

number of 2's in the base-4 expansion of n code-
#include <iostream> 
using namespace std; 

int base4(int n) 
{ 
 
        int rem,count=0; 

        int i = 0; 
        while (n > 0) { 
        rem = n % 4; 
                n = n / 4;
                if(rem==2)
                    count++;
        } 
        return count;
} 

int main() 
{ 
        int n;
        cin>>n;
        cout<<base4(n); 
        return 0; 
} 

Output

Recursive code

 
#include <bits/stdc++.h> 
using namespace std; 

int recusion_base4(int x) 
{ 
        if (x == 0) 
                return 0; 
        else
                return (x % 4 + 10 * recusion_base4(x / 4)); 
} 


int main() 
{ 
        int n;
        cin>>n;
        int ans= recusion_base4(n);
        int count=0;
        while(ans>0)
        {
            int rem=ans%10;
            ans= ans/10;
            if(rem==2)
                count++;
        }
        cout<<count;
        return 0; 
} 

Please upvote my answer


Related Solutions

Write a short recursive C++ function that determines if a string s is a palindrome, that...
Write a short recursive C++ function that determines if a string s is a palindrome, that is, it is equal to its reverse. For example,"racecar" and "gohangasalamiimalasagnahog" are palindromes. Please include the pseudo code so that I can understand better with simple English as much as possible.
Write a recursive method to determine if a String is a palindrome. Create a String array...
Write a recursive method to determine if a String is a palindrome. Create a String array with several test cases and test your method. Write a recursive method to determine if a String is a palindrome. Create a String array with several test cases and test your method. In Java
C# Palindrome Permutation: Given a string, write a function to check if it is a permutation...
C# Palindrome Permutation: Given a string, write a function to check if it is a permutation of a palindrome. A palindrome is a word or phrase that is the same forwards and backwards. A permutation is a rearrangement of letters. The palindrome does not need to be limited to just dictionary words. Input: Tact Coa Output: True (permutations: "taco cat", "atco cta", etc.) Comment your code to explain it.
Write a recursive method to determine if a String is a palindrome. The program should contain...
Write a recursive method to determine if a String is a palindrome. The program should contain a String array that you can load several test cases to test your palindrome testing method. The program should load your String Array with the test data containing the possible palindromes from a text file. The program should test you application with a text file contained in your project folder After testing your program with your test cases in the text file, you program...
Write a recursive method to determine if a String is a palindrome. The program should contain...
Write a recursive method to determine if a String is a palindrome. The program should contain a String array that you can load several test cases to test your palindrome testing method. The program should load your String Array with the test data containing the possible palindromes from a text file. The program should test you application with a text file contained in your project folder After testing your program with your test cases in the text file, you program...
Write a recursive Racket function "remove-char" that takes two string parameters, s and c, and evaluates...
Write a recursive Racket function "remove-char" that takes two string parameters, s and c, and evaluates to string s with all occurrences of c removed. The string c is guaranteed to be a length-1 string; in other words a single character string. For example (remove-char "abc" "b") should evaluate to "ac". Here is pseudocode that you could implement.
Write a recursive method using Java that takes a string s as input and returns a...
Write a recursive method using Java that takes a string s as input and returns a list that contains all the anagrams of the string s. An anagram is a word formed by rearranging the letters of a different word. For instance, the word ‘cat’ is an anagram of ‘act’. Notice that the output list cannot contain duplicates.
#include <string> using namespace std; //using recursion no loops allowed int main() { double nums[] =...
#include <string> using namespace std; //using recursion no loops allowed int main() { double nums[] = { 13.8, 2.14, 51, 82, 3.14, 1.7, 4.89, 18, 5, 23.6, 17, 48, 5.6 };   //Challenge #2: print the list from given range   printList(nums, 0, 12); //13.8 2.14 51 .... 48 5.6   cout << endl;   //Challenge #3: print the list, but backwards   printReverse(nums, 0, 12); //5.6 48 17 ... 2.14 13.8   cout << endl;                  //Challenge #4: reverse order of items in list   reverse(nums,...
Write a recursive method repeatNTimes(String s, int n) that accepts a String and an integer as...
Write a recursive method repeatNTimes(String s, int n) that accepts a String and an integer as two parameters and returns a string that is concatenated together n times. (For example, repeatNTimes ("hello", 3) returns "hellohellohello") Write a driver program that calls this method from the Main program. Need code in c#.
In Java, write a recursive function that accepts a string as its argument and prints the...
In Java, write a recursive function that accepts a string as its argument and prints the string in reverse order. Demonstrate the function in a driver program.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT