mardi 18 avril 2017

Inserting parts of a String in Java

I am currently learning Java in school and I am having a hard time with this one assignment. What you are supposed to do is take a string and insert "like" in between every word to make it "teen talk" i.e. I like love like to like code.

public String teenTalk(String sentence)
{
   for (int i = 0; i < sentence.length(); i++)
   {
       if(sentence.substring(i, i+1).equals(" "))
       {
           System.out.println("like");

       }
   }

   return sentence;

}

Does anyone know how I would insert "like" in a certain location, and how I could make it insert in the spaces that it is supposed to? As you can see, I have also been having problems with making infinite loops.

Aucun commentaire:

Enregistrer un commentaire