Write a recursive function that takes a string as an input and returns the reverse of the string.
Write a recursive function rec_string that produces the output shown below for the corresponding function calls. Write a main function to test the function.
Method call rec_string(‘abcde’), will produce the following output:
*
e
de
cde
bcde
abcde
Method call rec_string(‘abc’), will produce the following output:
*
c
bc
abc
I have written the program in c++
Question 1:
Here is code:
string rec_string(string str){
if (str.length() == 1) {
return str;
}else{
return reverseStringRecursively(str.substr(1,str.length())) +
str.at(0);
}
}
Question 2:
Here is function:
void rec_string(string str){
static int count = 0;
if(count == 0) // if count is 0 prints only *
cout << "*" << endl;
else
{
// loops from str length - count to
str end
for(int i = str.length() - count; i < str.length()
; i++)
cout << str[i];
cout << endl;
}
if(count == str.length()) // if count read str length then
exit
return;
count++;
rec_string(str);
}
Sample main function to test:
#include <iostream>
#include <string>
using namespace std;
void rec_string(string str){
static int count = 0;
if(count == 0) // if count is 0 prints only *
cout << "*" << endl;
else
{
// loops from str length - count to
str end
for(int i = str.length() - count; i < str.length()
; i++)
cout << str[i];
cout << endl;
}
if(count == str.length()) // if count read str length then
exit
return;
count++;
rec_string(str);
}
int main()
{
rec_string("abcde");
return 0;
}
Output:
#include<stdio.h> void revs(char *p) { if(*p) revs(p+1); printf("%c",*p); } int main() { char name[40]; scanf("%[^\n]s",name); revs(name); }
Write a recursive function that takes a string as an input and returns the reverse of...
Write a recursive method isReverse(String s1, String s2) that takes two strings and returns true if s1 is the reverse of s2, false otherwise. Then, draw the sequence of recursive calls for the following cases. Submit your diagrams in a PDF file called isReverseTrace.pdf. isReverse("happy", "yppah") will return true isReverse("cool", "loac") will return false isReverse("", "") will return true
Write a recursive function, take a String as input, return true, if the String is a palindrome; false otherwise. For instance, if the input is: “A nut for a jar of tuna” Then the return value is true. Notice that the non English letters are ignored; the spaces are ignored; and it is NOT case sensitive. You must write recursive function. You shall turn in a complete program, including main function to use your function to test if a String...
Qhuestion 6 i.Write a recursive function reverseConcatenate that takes as input a list with string entries and returns one long string which is the concatenation of all the string entries in reverse order. For instance, reverseConcatenate([’how’, ’are’, ’you?’]) should return ’you?arehow’.
java code Write a method called reverse that takes an array of integers as an input and returns the same values in reverse order as the output. Test your method in the main.
C++ Write a recursive function that reverses the given input string. No loops allowed, only use recursive functions. Do not add more or change the parameters to the original function. Do not change the main program. #include <iostream> #include <string> using namespace std; //Write a recursive function 'void reverse(string &str)' that reverses the given input string. void reverse(string &str) { /*Code needed*/ } int main() { string name = "sherry"; reverse(name); cout << name << endl; //should...
JAVA - Write a recursive function that takes a linked list as input and returns all of the elements in the linked list. Input: 1, 2, 3, 4, 5 Output: (1+2+3+4+5)/5 = 3 What I have: public static int findMean (Node head, Node cur, int n){ int average = 0; if (head == null){ if(n == 0) return 0; } if (n > 0){ int sum = n + findMean(head, head.next, n -1); average = (sum)/n; } return average; }...
11) Write a method (including header and body) that takes as an input an array of doubles and f three doubles containing the average, the maximum and the minimunm values of the input array. Test the program by calling the method from the main method. For example Input: 1 2 3 45 Output: 3 5 1 12) Write a Java method that takes a string as an argument and returns the reverse the string. Test the program by calling the...
C++ Write a recursive function that passes no parameters which returns the reverse of a global variable string str. It should be structured like this: string recursive() { return reversed_string; }
Using C, Write a function reverse which takes a string as an argument, reverses the string and returns the reversed string. Note; you should not return a string that you created inside the reverse function! For example: Test char str[]-"hello" printf("%s", reverse (str)); Result olleh
Write a recursive function that takes a string as input. This recursive function should open a file by the name of the string, and read the first word. Then attempt to open a file by the word you just read and repeat this process. This ends, when there is no file with the name you are looking for. At this point, cout the last word in the current file (the one where the first word was not a different file)....