So I was solving problem from one competitive websites.And I have completed 75% of the constraint.But the condition where the first word in the given sentence after space must be uppercase.I tried but it does not give correct answer. For example if the sentence is "This is" is the sentence it must be changed to "ThIs Is".So help me. And my code is as follows
import java.util.Scanner;
public class DancingSentence {
public String makeDancing(String sentence)
{
//sentence=sentence.replace("\\s+","");
//String sen = null;
char[] sen = sentence.toCharArray();
int i = 0;
System.out.println(sen.length);
if(i==0)
{
sen[i]=sen[i];
}
for(i=1;i<sen.length;i++)
{
// if()
if (i%2==0)
{
sen[i] = (char)(sen[i]-32);
}
//if((int)sen[i]==32)
if(Character.isWhitespace(sen[i]))
{
//System.out.print(" ");
sen[i+1]=Character.toUpperCase(sen[i+1]);
//i+=1;
}
}
sentence = sen.toString().copyValueOf(sen);
//sentence = sentence.replace("", "\\s+");
//System.out.println(sentence);
return sentence;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String sentence = sc.nextLine();
//sentence=sentence.replaceAll("\\s+","");
DancingSentence ds = new DancingSentence();
String result=ds.makeDancing(sentence);
System.out.println(result);
}
}