Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
So in Python it's nice and simple;

A = lol

a * 3 would output lollollol

I have a program to make which was a LOT simpler in Python to do. I haven't figured out a way to do this yet. I thought I was getting close but it turns out that this tactic cannot support 4 arguments:

static void Problem5()
        {
            int ast = 1;
            int dash = 7;
            
            while (dash > -1)

            {
                Console.WriteLine(String.Concat(Enumerable.Repeat("-", "*", dash, ast));
                ast++;
                ast++;
                dash--;
            }


Keep in mind that this isn't all of the code of course; I was just working on figuring out how to multiply characters in strings as needed.

This is what my end output is supposed to be:

-------*
------***
-----*****
----*******
---*********
--***********
-*************
***************
-*************
--***********
---*********
----*******
-----*****
------***
-------*

What I have tried:

About an hour of reading similar posts on StackExchange
Posted
Updated 30-Nov-21 4:01am
Comments
BillWoodruff 29-Nov-21 1:33am    
If we do your homework, you will learn nothing.
Grelm 29-Nov-21 1:37am    
I don't want my homework to be done for me. All I'm asking here is how to multiply string in C#.
#realJSOP 29-Nov-21 12:10pm    
There's no such thing as multiplying a string. Multiplication is a numeric function. Furthermore, this isn't any harder to do in C# than it is in python. This is a logic problem more than it is a string manipulation problem.
George Swan 29-Nov-21 3:40am    
To instantiate a string consisting of the same char repeated, use the constructor: string dashes = new string('-', maxDashes);
To build each line in the pattern, join a dashes Substring to a stars Substring
#realJSOP 29-Nov-21 11:45am    
That was a fun little distraction. I did it in 11 lines of code (with curly braces on their own line, where they belong, and only one loop).

"Multiplying strings" is not a valid concept: it's like "adding phone numbers" and expecting to get a useful number as a result!

What you need to do for this is simple: two loops, one after the other.
The first loop counts down, the second loop counts up.
The first loop guard changes between 7 and 1 in the example above, the second changes between 1 and 7.
Inside each loop, print the guard number of hyphens, then (7 - guard) * 2 + 1 stars.
Between the loops, print the full row of stars.

Give it a try on paper to get you mind round it, it's not complicated. Then think about implementation.
 
Share this answer
 
v2
Comments
Richard MacCutchan 29-Nov-21 3:18am    
"The first loop counts down, the first loop counts down." ??
OriginalGriff 29-Nov-21 3:45am    
Lack of caffeine disease ... :blush:
Richard MacCutchan 29-Nov-21 3:49am    
I thought you'd be on your third pot by now.
OriginalGriff 29-Nov-21 4:41am    
I'm trying to cut down to three cups a day, since I got the bean to cup machine and noticed what weight of beans I was getting through ... :D
#realJSOP 29-Nov-21 11:00am    
Might be the chorus of a song...
The way I approached it was to determine how many stars are needed on a given line, and then determine how many hyphens based on the max number of stars (must be an odd number to work).

After that, it's knowing how to display two strings (one with the right number of hyphens, and the other with the right number of stars. You've already been given the best way to do this. After you post your working code, I'll show you how I did it.

BTW, I was looking at the code, and eliminated two more lines - I'm down to just nine lines of code. :)
 
Share this answer
 
v2
I'm going to go ahead and post my code because I think there are a few things I think are important to note:

0) Your instructor is apparently not preparing you adequately to do the assignments. If you have to go to a programming web site like CodeProject for help with a homework assignment, that tells me that part of your problem is that you're not getting the necessary instruction.

1) You need to teach yourself to use google to find the answers. This means you have to construct your search queries adequately so that you get appropriate search results. I recognize that this can be somewhat tedious, especially when you don't know what something is called, but as you progress, you'll learn better terms to include in your search. I pretty much live on google because there's simply too much info to hold in my head.

2) Do not start off thinking about how something done in .Net is "harder" to do than in python. It's just different, and that's to be expected. C# apps are statically compile, whereas Python is a scripting language. There's really no logical comparison that can be made beyond that point.

Anyway, here's the code I came up with. When you remove all the comments, it comes down to just nine lines of code. If you drop it into the main() method in a new a console app, and then add Console.ReadKey(); after this block of code, you'll see it work.

C#
// Teaching moment - exercises various string class methods, using the Math object to 
// normalize negative values, illustrate a while loop, and logic to only require 
// a single loop to generate the desired output.

// this is how many characters you want to display. It MUST be an odd number to work, 
// *but* it can be a positive or negative value, because we use the Math.Abs() method to 
// ensure that we use the absolute value of maxWidth (Math.Abs() always returns a positive 
// value).
int maxWidth  = 15;
// this is the number of stars we want to display. We start with 1.
int starCount = 1;
// this is the starcount increment value for each loop iteration. This is 
// the key to only needing one loop.
int increment = 2;
// while the starcount is greater than 1, we iterate
while (starCount >= 1)
{
    // display the resulting string - we build two strings with "new string(X, count)", 
    // where "X" is a character we want to repeat, and "count" is how many times we 
    // want to repeat the character. For the stars, that's easy because we are controlling 
    // that value with each iteration. For the hyphens, we need to calculate it on the fly 
    // by subtracting the star count from the maxWidth value, and then deividing that 
    // result by 2. Since we require an odd number for the maxWidth, and we're always 
    // displaying an odd number of stars, we don't have to worry about rounding because 
    // the math always works out to an even number, or 0.
    Console.WriteLine(string.Concat(new string('-', (int)((Math.Abs(maxWidth)-starCount)/2)), new string('*', starCount)));
    // next, we prepare for the next iteration by setting the increment value. If the 
    // increment value is current 2 (a positive number), and the starcount is less than 
    // the max width, we keep the increment value at 2. otherwise we set it to -2. This 
    // is where we constrain the number of necessary loops to just one. 
    increment = (increment == 2 && starCount < Math.Abs(maxWidth)) ? 2 : -2;
    // finally, do the math on the starcount. If we're adding a negative number, we're 
    // actually subracting the value, so we're in essence decrementing the starcount back 
    // to 1.
    starCount = starCount+increment;
};
 
Share this answer
 
v2
Comments
Richard Deeming 30-Nov-21 10:22am    
I'd probably be inclined to replace the three string instances with one StringBuilder. :)
Console.WriteLine(new StringBuilder(maxWidth).Append('-', (maxWidth - starCount) >> 1).Append('*', starCount));

You also don't need Math.Abs(maxWidth), since it never changes.
#realJSOP 30-Nov-21 12:17pm    
It's needed if the maxWidth is a negative value, which was one of my edge case tests
Richard Deeming 30-Nov-21 12:20pm    
So you're the inspiration for the old QA joke! :D

A QA engineer walks into a bar. Orders a beer. Orders 0 beers. Orders 99999999999 beers. Orders a lizard. Orders -1 beers. Orders a ueicbksjdhd.

First real customer walks in and asks where the bathroom is. The bar bursts into flames, killing everyone.
Matthew Dennis 30-Nov-21 11:01am    
Since you are posting solutions, my one liner
Console.WriteLine(string.Join("\n", Enumerable.Range(-7, 15)
    .Select(x => Math.Abs(x))
    .Select(x => new string('-', x) + new string('*',  15 - 2 * x ))));
#realJSOP 30-Nov-21 12:16pm    
Show-off. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900