Question

1. DOES A DIGIT APPEAR IN AN INTEGER, Write a recursive function appears(n,i) that returns true...

1. DOES A DIGIT APPEAR IN AN INTEGER, Write a recursive function appears(n,i) that returns true if the digit i appears in the positive (base 10) integer n, false if it does not in c++. ex.

Enter a positive integer: 54321
          Enter a digit: 7
          The digit 7 does NOT appear in the integer 54321.

2. IS AN INPUT STRING A PALINDROME? A palindrome is a string in which the letters are the same in both directions, disregarding capitalization and non-letter characters. Write a program that reads a sequence of characters and calls a recursive Boolean function to determine if the letters in the string form a palindrome in c++. example:

csh> pal
      Enter a line that might be a palindrome:
      Madam I'm Adam.
      This string IS a palindrome.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

1)

#include <bits/stdc++.h>

using namespace std;

bool appears(int n, int i)

{

// Base case

if(n < 10)

{

return (n == i);

}

// If last digit is i return true

if(n%10 == i)

return true;

// Else recur for n/10

return appears(n/10, i);

}

int main()

{

cout << "3 appears in 1234: " << appears(1234, 3) << " 6 appears in 1234: " << appears(1234, 6);

return 0;

}

Output :

3 appears in 1234: 1 6 appears in 1234: 0

2)

#include <bits/stdc++.h>

using namespace std;

bool palindrome(char str[], int s, int e)

{

// Base case

if(s == e || s + 1 == e)

return true;

// If start is special character then increment s

while((str[s] < 'a' || str[s] > 'z') && (str[s] < 'A' || str[s] > 'Z'))

s++;

// If end is special character then decrement e

while((str[e - 1] < 'a' || str[e - 1] > 'z') && (str[e - 1] < 'A' || str[e - 1] > 'Z'))

e--;

// If start and last are unequal then return false

if(tolower(str[s]) != tolower(str[e - 1]))

return false;

// Otherwise recur

return palindrome(str, s + 1, e - 1);

}

int main()

{

char str[100];

cout << "Enter the string ";

cin >> str;

if(palindrome(str, 0, strlen(str)))

cout << "Palindrome ";

else

cout << "Not Palindrome ";

return 0;

}

Output:

Enter the string Aba Palindrome PS C:\Users Aashutosh Khandelwal\Documents VSC\Chegg ./a.exe Enter the string abcba Palindrome

Please upvote if satisfied!! Thanks :)

Add a comment
Know the answer?
Add Answer to:
1. DOES A DIGIT APPEAR IN AN INTEGER, Write a recursive function appears(n,i) that returns true...
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
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