Skip to content

Instantly share code, notes, and snippets.

@anujku
Created December 14, 2014 21:06
Show Gist options
  • Save anujku/e37e273901dc4ebbfbe0 to your computer and use it in GitHub Desktop.
Save anujku/e37e273901dc4ebbfbe0 to your computer and use it in GitHub Desktop.
String comparison Console App
// Write a small console application that uses a string comparison function (that you
// will also write) that meets the following prototype:
//
// int MyStrCmp(char *a, char *b) - for C;
// or assume passed String data types - for C# / Java / PHP
//
// Entered or passed strings may be of any length; trap for edge conditions and
// display error messages. Functions cannot make use of or "wrap" any existing API
// calls. Function must return an int that is:
// <0 if string1 is less than string2;
// 0 if string1 is the same as string2; or
// >0 if string1 is greater than string2;
// and display the 2 strings as noted below.
//
// If the return value is <0, then returned strings are inverted, IE abcdef and uvwxyz,
// would be displayed as fedcba and zyxwvu
//
// If the return value is 0, then one string is returned and is made up with the merged
// 2 input strings; IE abcdef and uvwxyz, would be displayed as aubvcwdxeyfz.
//
// If the return value is >0, then one string is returned and is made up with the merged
// 2 input strings; IE abcdef and uvwxyz, would be displayed as zfyexdwcvbua
/**
* The Class StringCompareApp.
*/
public class StringCompareApp {
/**
* The main method.
*
* @param args the arguments
*/
public static void main(String[] args) {
String str1 = "abcdef";
String str2 = "uvwxyz";
StringCompareApp.run(str1, str2);
}
/**
* The main method.
*
* @param str1 the str1
* @param str2 the str2
*/
public static void run(String str1, String str2) {
int stringCompareVal = stringCompare(str1, str2);
if (stringCompareVal < 0) {
System.out.println(reverse(str1) + " and " + reverse(str2));
} else if (stringCompareVal == 0) {
System.out.println(merge(str1, str2));
} else if (stringCompareVal > 0) {
System.out.println(merge(reverse(str2), reverse(str1)));
}
}
/**
* String compare method.
*
* @param str1 the str1
* @param str2 the str2
* @return the int
*/
private static int stringCompare(String str1, String str2) {
int length1 = getStringLength(str1);
int length2 = getStringLength(str2);
int returnVal = 0;
if (length1 < length2)
returnVal = -1;
else if (length1 == length2)
returnVal = 0;
else if (length1 > length2) returnVal = 1;
return returnVal;
}
/**
* Finds the string length.
*
* @param str the str
* @return the string length
*/
private static int getStringLength(String str) {
int length = 0;
try {
while (true) {
str.charAt(length);
length++;
}
} catch (StringIndexOutOfBoundsException e) {
}
return length;
}
/**
* Reverses the string.
*
* @param str the str
* @return the string
*/
private static String reverse(String str) {
int length = str.length();
char[] chars = new char[getStringLength(str)];
int count = 0;
for (int i = length - 1; i >= 0; i--)
chars[count++] = str.charAt(i);
return new String(chars);
}
/**
* Merges the two strings.
*
* @param str1 the str1
* @param str2 the str2
* @return the string
*/
private static String merge(String str1, String str2) {
int length1 = getStringLength(str1);
int length2 = getStringLength(str2);
char[] chars = new char[length1 + length2];
int max = length1 > length2 ? length1 : length2;
int count = 0;
for (int i = 0; i < max; i++) {
if (i < length1) {
chars[count] = str1.charAt(i);
count++;
}
if (i < length2) {
chars[count] = str2.charAt(i);
count++;
}
}
return new String(chars);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment