I’m new to coding, and need some help completing an assignment. It specifies that we need to take strings from a file, and shift the letters forward a certain amount. This is the part I’m struggling with:
If a word has n letters, where n is an odd number, move the first (n+1)/2 letters to the end of the word. For example: ‘kitchen’ becomes ‘henkitc’.
If a word has n letters, where n is an even number, move the first n/2 letters to the end of the word. For example, ‘before’ becomes ‘orebef’.
Input files will look like this:
Before
Kitchen
Outputs like this:
Before OREBEF
Kitchen HENKITC
Apologies if this is a stupid question.
Here’s what I have so far:
public static void main(String[] args) throws IOException {
Scanner input;
FileInputStream inputFile = null;
FileOutputStream outputFile = null;
PrintWriter outPrint = null;
inputFile = new FileInputStream("input2.in");
outputFile = new FileOutputStream("results.out.txt");
outPrint = new PrintWriter(outputFile);
input = new Scanner(inputFile);
while (input.hasNext()) {
String lineReader = null;
lineReader = input.nextLine();
if (lineReader.length() % 2 == 0) {
String evenWord = lineReader;
String flippedEvenWord = null;
System.out.print(evenWord);
System.out.print(" ");
System.out.println(evenWord.toUpperCase());
outPrint.print(evenWord);
outPrint.print(" ");
outPrint.println(evenWord.toUpperCase());
}
else if (lineReader.length() % 2 != 0){
String oddWord = lineReader;
System.out.print(oddWord);
outPrint.print(oddWord);
outPrint.print(" ");
System.out.print(" ");
String flippedOddWord = oddWord;
System.out.println(flippedOddWord.toUpperCase());
outPrint.println(oddWord.toUpperCase());
}
else {
break;
}
}
inputFile.close();
outPrint.close();
input.close();
}
}
>Solution :
You are on the right track! Here’s how you can modify your code to shift the letters forward as specified in the assignment:
public static void main(String[] args) throws IOException {
Scanner input;
FileInputStream inputFile = null;
FileOutputStream outputFile = null;
PrintWriter outPrint = null;
inputFile = new FileInputStream("input2.in");
outputFile = new FileOutputStream("results.out.txt");
outPrint = new PrintWriter(outputFile);
input = new Scanner(inputFile);
while (input.hasNext()) {
String lineReader = null;
lineReader = input.nextLine();
if (lineReader.length() % 2 == 0) {
// even length word
String evenWord = lineReader;
String shiftedWord = evenWord.substring(evenWord.length()/2) + evenWord.substring(0, evenWord.length()/2);
System.out.print(evenWord + " ");
System.out.print(shiftedWord.toUpperCase());
System.out.println();
outPrint.print(evenWord + " ");
outPrint.println(shiftedWord.toUpperCase());
}
else if (lineReader.length() % 2 != 0){
// odd length word
String oddWord = lineReader;
String shiftedWord = oddWord.substring((oddWord.length()+1)/2) + oddWord.substring(0, (oddWord.length()+1)/2);
System.out.print(oddWord + " ");
System.out.println(shiftedWord.toUpperCase());
outPrint.print(oddWord + " ");
outPrint.println(shiftedWord.toUpperCase());
}
}
inputFile.close();
outPrint.close();
input.close();
}
In the code above, for even length words, we split the word at the middle, swap the two halves and concatenate them back together. For odd length words, we add 1 to the length of the word, split it at the middle, swap the two halves and concatenate them back together.
Note that I have also modified the print statements to match the output format specified in the assignment.