Skip to content
Advertisement

How to add a letter the end of each word per its word index? [closed]

I have a problem about adding a letter the end of each word per its word index.

I cannot handle with that. How can I fix it?

Here is my code shown below.

for(int i=0;i<sentenceOfArray.length;i++) {
    sentenceOfArray[i] = sentenceOfArray[i] + "c";
}

Advertisement

Answer

Here is the solution with the usage of repeat method.

for(int i=0;i<sentenceOfArray.length;i++) {
    sentenceOfArray[i] = sentenceOfArray[i] + "a".repeat(i+1);
}
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement