Monday, 27 November 2017

Java Program to Permute All Letters of an Input String


Code:

import java.util.Scanner;

public class Permute_All_Letters 
{
    static void permute(char[] a, int k) 
    {
        if (k == a.length) 
        {
            for (int i = 0; i < a.length; i++) 
            {
                System.out.print(a[i]);
            }
            System.out.println();
        } 
        else 
        {
            for (int i = k; i < a.length; i++) 
            {
                char temp = a[k];
                a[k] = a[i];
                a[i] = temp;

                permute(a, k + 1);

                temp = a[k];
                a[k] = a[i];
                a[i] = temp;
            }
        }
    }

    public static void main(String args[]) 
    {
        System.out.println("Enter the length of character string: ");
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();

        char[] sequence = new char[n];
        System.out.println("Enter the original string: ");
        for (int i = 0; i < n; i++)
            sequence[i] = sc.next().charAt(0);
        System.out.println("The permuted strings are: ");
        permute(sequence, 0);

        sc.close();
    }
}



Output:

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



More Java Programs:

















100+ Best Home Decoration Ideas For Christmas Day 2019 To Make Home Beautiful

Best gifts for Christmas Day | Greeting cards for Christmas Day | Gift your children a new gift on Christmas day This Christmas d...