Need Help Developing Simple Program
I need to:
Develop a program that uses recursion to create all permutations of a string argument and prints the permutations, one permutation per line. The string argument must be supplied as the first argument of the program execution call.
If an argument is not supplied whenever the program is to be executed, output a message stating that a string argument is required instead of letting the program abort.
Your help in this would be greatly appreciated.
> Your help in this would be greatly appreciated.You bet. You have to tell us what part you are stuck on and need help with, though.Drake
You are in luck! Code that does exactly what you want exists on this forum. This appears to be a very common assignment. All you have to do is look at every entry in this forum or use the search function until you find it.
Granted that search can take more time than writing the function from scratch but its there if you look for it.
Enjoy
<quote>
public class PermutationGenerator
{
private void perm(String s, String prefix) {
int length = s.length();
if (length== 0) System.out.println(prefix);
else {
for (int i = 0; i < length; i++){
perm(s.substring(0, i) + s.substring(i+1, length), prefix + s.charAt(i));
}
}
}
public void printPermutations(String s){
perm(s,"");
}
public static void main(String[] args) {
new PermutationGenerator().printPermutations("ABC") ;
}
}
</quote>
this is close but if an argument is not supplied whenever the program is to be executed, i need to output a message stating that a string argument is required instead of letting the program abort.
> this is close but if an argument is not supplied
> whenever the program is to be executed, i need to
> output a message stating that a string argument is
> required instead of letting the program abort.
So basically your saying something like:
if (args.length > 0) {
// execute the program
} else {
// print out your message about the manadatory argument
}
Is that what your looking for?
J
Yes, but where do i insert that into the program?
> Yes, but where do i insert that into the program?
This is like asking someone for "help" rotating the tires of your car, then having the person come over and do the entire tire rotation themselves (which is what you meant when you said "help") while you eat a sandwich in the kitchen, and then complaining because they got bored and quit before putting on the last hubcap.
They have already helped you cheat on your assignment enough. If you cannot put the cherry on top, drop the class.
Drake
go back to your hole...40 year old virgin
> go back to your hole...40 year old virginDefinitely professional programmer material.Drake
private void perm(String s, String prefix) {
int length = s.length();
if (length== 0) System.out.println(prefix);
else {
for (int i = 0; i < length; i++){
perm(s.substring(0, i) + s.substring(i+1, length), prefix + s.charAt(i));
}
}
}
public void printPermutations(String s){
perm(s,"");
}
I am confused about the purpose of the prefix....and perm(s, " ")
> I am confused about the purpose of the prefix....and
> perm(s, " ")
Translation: I don't know jack about programming, can someone do my homework?
The answers were given to you. If you can't figure it out now, it's tiime to read some of those textbooks or do some tutorials.
Maybe this is a bit of a harsh reply, but you provoked it with your reaction to Drake (reply #8).
Good luck.
It goes in the beginning of the main method.