Write a java program to change first char of every word in initCap : 

import java.util.Scanner;

public class ConvertWordInitCap {
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 init Caps");
System.out.println(str);
str = intitCaps(str);
System.out.println("After init Cap ");
System.out.println(str);
}

private static String intitCaps(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

Popular posts from this blog