Search Tutorials

Tuesday 23 April 2013

Java code to reverse a word or sentence ending with '.' using recursion

/*
Program to reverse a word or sentence ending with '.' using recursion
*/
import java.io.*;
class reverseWord
{
static BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
public static void main(String args[]) throws IOException
{
reverseWord call = new reverseWord();
System.out.print("Enter characters & '.' to end : ");
call.print();
}
void print() throws IOException
{
char c = (char)br.read();
if(c!='.')
{
print();
System.out.print(c);
}
}
}


/**
* ALGORITHM :
* ---------
* 1. Start
* 2. Accept a sentence from user with full-stop(.) at the end.
* 3. Using recursion, print the sentence in reverse order.
* 4. End
*/

/*
OUTPUT :
------
Enter characters & '.' to end : stressed.
desserts
*/


No comments:

Post a Comment

Back to Top