Question

In: Computer Science

using recursion no loops allowed(for,while..ect) not allowed #4: Write a recursive function ‘bool palindrome(string s)’ that...

using recursion
no loops allowed(for,while..ect) not allowed

#4: 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".

#6: Write a function 'string binary(int n)' that returns a string of 1's and 0's which is the binary representation of n.

cout << binary(43) << endl; // 101011

#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

c++

Solutions

Expert Solution

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

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

// recursive function which
// check if s is
// palindrome or not.
bool check(char s[],
                        int start, int end)
{

        // If there is only one character
        if (start == end)
        return true;

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

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

   return true;
}

bool palindrome(char s[])
{
        int length = strlen(s);

        if (length == 0)
                  return true;

        return check(s, 0, length - 1);
}

// Driver Code
int main()
{
        char s[] = "hellolleh";

        if (palindrome(s))
        cout << "true";
        else
        cout << "false";

        return 0;
}

Write a recursive function 'void reverse(string &word)' that reverses the given input string.
#include <iostream>
#include <algorithm>
using namespace std;

// recursive function to reverse a given string note string is passed as reference parameter
void reverse(string &str, int p)
{
        static int i = 0;

        // if we have reached the end of the string
        if (p == str.length())
                return;

        reverse(str, p + 1);

        if (i <= p)
                swap(str[i++], str[p]);
}

// Reverse given string using Recursion
int main()
{
        string str = "damian";

        reverse(str, 0);
        cout<< str;

        return 0;
}
Write a function 'string binary(int n)' that returns a string of 1's and 0's which is the binary representation of n.
 #include <bits/stdc++.h>
using namespace std;

// decimal to binary conversion using recursion
int binary(int number)
{
        if (number == 0)
                return 0;
        else
                return (number % 2 + 10 *
                                binary(number / 2));
}

int main()
{
        cout << binary(43);
        return 0;
}

Write a function 'int numTwos(int n)' which returns the number of 2's in the base-4 expansion of n.
// C Program to convert decimal to any given base
#include<iostream>
#include <stdio.h>
#include <string.h>
using namespace std;

int c=1;
// return char for a value. For example '2' is returned for 2. 'A' is returned for 10. 'B' for 11
char val(int n)
{
        if (n >= 0 && n <= 9)
                return (char)(n + '0');
        else
                return (char)(n - 10 + 'A');
}

// Utility function to reverse a string
void strev(char *str)
{
        int len = strlen(str);
        int i;
        for (i = 0; i < len/2; i++)
        {
                char temp = str[i];
                str[i] = str[len-i-1];
                str[len-i-1] = temp;
        }
}

// Function to convert a given decimal number
// to a base 'base' and
char* fromDeci(char res[], int base, int inputNum)
{
        int index = 0; // Initialize index of result

        // Convert input number is given base by repeatedly
        // dividing it by base and taking remainder
        while (inputNum > 0)
        {
                res[index++] = val(inputNum % base);
                inputNum /= base;
        }
        res[index] = '\0';

        // Reverse the result
        strev(res);

        return res;
}

int numTwos(int num)
{
    if(num==0)
        return c;
    else
    {
        if(num%10==2)
            c++;
        num=num/10;
        numTwos(num);
    }

}

// Driver program
int main()
{
        int num1 = 2170, base = 4;
        char res[100];
    char* num=fromDeci(res, base, num1);
    cout<<num;
    int x=(int)(num);
    cout<<endl;
    cout<<"number of 2's: "<< numTwos(x);
        return 0;
}

Related Solutions

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; //...
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.
In C++ Valid Palindrome In this assignment, you need to implement a bool isPalindrome(string s) function....
In C++ Valid Palindrome In this assignment, you need to implement a bool isPalindrome(string s) function. Given a string s, isPalindrome(s) can determine if s is a palindrome, considering only alphanumeric characters and ignoring cases. Note: for the purpose of this problem, we define empty string as valid palindrome. Example 1: Input: ”A man, a plan, a canal: Panama” Output: true Example 2: Input: ”race a car” Output: false Requirement: There are many methods to check if a string is...
#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 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
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...
Using Matlab: Using nested for loops (other methods are not allowed), write a function which takes...
Using Matlab: Using nested for loops (other methods are not allowed), write a function which takes as input a matrix, and as output returns true if at least one entry in the matrix is strictly greater than 8 and returns false otherwise.
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.
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT