patternjavaMinor
Circular shift string
Viewed 0 times
shiftcircularstring
Problem
So,basically I am working on an algorithm for circular shifting a string upto a position.There are two parameters required here.
For example:
Ignore the limited validation here.
`
Give your views on the code here.
- String of text to shift
- Number of shifts required.
For example:
Given string: "Hello"
Shifts: 3
Final: "ollHe"Ignore the limited validation here.
import java.lang.*;
import java.util.*;
public class Shift
{
public String shifting(String s,Integer sh)
{
Integer shifts=sh;
String string=s;
char[] shifted=new char[50];
Integer pos=0;
for(int i=string.length();i>shifts;i--)
{
System.out.println(""+i);
pos++;
shifted[pos]=string.charAt(i-1);
}
System.out.println("Shifting complete");
for(int j=0;j<shifts;j++)
{
System.out.println(""+j);
pos++;
shifted[pos]=string.charAt(j);
}
return new String(shifted);
}
public static void main(String[] args) {
System.out.println("Enter the string ");
Scanner sc=new Scanner(System.in);
String string=sc.nextLine();
System.out.println("So you have entered:"+string);
System.out.println("Enter the number of shifts:");
Integer shifts=sc.nextInt();
System.out.println("number of shifts is:"+shifts.toString());
System.out.println("Shifted string:"+new Shift().shifting(string,shifts));
}
}`
Give your views on the code here.
Solution
Do you have any constraints?
Assuming your example is wrong, what about:
- Is your example wrong? I would expected
lloHe
- If you are concerned of modifying your method parameters make them
final. Copy it to local variables and don't change the variables is kind of useless.
- Don't use short variables names (exception: the loop counter)
- If you use
Integerinstead ofint, you should handlenull
- Maybe a simple
staticutility method would be fine here, except you have some inheritance scenario in mind.
- Maybe you should also indicate that you are shifting left for positive numbers
- You should take the length of you string for the
char.
Assuming your example is wrong, what about:
import java.util.Objects;
import javax.annotation.Nonnull;
public final class Shift
{
@Nonnull
public static String left( @Nonnull final String string, final int shift )
{
final int length = string.length();
if( length == 0 ) return "";
final int offset = ((shift % length) + length) % length; // get a positive offset
return string.substring( offset, length ) + string.substring( 0, offset );
}
public static void main( String... args )
{
assertEquals( "loHel", Shift.left( "Hello", -2 ) );
assertEquals( "oHell", Shift.left( "Hello", -1 ) );
assertEquals( "Hello", Shift.left( "Hello", 0 ) );
assertEquals( "elloH", Shift.left( "Hello", 1 ) );
assertEquals( "lloHe", Shift.left( "Hello", 2 ) );
assertEquals( "loHel", Shift.left( "Hello", 3 ) );
assertEquals( "oHell", Shift.left( "Hello", 4 ) );
assertEquals( "Hello", Shift.left( "Hello", 5 ) );
assertEquals( "elloH", Shift.left( "Hello", 6 ) );
assertEquals( "", Shift.left( "", 3 ) );
}
private static void assertEquals( String expected, String actual )
{
if( !Objects.equals( expected, actual ) ) throw new AssertionError( "Expected: >" + expected + "" + actual + "<" );
}
}Code Snippets
import java.util.Objects;
import javax.annotation.Nonnull;
public final class Shift
{
@Nonnull
public static String left( @Nonnull final String string, final int shift )
{
final int length = string.length();
if( length == 0 ) return "";
final int offset = ((shift % length) + length) % length; // get a positive offset
return string.substring( offset, length ) + string.substring( 0, offset );
}
public static void main( String... args )
{
assertEquals( "loHel", Shift.left( "Hello", -2 ) );
assertEquals( "oHell", Shift.left( "Hello", -1 ) );
assertEquals( "Hello", Shift.left( "Hello", 0 ) );
assertEquals( "elloH", Shift.left( "Hello", 1 ) );
assertEquals( "lloHe", Shift.left( "Hello", 2 ) );
assertEquals( "loHel", Shift.left( "Hello", 3 ) );
assertEquals( "oHell", Shift.left( "Hello", 4 ) );
assertEquals( "Hello", Shift.left( "Hello", 5 ) );
assertEquals( "elloH", Shift.left( "Hello", 6 ) );
assertEquals( "", Shift.left( "", 3 ) );
}
private static void assertEquals( String expected, String actual )
{
if( !Objects.equals( expected, actual ) ) throw new AssertionError( "Expected: >" + expected + "< was: >" + actual + "<" );
}
}Context
StackExchange Code Review Q#149749, answer score: 3
Revisions (0)
No revisions yet.