Issue
public class KebabCaseToCamelCase {
public String kebabToCamel(String str){
str = str.substring(0,1).toLowerCase() + str.substring(1);
StringBuilder builder = new StringBuilder(str);
int strLength = str.length();
for (int i = 0; i < strLength; i++){
if (builder.charAt(i) == '-'){
builder.deleteCharAt(i);
builder.replace(i, i+1,
String.valueOf(Character.toUpperCase(
builder.charAt(i))));
strLength--;
}
}
return builder.toString();
}
public static void main(String[] args) {
KebabCaseToCamelCase obj = new KebabCaseToCamelCase();
String modifiedString = obj.kebabToCamel("geeks-for-geeks");
System.out.println(modifiedString);
}
}
the code above converts any string written in kebab cased style to camel case style. Now the challenge is to ensure that a string in this format: "a-1c" does not pass the test as we will be presented with "a1C" instead of "aC". how do I manipulate the code above to obtain the latter output.
I appreciate your help. Thank you.
Solution
All characters have an index in the ASCII table. You can convert any char to an integer to retrieve the actual ASCII index of the char.
Example: If you convert the char '1' to int using typecasting, the int will store the value 49. So if check if the casted int is in range 48 - 57, the char will hold a number between '0' and '9'. You can then handle this char however you want.
Here is a small but useful ASCII table that will help you: https://www.asciitable.com/
Answered By - John
Answer Checked By - Mary Flores (JavaFixing Volunteer)