Write a java program to change First char in small and other in capital :
import java.util.Scanner;
public class FirstCharIntoSmall {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter Sentence ");
String str = sc.nextLine();
System.out.println("Before small caps \n"+str);
System.out.println("After changing String ");
str=initSmall(str);
System.out.println(str);
}
private static String initSmall(String str) {
char ch[] = str.toCharArray();
for (int i = 0; i < ch.length; i++) {
if (i == 0 && ch[i] != ' ' || (ch[i] != ' ' && ch[i - 1] == ' ')) {
if (ch[i] >= 'A' && ch[i] <= 'Z') {
ch[i] = (char) (ch[i] + 32);
}
} else {
if (ch[i] >= 'a' && ch[i] <= 'z') {
ch[i] = (char) (ch[i] - 32);
}
}
}
return new String(ch);
}
}
Comments
Post a Comment