Share on Google+Share on Google+

Repeat word as many times as it has characters

Ads
This section illustrates you the repetition of same word as many times as it has characters in it.

Repeat word as many times as it has characters in Java

This section illustrates you the repetition of same word as many times as it has characters in it. To do this, we prompt the user to enter the word using the Scanner class. After finding the length of the word, we have created a for loop, it will display the entered word no of times its length.

Here is the code:

import java.util.*;
class RepeatWord{
  public static void main(String[] args)  {
  System.out.println("Enter word");
  Scanner input=new Scanner(System.in);
  String word=input.next();
  int len=word.length();
    System.out.println();
  for(int i=0;i<len;i++){
    System.out.println(word);
  }
  }
}

Output:

Enter word
Hello

Hello
Hello
Hello
Hello
Hello

Advertisements

Ads

Posted on: October 24, 2009 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Ads

Advertisement