A class ‘REV’ has been defined to display the reverse of each word of the entered sentence. The member method of the class are given as:
String sentence String word String reverse
REV()
void input()
void rev_word(String w, int l)
To store a sentence
To store each word of the sentence
To store the reverse of each word of the sentence
A default constructor to initialize the instance variables
To accept the sentence and to find each word of the sentence
To display the reverse of each word using recursive technique
Define the main() function to create an object and call the functions accordingly to enable the task
Answers
Answered by
0
Reverse of a sentence using recursion
Explanation:
In C Programming:
#include <stdio.h>
void reverseSentence();
int main() {
printf("Enter a sentence: ");
reverseSentence();
return 0;
}
void reverseSentence() {
char c;
scanf("%c", &c);
if (c != '\n') {
reverseSentence();
printf("%c", c);
}
}
In Java Programming:
public class Reverse {
public static void main(String[] args) {
String sentence = "Go work";
String reversed = reverse(sentence);
System.out.println("The reversed sentence is: " + reversed);
}
public static String reverse(String sentence) {
if (sentence.isEmpty())
return sentence;
return reverse(sentence.substring(1)) + sentence.charAt(0);
}
}
Hope it helps
Similar questions
Accountancy,
4 months ago
Math,
4 months ago
Math,
4 months ago
Accountancy,
8 months ago
English,
8 months ago
Chemistry,
11 months ago
Chemistry,
11 months ago
Hindi,
11 months ago