Recursive method to print all permutations of a string, JAVA Programming

Objectives

_ Creating and compiling a simple program

_ Designing classes

_ Use of recursive algorithms

_ Learn basic combinatorics  

Task: string permutation

Write a recursive method to print all permutations of a string. For example, for a string "abc", the output would be:

abc

acb

bac

bca

cab

cba

Write a program to test your method.

The program should prompt the user to enter a string. Then the program should display all the possible permutations. You can assume that all characters in the input string are different.

Hint:

Declare the following two helping methods:

void displayPermutations(String text)

and

void displayPermutations(String done, String todo)

The first method invokes displayPermutations( "", text ). The second method uses a loop to move a character from todo to done and recursively invokes it with a new todo and done. The base case is when todo string is empty. In this case the method prints done

Example

Note:

user entered values are shown in bold.  

Example 1

This program prints out all permutations of a string.

Enter a string? dome

There are the following permutations:

dome

doem

dmoe

dmeo

deom

demo

odme

odem

omde

omed

oedm

oemd

mdoe

mdeo

mode

moed

medo

meod

edom

edmo

eodm

eomd

emdo

emod

Example 2

This program prints out all permutations of a string.

Enter a string? one

There are the following permutations:

one

oen

noe

neo

eon

eno  

Important Notes

1. For the purposes of this assignment, you can assume that the user will only enter  valid values.

2. In your program you must define and make use of at least the two methods:

a. displayPermutations(String text) and

b. displayPermutations(String done, String todo) as described above.

3. Test your program. In particular, be sure to test it on turing before you submit it.

Posted Date: 2/22/2013 7:05:56 AM | Location : United States







Related Discussions:- Recursive method to print all permutations of a string, Assignment Help, Ask Question on Recursive method to print all permutations of a string, Get Answer, Expert's Help, Recursive method to print all permutations of a string Discussions

Write discussion on Recursive method to print all permutations of a string
Your posts are moderated
Related Questions
Instructions Modify the program so that the computer will generate moves for the ‘O' player (it must continue to accept user input for ‘X'). A simple way to do this is to use a

LookupDispatchAction is useful if the method name in the Action is not driven by its name in the front end, but by the Locale independent key into the resource bundle. As the key i

Relate in brief - Microsoft vs Java Microsoft hates Java, as a Java program (portable) isn't tied to any particular operating system. If Java is popular, then programs written

What is Object Oriented Programming explain with an example? In Java everything is an object or a class (or a piece of one or a collection of various). Objects send messages t

Explain the term - Garbage Collection Java  performs  garbage  collection  and  eliminates  the  need  to  free  objects  explicitly.  When an object  has  no references   to

Explain init(), start(), stop(), and destroy() method? The init() method is known as exactly once in an applet's life, while the applet is first loaded. It's generally used to

A   socket   is      interaction   channel,   which   facilitates   inter-process   communication. A socket is an endpoint for interaction. There are two types of sockets, dependin

How we Use a Car object in a different class ? class Car { String licensePlate; // e.g. "New York 543 A23" double speed; // in kilometers per hour double maxSpeed;

The non-final functions are clone (), finalize (), toString (), equals () , hashCode () and. The other methods like wait (), getClass (), notifyAll (), notify () etc are final

The last exercise of each assignment will be continuous from assignment to assignment and will have the goal of building an e-store website by the end of the course. Date & Tim