4

I want to "multiply" a string by an int variable that the user inputs.

#include <cs50.h>
#include <stdio.h>

    // Height < 24

    string block = "#";
    string output;

    int main(void) {
        printf("Height: ");
        int height = GetInt();
        while (true) { 
            if (height < 24 && height > 0) {
                output = "#" + block * height;
                printf("%s\n", output); 
                break;

            } else {
                printf("Height: ");
                height = GetInt();
            }
        }
        return 0;
    }

Using the Height variable I want to multiply the string variable block (#) by Height, and add that to another "#".

I tried implementing it in the only way I could think of it making sense however it doesnt seem the syntax is right.

I've looked over StackOverflow on this subject and can only find C# and C++ topics with this question in mind.

EDIT: After being printed the output should look like this:

       ##
      ###
     ####
    #####
   ######
  #######
 ########
#########

And the lines of "#" being outputted depends on the Height variable that the user inputs. Say the user inputs a height of "5":

Height: 5
       ##
      ###
     ####
    #####
   ######

Should be output.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
TylerW
  • 61
  • 1
  • 1
  • 5
  • 3
    you cannot _multiply_ a _string_. Period. – Sourav Ghosh Jan 08 '16 at 12:33
  • 5
    Unclear for me: what does it mean to *multiply string*? ASCII code for `#`? Or you want `#` to be a substitution value? (then use a function) – John_West Jan 08 '16 at 12:33
  • 4
    How did you get `string` in `C` ? – Minato Jan 08 '16 at 12:35
  • I assume "multiplying a string" means "I want n # symbols in a string." C doesn't define *string* so I don't know what types those are and where cs50.h comes from. Normally you'd just use a char array and add as many #s as you want. – Sami Kuhmonen Jan 08 '16 at 12:35
  • @Minato It's a kind of magic ;) It is *not* a `std::string`! Look at ``: [`typedef char *string;`](https://mirror.cs50.net/library50/c/cs50-library-c-3.0/cs50.h) – John_West Jan 08 '16 at 12:35
  • allocate memory block like `output = malloc(height + 1)` and `memset(output, '#', height); output[height] = 0;printf("%s\n", output);free(output);` – BLUEPIXY Jan 08 '16 at 12:37
  • @John_West I am bit rusty.. you need to use `char*` for `string` like functionality.. or `typedef` – Minato Jan 08 '16 at 12:37
  • 2
    @Minato I assume he is using [this library](https://manual.cs50.net/library/) which has a `typedef` in it. – Simon Gibbons Jan 08 '16 at 12:37
  • 2
    could you show us how should look that string after you "multiply" it – Michi Jan 08 '16 at 12:41
  • he does not mean 'multiply' in numerical sense, he wants a 'duplicate' – Peter Miehle Jan 08 '16 at 12:43
  • You mean here `printf("%s\n", output);` the output should print `#` 24 times ? like this `########################` – Michi Jan 08 '16 at 12:44
  • After being "Multiplied" It should look like ## ### #### ##### ###### ####### ######## ######### – TylerW Jan 08 '16 at 12:47
  • @TylerW please edit your question instead of posting comments. And please explain the logic behind this. – Jabberwocky Jan 08 '16 at 12:49
  • @TylerW, OK now your question is a bit clearer. But it's still unclear how many lines of "##" you want. – Jabberwocky Jan 08 '16 at 12:55
  • @TylerW There is no any need to "multiply" a string that to output the figure you showed. Also what about strings containing several characters? How should the figure look fro example string "123"? – Vlad from Moscow Jan 08 '16 at 13:02
  • This may be because this is from a problem set for a class in CS. – TylerW Jan 08 '16 at 13:05
  • 1
    @TylerW I hope you have at least learned how to ask questionss. – Jabberwocky Jan 08 '16 at 13:07

3 Answers3

7

So you need 2 loops to do this. One for iterating through the characters you want to print on a line, one to iterate through the entire height (number of lines).

So what we want to do is:

  • Go through each line from 1 up to and including the height.
  • For each line, output as many #'s as the current line number

e.g.

 int lineno;
 int height  = GetInt();
 ... 
 for (lineno = 1; lineno <= height; lineno++) {
      int column;
      for (column = 0; column < lineno; column++) {
           putchar('#');
      }
     putchar('\n');
 }

This will be a left adjusted tree. I'll leave it up to you to right adjust it, i.e. print spaces in front of the '#', or start by printing 2 #'s instead of 1.

nos
  • 223,662
  • 58
  • 417
  • 506
1

You don't multiply a string, you simply use a loop and output the character repeatedly.

int j;
for (j = 0; j < (1 + height); ++j) {
    printf ("#");  /* Or putchar('#') */
}
printf ("\n");
Jens
  • 69,818
  • 15
  • 125
  • 179
0

Although C++ is not Python, you could try and implement a function that imitates string multiplication using string concatenation, like so:

string strmltply (string s, int multiplier) {
    string res;

    strcpy(res, s);

    // concatenate s (multiplier - 1) times
    for (int i = 1; i < multiplier; ++i) {
        strcat(res, s);
    }

    return res;
}

and then use it in a loop over the multiplier in your program, where each iteration you print the wanted string (in your case #) and then add a newline character \n.

Ziezi
  • 6,375
  • 3
  • 39
  • 49