-2

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);

   }

  }
Jens
  • 67,715
  • 15
  • 98
  • 113
Nisha Vittal
  • 13
  • 1
  • 4
  • Instead of working so low level, why don't you break the string into words, capitalize each first letter, and put them back together? – vikingsteve Aug 28 '17 at 07:43
  • found something similar here: https://stackoverflow.com/questions/1086123/string-conversion-to-title-case. Check Scottb's answer – Rohit Verma Aug 28 '17 at 07:44
  • 4
    why `This` should be converted to `ThIs` what is the rule? – Youcef LAIDANI Aug 28 '17 at 07:47
  • The rule is that it follows a pattern where the letters in the even positions should be capitalized.And the space between words should be ignored.And for example "This is" is represented as "ThIs Is".So I am not getting the logic to make the character next to space to be capitalized – Nisha Vittal Aug 29 '17 at 09:46

1 Answers1

0

Your logic is almost right. If the index is even (i % 2 == 0), you convert the character to uppercase. If you encounter a whitespace, you just skip it and don't increase your variable that you use for determining even positions.

public String makeDancing(String sentence) {

    char[] sen = sentence.toCharArray();
    int mod = 0; //counts just letters
    for(int i=0; i < sen.length; i++) {
        if(Character.isWhitespace(sen[i])) { //if character is a whitespace, skip it
            continue;
        }
        if (mod % 2 == 0) { //if it is an even position, make it uppercase 
            sen[i]  = Character.toUpperCase(sen[i]);
        }
        mod++;
    }
    sentence = new String(sen);
    return sentence;
}
Tobias Geiselmann
  • 2,139
  • 2
  • 23
  • 36