Sunteți pe pagina 1din 3

1.

//This is a java program to permute all the characters of the input


string
2. import java.util.Scanner;
3.
4. public class Permute_All_Letters
5. {
6.
static void permute(char[] a, int k)
7.
{
8.
if (k == a.length)
9.
{
10.
for (int i = 0; i < a.length; i++)
11.
{
12.
System.out.print(a[i]);
13.
}
14.
System.out.println();
15.
}
16.
else
17.
{
18.
for (int i = k; i < a.length; i++)
19.
{
20.
char temp = a[k];
21.
a[k] = a[i];
22.
a[i] = temp;
23.
24.
permute(a, k + 1);
25.
26.
temp = a[k];
27.
a[k] = a[i];
28.
a[i] = temp;
29.
}
30.
}
31.
}
32.
33.
public static void main(String args[])
34.
{
35.
System.out.println("Enter the length of character string: ");
36.
Scanner sc = new Scanner(System.in);
37.
int n = sc.nextInt();
38.
39.
char[] sequence = new char[n];
40.
System.out.println("Enter the original string: ");
41.
for (int i = 0; i < n; i++)
42.
sequence[i] = sc.next().charAt(0);
43.
System.out.println("The permuted strings are: ");
44.
permute(sequence, 0);
45.
46.
sc.close();
47.
}
48. }

advertisements
Output:
$ javac Permute_All_Letters.java
$ java Permute_All_Letters

Enter the length of character string:


3
Enter the original string:
H e y
The permuted strings are:
Hey
Hye
eHy
eyH
yeH
yHe

Sanfoundry Global Education & Learning Series 1000 Java Programs.


Heres the list of Best Reference Books in Java Programming, Data Structures and Algorithms.
advertisements
Manish Bhojasia, a technology veteran with 19+ years @ Cisco & Wipro, is Founder and CTO at
Sanfoundry. He is Linux Kernel Developer and SAN Architect and is passionate about
competency developments in these areas. He lives in Bangalore and delivers focused training
sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux
Networking, Linux Storage & Cluster Administration, Advanced C Programming, SAN Storage
Technologies, SCSI Internals and Storage Protocols such as iSCSI & Fiber Channel. Stay
connected with us below:
Google+ | Facebook | Twitter | LinkedIn
Subscribe Sanfoundry Newsletter and Posts

Best Careers
Developer Tracks
SAN Developer
Linux Kernel Developer
Linux Driver Developer
Linux Network Developer
Live Training Photos
Mentoring
Software Productivity
GDB Assignment

Best Training
SAN I - Technology
SAN II - Admin

Linux Fundamentals
Advanced C Training
Linux-C Debugging
System Programming
Network Programming
Linux Threads
Kernel Programming
Kernel Debugging
Linux Device Drivers
advertisements
Sanfoundry is No. 1 choice for Deep Hands-ON Trainings in SAN, Linux & C, Kernel
Programming. Our Founder has trained employees of almost all Top Companies in India such
as VMware, Citrix, Oracle, Motorola, Ericsson, Aricent, HP, Intuit, Microsoft, Cisco, SAP Labs,
Siemens, Symantec, Redhat, Chelsio, Cavium, ST-Micro, Samsung, LG-Soft, Wipro, TCS, HCL,
IBM, Accenture, HSBC, Mphasis, Tata-Elxsi, Tata VSNL, Mindtree, Cognizant and Startups.

S-ar putea să vă placă și