user3912862
user3912862

Reputation: 7

String pattern printing

How to print a string like below?

S
St
Str
Stri
String

I have tried this, but I don't know how to proceed after this:

String s = "string";

for (int i = 1; i <= s.length(); i++)
{
    System.out.println(s.charAt(i));
}

Upvotes: 0

Views: 86

Answers (1)

The scion
The scion

Reputation: 972

using substring?

String s="stringyyyyiyi";

for(int i=1;i<=s.length();i++)
{

   System.out.println(s.substring(0,i));
}

Upvotes: 2

Related Questions