Created
July 2, 2013 09:28
-
-
Save alexchantavy/5907937 to your computer and use it in GitHub Desktop.
Java attempt for http://www.reddit.com/r/dailyprogrammer/comments/1heozl/070113_challenge_131_easy_who_tests_the_test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* http://www.reddit.com/r/dailyprogrammer/comments/1heozl/070113_challenge_131_easy_who_tests_the_test | |
*/ | |
import java.util.*; // '*' cus i was lazy and doing this in a rush | |
import java.io.*; //ditto | |
public class Challenge131 { | |
public static void main (String args[]) { | |
//args[0] = input file name | |
try { | |
BufferedReader br = new BufferedReader(new FileReader(args[0])); | |
String line = br.readLine(); | |
// n = the total number of lines in the input file | |
int n = Integer.parseInt(line); | |
for (int i = 0; i < n; i++) { | |
line = br.readLine(); | |
StringTokenizer tk = new StringTokenizer(line); | |
// format of each line = | |
// <integer t> <string a> <string b> | |
// t = 0: reverse string a | |
// t = 1: capitalize string a | |
int t = Integer.parseInt(tk.nextToken()) ; | |
String a = tk.nextToken(); | |
String b = tk.nextToken(); | |
String aReversed, aCaps; | |
switch (t) { | |
case 0: | |
aReversed = reverse(a); | |
System.out.println("aReversed = " + aReversed + "; b = " + b); | |
System.out.println( (aReversed.equals(b))? " Match!" : " No match, fuck you!"); | |
break; | |
case 1: | |
aCaps = capitalize(a); | |
System.out.println("aCaps = " + aCaps + "; b = " + b); | |
System.out.println( (aCaps.equals(b))? " Match!" : " No match, fuck you!"); | |
break; | |
default: | |
break; | |
} | |
} | |
br.close(); | |
} | |
catch (Exception e) { | |
System.out.println(e); | |
} | |
} | |
/** | |
* My slow ass way to reverse a String in Java | |
*/ | |
public static String reverse(String str) { | |
char[] charArray = str.toCharArray(); | |
char[] reversed = new char[str.length()]; | |
int j = 0; | |
for (int i = charArray.length - 1; i >= 0; i--) { | |
reversed[j] = charArray[i]; | |
j++; | |
} | |
return new String(reversed); | |
} | |
/** | |
* (Java doesn't have a built-in String caps function?) | |
*/ | |
public static String capitalize(String str) { | |
char[] charArray = str.toCharArray(); | |
for (int i = 0; i < charArray.length; i++) { | |
charArray[i] = Character.toUpperCase(charArray[i]); | |
} | |
return new String(charArray); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment