Rotate String Only if Length Even
This program rotates string only if length is even.
Problem Statement
Write a Java program to rotate "Eduinq" by 2 positions only if length is even.
Source Code
| 1 | public class RotateIfEvenLength { |
| 2 | public static void main(String[] args) { |
| 3 | System.out.println("Eduinq Rotate If Even Length"); |
| 4 | String str="Eduinq"; |
| 5 | if(str.length()%2==0){ |
| 6 | int d=2; |
| 7 | String rotated=str.substring(d)+str.substring(0,d); |
| 8 | System.out.println("Rotated string: "+rotated); |
| 9 | } else System.out.println("Length not even, no rotation"); |
| 10 | } |
| 11 | } |
Program Output
Eduinq Rotate If Even Length Length not even, no rotation
Explanation
The string's length is checked for evenness using the modulus operator, and rotation is only applied if this condition holds, demonstrating string rotation with a length-based constraint.