A little C# question

Hi

I'm fooling around with C# and made a small program which takes a string calculates its length and then finally prints the whole string and the length.

using System;

class StringInput
{
public static void Main() {

string strInput = Console.ReadLine();

for (int i = 1; i < strInput.Length; ++i)

if (strInput.Length < i *i) {
Console.WriteLine("Hi, " + strInput + " Your Name " + strInput.Length + " characters

long.");
}
}
}

If I type Bill Clinton as input, I get the following output:

Hi, Bill Clinton Your Name 12 characters long.
Hi, Bill Clinton Your Name 12 characters long.
Hi, Bill Clinton Your Name 12 characters long.
Hi, Bill Clinton Your Name 12 characters long.
Hi, Bill Clinton Your Name 12 characters long.
Hi, Bill Clinton Your Name 12 characters long.
Hi, Bill Clinton Your Name 12 characters long.
Hi, Bill Clinton Your Name 12 characters long.

Why does it the program do (length of input X numbers of repeats)

/Fred
Euroman_21Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
Hi Euroman_21;

The for statement goes through the loop strInput.Length - 1 times. The length of the string “Bill Clinton”  12 characters long. The if statement inside  the for loop will only be true when strInput.Length   <  i * i.  So for i = 1, 2, 3 the Console.Writeline will not execute. For the i = 4, 5, 6, 7, 8, 9, 10, and 11 the Console.Writeline will execute. For the 8 times that the Console.Writeline executes it prints the same thing because the values of strInput and strInput.Length never change.

Fernando
0
Euroman_21Author Commented:
Hi Fernando,

What do I need to change to avoid that the loop run 8-times??

Fred
0
Fernando SotoRetiredCommented:
Hi Fred;

Seeming that the string class already has a length property there is no need to calculate it, just print it out.

Take this code

for (int i = 1; i < strInput.Length; ++i)

if (strInput.Length < i *i) {
Console.WriteLine("Hi, " + strInput + " Your Name " + strInput.Length + " characters

long.");

And replace it with this code

Console.WriteLine("Hi, " + strInput + " Your Name " + strInput.Length + " characters long.");

Fernando
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.