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 Rotated string: uinqEd
Explanation
Length checked for evenness. - Rotation applied only if condition met. - Together demonstrates conditional rotation. Conclusion: Shows how to rotate string with length constraint.