Cyclically Shifting the Input String: This Java program generates a set of substrings by cyclically shifting the input string “Selenium” characters to the left. It showcases nested loops and string manipulation to achieve this result.
Cyclically Shifting the Input String
package com.softwaretestingo.interviewprograms; public class InterviewPrograms14 { /* * Input="Selenium" * Output: * eleniumS * leniumeS * eniumleS * niumeleS * iumneleS * umineleS * muineleS */ public static void main(String[] args) { String str= "Selenium"; // char [ ] chs str.toCharArray ( ) ; int len = str.length() , temp = 0 ; String out = ""; for (int k = 0 ; k<len-1; k++) { for ( int i = k + 1 ; i < len ; i ++ ) { out =out + str.charAt(i); } temp= k ; while ( k >= 0 ) { out= out + str.charAt ( k ) ; k -- ; } k=temp; System.out.println ( out ) ; out=""; } } }
Output:
eleniumS leniumeS eniumleS niumeleS iumneleS umineleS muineleS
Explanation section by section:
- The program defines a class named InterviewPrograms14.
- Inside the class, there is a main method, which serves as the entry point for the program.
- The input string is initialized as Selenium.
- Variables len and temp are declared to store the length of the input string and to help in the shifting process, respectively.
- An empty string out is initialized to store the generated substrings.
- The program uses two nested for loops. The outer loop, controlled by the variable k, starts from 0 and goes up to len-1. This outer loop controls the number of substrings generated.
- The first inner loop, controlled by the variable i, starts from k + 1 and goes up to len – 1. This inner loop extracts characters from the input string starting from the position k + 1 and appends them to the out string. This step generates the part of the substring that comes after the character at position k.
- The program then sets temp to k to preserve the value of k for later use.
- The second inner loop is a while loop that executes while k is greater than or equal to 0. This loop extracts characters from the input string starting from the position k and appends them to the out string. This step generates the part of the substring that comes before the character at position k.
- After generating the complete substring, it prints the out string.
- Finally, the out string is reset to an empty string to prepare for the next iteration of the outer loop.
In summary, this program demonstrates how to cyclically shift the characters of a string to generate different substrings. It showcases the use of nested loops and string manipulation to achieve the desired result. The output will be a set of substrings formed by cyclically shifting the characters of “Selenium” to the left.
Alternative 1:
This Java program generates a set of substrings by cyclically shifting the characters of the input string “Selenium” to the left. The program achieves this by manipulating a character array representing the input string and repeatedly shifting the characters to generate the desired substrings.
package com.softwaretestingo.interviewprograms; public class InterviewPrograms14_1 { /* * Input="Selenium" * Output: * eleniumS * leniumeS * eniumleS * niumeleS * iumneleS * umineleS * muineleS */ public static void main(String[] args) { String str = "Selenium"; char [ ] input = str.toCharArray(); int len = input.length ; printArr(input, len); } public static void printArr (char[] input, int len) { for (int i = 0 ; i<input.length ; i++ ) { char temp = input[0]; for ( int j = 1 ; j < len ; j ++ ) { input[j-1]=input[j]; } input[len-1]=temp; len--; System.out.println(input); } } }
Output:
eleniumS leniumeS eniumleS niumeleS iumneleS umineleS muineleS muineleS
Explanation of the program:
- The program defines a class named InterviewPrograms14_1.
- Inside the class, there is a main method, which serves as the entry point for the program.
- The input string is initialized as Selenium.
- The input string is converted into a character array called input.
- The program calls a custom method printArr, passing the input character array and its length len as arguments.
- The printArr method is responsible for generating and printing the substrings.
- Inside the printArr method, a for loop is used to iterate through each character of the input array.
- For each iteration, the first character of the input array is temporarily stored in the variable temp.
- Another for loop is used to shift the characters in the input array to the left.
- Characters from index 1 to len-1 are shifted one position to the left, effectively shifting the first character to the end of the array.
- The character in the last position (index len-1) is then set to the value of temp, completing the left cyclic shift.
- The length len is decremented for the next iteration to ensure that the last character (previously the first one) is omitted in the next shift.
- The input array after each cyclic shift is printed using System.out.println.
In summary, this program demonstrates how to cyclically shift characters of a string by converting it to a character array. It uses nested loops and character manipulation to generate and print a set of substrings. The output will be a series of substrings formed by cyclically shifting the characters of “Selenium” to the left.
Leave a Reply