How can I multiply a string in the C language?

TylerW

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.

nos

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How i can Detect type of language in a string?

From Dev

How can I multiply table?

From Dev

How can I multiply really big numbers c++

From Dev

How do I multiply this in C?

From Dev

How I can use logarithms in C language

From Dev

In Haskell how can you multiply a string?

From Dev

Can I multiply an int with a boolean in C++?

From Dev

How can I multiply two arrays in Ruby?

From Dev

How can I split a String in Expression Language and take the last element?

From Dev

How can I show a string resource in a language and store it in other?

From Dev

How to insert a number in the place I need in a string, in the C programming language

From Dev

How can I provide garbage collection for an interpreted language implemented in C?

From Dev

How can I make multi-language program with C++

From Dev

how can I get char array size in function (language c)?

From Dev

How can I return a string properly in C

From Dev

In Python, why can I multiply a string by a number, but I can't add a string and a number?

From Dev

How to search for String in C language

From Dev

How can I convert an ISO language code to its language name in Objective-C?

From Dev

How can I convert "year-month-day" string of date, to day as a string in user local language

From Dev

How can I multiply or divide value under the cursor by a repeat count?

From Dev

How can I multiply a range specified by a variable with its offset in VBA?

From Java

How can I multiply row under certain condition with R?

From Dev

How can I multiply only the cells that contain value?

From Dev

How can I multiply a range specified by a variable with its offset in VBA?

From Dev

How can I multiply a column by a variable and write out?

From Dev

How can I "multiply" rows to every row in another table with MSSQL

From Dev

How can I multiply numbers(data) in one column in MySQL?

From Dev

How can I multiply std::tuple to some value?

From Dev

How can I concatenate string + int + string in C++?

Related Related

  1. 1

    How i can Detect type of language in a string?

  2. 2

    How can I multiply table?

  3. 3

    How can I multiply really big numbers c++

  4. 4

    How do I multiply this in C?

  5. 5

    How I can use logarithms in C language

  6. 6

    In Haskell how can you multiply a string?

  7. 7

    Can I multiply an int with a boolean in C++?

  8. 8

    How can I multiply two arrays in Ruby?

  9. 9

    How can I split a String in Expression Language and take the last element?

  10. 10

    How can I show a string resource in a language and store it in other?

  11. 11

    How to insert a number in the place I need in a string, in the C programming language

  12. 12

    How can I provide garbage collection for an interpreted language implemented in C?

  13. 13

    How can I make multi-language program with C++

  14. 14

    how can I get char array size in function (language c)?

  15. 15

    How can I return a string properly in C

  16. 16

    In Python, why can I multiply a string by a number, but I can't add a string and a number?

  17. 17

    How to search for String in C language

  18. 18

    How can I convert an ISO language code to its language name in Objective-C?

  19. 19

    How can I convert "year-month-day" string of date, to day as a string in user local language

  20. 20

    How can I multiply or divide value under the cursor by a repeat count?

  21. 21

    How can I multiply a range specified by a variable with its offset in VBA?

  22. 22

    How can I multiply row under certain condition with R?

  23. 23

    How can I multiply only the cells that contain value?

  24. 24

    How can I multiply a range specified by a variable with its offset in VBA?

  25. 25

    How can I multiply a column by a variable and write out?

  26. 26

    How can I "multiply" rows to every row in another table with MSSQL

  27. 27

    How can I multiply numbers(data) in one column in MySQL?

  28. 28

    How can I multiply std::tuple to some value?

  29. 29

    How can I concatenate string + int + string in C++?

HotTag

Archive