Hello Folks,

This is frequently asked interview question.

Problem:

Suppose you have below string:

“Make Selenium Easy” Where

-“Make” is at position zero.
-“Selenium” is at first position .
-“Easy” is at second position .

If I say:

-zero, You need to print “Make Selenium Easy”.
-one, you need to print “Selenium Easy Make”.
-two, you need to print “Easy Selenium Make”.

Accept string and position from user.

Solution:

 

package MakeSeleniumEasy;

import java.util.Scanner;

public class CircularString {

	public static void main(String[] args) {
		
		Scanner sc= new Scanner(System.in);
		System.out.println("Enter words separated by space:");
		String input= sc.nextLine();
		System.out.println("Enter position:");
		int index=sc.nextInt();
		// Splitting input string in to string array
		String inputArray[]= input.split(" ");
		// validation for acceptable index
		// index should be grater than or equal to zero and less than length of array
		if(index>=0 && index <inputArray.length)
		{
			System.out.print("Output: ");
			// Printing from given index till last index
			for(int i=index;i<inputArray.length;i++)
			{
				System.out.print(inputArray[i]+" ");
			}
			// printing from index zero to position-1
			for(int k=0;k<index;k++)
			{
				System.out.print(inputArray[k]+" ");
			}
		}
		else
		{
			System.out.println("Index is invalid.");
		}
	}
}

Output:

Enter words separated by space:
Make Selenium Easy
Enter position:
0
Output: Make Selenium Easy 
===========================
Enter words separated by space:
Make Selenium Easy
Enter position:
1
Output: Selenium Easy Make 
==========================
Enter words separated by space:
Make Selenium Easy
Enter position:
2
Output: Easy Make Selenium 
==========================
Enter words separated by space:
Make Selenium Easy
Enter position:
3
Index is invalid.

Program is self descriptive. I have put required comments as well. If you have nay doubt, feel free to comment.

#HappyCoding

2 thoughts on “Java Program 09: Printing Words In Circular Order From Given Index

  1. Hi Amod,
    This program can be done other way also, using do..while loop

    public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println(“Enter words separated by space:”);
    String input = sc.nextLine();
    System.out.println(“Enter position:”);
    int index = sc.nextInt();
    // Splitting input string in to string array
    String inputArray[] = input.split(” “);
    int len = inputArray.length;
    // validation for acceptable index
    // index should be grater than or equal to zero and less than length of array
    if (index >= 0 && index < len) {
    System.out.print("Output: ");
    // Printing from given index till last index
    int i = index;
    do{

    System.out.print(inputArray[i%len]+" ");
    i++;
    i = i % len;
    }while(i!=index);

    } else {
    System.out.println("Index is invalid.");
    }
    sc.close();
    }

    I have validated this one and it is also giving the same expected output.

Leave a Reply