Solved

For Loop c#

Posted on 2014-11-14
5
49 Views
Last Modified: 2015-03-06
Trying to understand this for Loop . New to C#

12.    for (int i = temp.Length-1; i >0 ; i--)  
13.    {  
14.        temp3 += " ";  
15.        Console.WriteLine("{0}"+temp3 +"{1}", temp.Remove(i), (temp2.Remove(i)));  
16.  
17.    }  

static void Main(string[] args)  
02.{  
03.    Console.WriteLine("Enter Your Name ");  
04.    string temp=Console.ReadLine();  
05.    Console.WriteLine("");  
06.    Console.WriteLine("");  
07.    Console.WriteLine("");  
08.    string temp2 = temp;  
09.    string temp3 = "";  
10.    temp += " ";  
11.    temp2 += " ";  
12.    for (int i = temp.Length-1; i >0 ; i--)  
13.    {  
14.        temp3 += " ";  
15.        Console.WriteLine("{0}"+temp3 +"{1}", temp.Remove(i), (temp2.Remove(i)));  
16.  
17.    }  
18.    temp3 += " ";  
19.    for (int i = 1; i < temp.Length; i++)  
20.    {  
21.        Console.WriteLine("{0}"+temp3.Remove(temp3.Length-i-i-i+1)+"{1}", temp.Remove(i), (temp2.Remove(i)));  
22.  
23.    }  
24.    Console.WriteLine("");  
25.    Console.WriteLine("");  
26.    Console.WriteLine("");  
27.}  

Open in new window

0
Comment
Question by:yguyon28
5 Comments
 

Author Comment

by:yguyon28
ID: 40442862
Like what does this line really do..
  Console.WriteLine("{0}"+temp3 +"{1}", temp.Remove(i), (temp2.Remove(i)));
0
 
LVL 24

Expert Comment

by:mankowitz
ID: 40442890
I can't tell you what the ultimate purpose of this is, but the command

String.Remove(int)

removes characters from the end of a string and returns the remaining string. For example,
string s = "I like to eat";
Console.Writeline(s.Remove(6));  // prints "I like "

so, the line
Console.WriteLine("{0}"+temp3 +"{1}", temp.Remove(i), (temp2.Remove(i)));

prints the person's name, then some spaces, then the name again. Each time the loops is run, it shaves off more characters from the end of the person's name until there are none left. When it runs, it should produce something like a triangle with the person's name.
0
 
LVL 7

Expert Comment

by:Thomas Wheeler
ID: 40442898
It is writing the variable temp out and removing a character starting at position i. in the loop so if you enter 12345678 as the name than it will output
12345678 12345678
1234567  1234567
123456   123456
12345    12345
1234     1234
123      123
12       12
1        1
0
 
LVL 24

Accepted Solution

by:
mankowitz earned 500 total points
ID: 40442910
right, I meant two triangles.

I mean two right triangles.

But it looks better in a monospace font.
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 40443007
Hi yguyon28;

I made comments in the code, please read.

static void Main(string[] args)  
{
    // Writes the string Enter Your Name and adds a crlf
    Console.WriteLine("Enter Your Name ");  
    // Waits for the user to type his / her name and places that name into the variable temp
    // after the user presses the return key
    string temp=Console.ReadLine();  
    // The next three lines advances the cursor on the screen three lines one for each WriteLine
    Console.WriteLine("");  
    Console.WriteLine("");  
    Console.WriteLine("");  
    // Assigns the user input, name, to the variable temp2
    string temp2 = temp;  
    // Initializes the string temp3 to an empty string
    string temp3 = "";  
    // Appends a space character to the end of temp
    temp += " ";  
    // Does the same thing to temp2 as line above
    temp2 += " ";  
    // Initializes the for loop to the length of the string in temp
    // and each time through the loop decraments by one
    for (int i = temp.Length-1; i >0 ; i--)  
    {  
        // Adds a single space character to the end of temp3
        temp3 += " ";  
        // Writes a single line to the console each time through the loop with the values of
        // temp with one character removed the value of temp3 with a space character inserted
        // temp3 is used to keep algiment of the two columns and temp2 with one character removed
        Console.WriteLine("{0}"+temp3 +"{1}", temp.Remove(i), (temp2.Remove(i)));  
        // So if the input was Mark then the console would look like this
        // Mark Mark
        // Mar  Mar
        // Ma   Ma
        // M    M
    }

    // Reinitialize temp3 to a single space character
    temp3 += " ";  

    // This for loop is going to cause issues because in the statement in the for loop
    // you will be attempting to access characters that have been removed in the 
    // previous for loop an so you are going to get index out of range
    for (int i = 0; i < temp.Length; i++)  
    {  
        Console.WriteLine("{0}"+temp3.Remove(temp3.Length-i-i-i+1)+"{1}", temp.Remove(i), (temp2.Remove(i)));  
  
    }  
    Console.WriteLine("");  
    Console.WriteLine("");  
    Console.WriteLine("");  
}]

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
install app on other machine 13 32
Problem to error 4 46
Problem of RegEx to match the first occurence of 10 36
Host asp.net pages 5 25
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

895 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now