We help IT Professionals succeed at work.

A little C# question

Euroman_21
Euroman_21 asked
on
Medium Priority
254 Views
Last Modified: 2010-04-16
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
Comment
Watch Question

Fernando SotoRetired
CERTIFIED EXPERT
Distinguished Expert 2017

Commented:
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

Author

Commented:
Hi Fernando,

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

Fred
Retired
CERTIFIED EXPERT
Distinguished Expert 2017
Commented:
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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.