10 Replies - 429 Views - Last Post: 02 December 2012 - 10:02 AM Rate Topic: -----

#1 rockkrose  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 25-November 12

Display multiple strings

Posted 01 December 2012 - 01:00 AM

Hello,

I'm trying to display a string a number of times. I build a method with 2 parameters: string s and int i; and I want to return s;
So for example if s = "aaa" the result should be "aaa aaa aaa" -----> i-times.
The method looks like this:

using System;
public class Program {
  public static string Puzzle(int i, string s) {
    
        
        for (int j = 0; j<i-1; j++) {
          s += " " + s;
        }
        return s;
    
  }
}




but the number of times strins s is displayed is incorrect. I would appreciate a little help. Thank you! :)

Is This A Good Question/Topic? 0
  • +

Replies To: Display multiple strings

#2 Michael26  Icon User is offline

  • DIC-head, major DIC-head
  • member icon

Reputation: 355
  • View blog
  • Posts: 1,518
  • Joined: 08-April 09

Re: Display multiple strings

Posted 01 December 2012 - 01:37 AM

I would use an array to store values and then display those values.
i in your case would be the size of the array, s would be the string you use.

This post has been edited by Michael26: 01 December 2012 - 01:39 AM

Was This Post Helpful? 0
  • +
  • -

#3 rockkrose  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 25-November 12

Re: Display multiple strings

Posted 01 December 2012 - 02:08 AM

View PostMichael26, on 01 December 2012 - 01:37 AM, said:

I would use an array to store values and then display those values.
i in your case would be the size of the array, s would be the string you use.


Hi,

Thanks for the response! I would like to use this solution, without arrays
Was This Post Helpful? 0
  • +
  • -

#4 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Display multiple strings

Posted 01 December 2012 - 02:12 AM

What are the input parameters? What is output? What did you expect it to output?
Run the code on paper, writing down what each variable is at each step.

Spoiler

This post has been edited by Momerath: 01 December 2012 - 02:14 AM

Was This Post Helpful? 0
  • +
  • -

#5 rockkrose  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 25-November 12

Re: Display multiple strings

Posted 01 December 2012 - 02:20 AM

View PostMomerath, on 01 December 2012 - 02:12 AM, said:

What are the input parameters? What is output? What did you expect it to output?
Run the code on paper, writing down what each variable is at each step.

Spoiler


If s = "aaa" and i = 3, the output should be "aaa aaa aaa" , the output should return string "s" -> for i times.
Was This Post Helpful? 0
  • +
  • -

#6 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Display multiple strings

Posted 01 December 2012 - 02:29 AM

I know what it is supposed to do, but what is it actually doing? Why is it printing out "aaa aaa aaa aaa"?

Here, let's run the code in our head
 Line 3 - i = 3, s = "aaa"
Line 6 - i = 3, s = "aaa", j = 0 (j is less than i-1 so we go to next line
Line 7 - i = 3, s = "aaa" + " " + "aaa" = "aaa aaa", j = 0
Line 8 - i = 3, s = "aaa aaa", j++ = 1
Line 6 - i = 3, s = "aaa aaa", j = 1 (j is less than i-1 so we go to next line
Line 7 - i = 3, s = "aaa aaa" + " " + "aaa aaa" = "aaa aaa aaa aaa", j = 1
Line 8 - i = 3, s = "aaa aaa aaa aaa", j++ = 2
Line 6 - i = 3, s = "aaa aaa aaa aaa", (j is equal to i-1 so we exit loop
Line 9 = i = 3, s = "aaa aaa aaa aaa"

Do you see what is causing the problem now?
Was This Post Helpful? 3
  • +
  • -

#7 rockkrose  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 25-November 12

Re: Display multiple strings

Posted 01 December 2012 - 03:28 AM

View PostMomerath, on 01 December 2012 - 02:29 AM, said:

I know what it is supposed to do, but what is it actually doing? Why is it printing out "aaa aaa aaa aaa"?

Here, let's run the code in our head
 Line 3 - i = 3, s = "aaa"
Line 6 - i = 3, s = "aaa", j = 0 (j is less than i-1 so we go to next line
Line 7 - i = 3, s = "aaa" + " " + "aaa" = "aaa aaa", j = 0
Line 8 - i = 3, s = "aaa aaa", j++ = 1
Line 6 - i = 3, s = "aaa aaa", j = 1 (j is less than i-1 so we go to next line
Line 7 - i = 3, s = "aaa aaa" + " " + "aaa aaa" = "aaa aaa aaa aaa", j = 1
Line 8 - i = 3, s = "aaa aaa aaa aaa", j++ = 2
Line 6 - i = 3, s = "aaa aaa aaa aaa", (j is equal to i-1 so we exit loop
Line 9 = i = 3, s = "aaa aaa aaa aaa"

Do you see what is causing the problem now?


Yes, I can see. Thanks a lot!!! I needed another object to store the list of string.
Was This Post Helpful? 0
  • +
  • -

#8 Michael26  Icon User is offline

  • DIC-head, major DIC-head
  • member icon

Reputation: 355
  • View blog
  • Posts: 1,518
  • Joined: 08-April 09

Re: Display multiple strings

Posted 01 December 2012 - 10:00 AM

View Postrockkrose, on 01 December 2012 - 10:08 AM, said:

View PostMichael26, on 01 December 2012 - 01:37 AM, said:

I would use an array to store values and then display those values.
i in your case would be the size of the array, s would be the string you use.


Hi,

Thanks for the response! I would like to use this solution, without arrays


Didn't know what you want, array came to mind first.
Was This Post Helpful? 0
  • +
  • -

#9 rockkrose  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 25-November 12

Re: Display multiple strings

Posted 02 December 2012 - 12:53 AM

View PostMichael26, on 01 December 2012 - 10:00 AM, said:

View Postrockkrose, on 01 December 2012 - 10:08 AM, said:

View PostMichael26, on 01 December 2012 - 01:37 AM, said:

I would use an array to store values and then display those values.
i in your case would be the size of the array, s would be the string you use.


Hi,

Thanks for the response! I would like to use this solution, without arrays


Didn't know what you want, array came to mind first.


Thanks anyway :)
Was This Post Helpful? 0
  • +
  • -

#10 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3535
  • View blog
  • Posts: 10,944
  • Joined: 05-May 12

Re: Display multiple strings

Posted 02 December 2012 - 09:41 AM

Why do you even need an array? This can easily done the inefficient way by having one temporary string variable, or the more efficient way using a StringBuilder.
Was This Post Helpful? 0
  • +
  • -

#11 rockkrose  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 25-November 12

Re: Display multiple strings

Posted 02 December 2012 - 10:02 AM

View PostSkydiver, on 02 December 2012 - 09:41 AM, said:

Why do you even need an array? This can easily done the inefficient way by having one temporary string variable, or the more efficient way using a StringBuilder.


I used a temporary string variable for this one, thanks!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1