[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

A little C# question

Posted on 2006-03-26
3
Medium Priority
?
233 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
0
Comment
Question by:Euroman_21
  • 2
3 Comments
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 16294687
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
 

Author Comment

by:Euroman_21
ID: 16294724
Hi Fernando,

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

Fred
0
 
LVL 64

Accepted Solution

by:
Fernando Soto earned 150 total points
ID: 16294750
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question