Multiplying a String by an Int in C++

How can I multiply a string in the C language?

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.

Is there a way to multiply a string and an integer in C++?

There is no such operator in C++.

But you can write a function that takes a string and a number as argument, and return the repeated string.

std::string also has a constructor that repeats given character a number of times. This may be useful since you used a string of one character as the example.

Multiplying a string by an int in C++

No, std::string has no operator *. You can add (char, string) to other string. Look at this http://en.cppreference.com/w/cpp/string/basic_string

And if you want this behaviour (no advice this) you can use something like this

#include <iostream>
#include <string>

template<typename Char, typename Traits, typename Allocator>
std::basic_string<Char, Traits, Allocator> operator *
(const std::basic_string<Char, Traits, Allocator> s, size_t n)
{
std::basic_string<Char, Traits, Allocator> tmp = s;
for (size_t i = 0; i < n; ++i)
{
tmp += s;
}
return tmp;
}

template<typename Char, typename Traits, typename Allocator>
std::basic_string<Char, Traits, Allocator> operator *
(size_t n, const std::basic_string<Char, Traits, Allocator>& s)
{
return s * n;
}

int main()
{
std::string s = "a";
std::cout << s * 5 << std::endl;
std::cout << 5 * s << std::endl;
std::wstring ws = L"a";
std::wcout << ws * 5 << std::endl;
std::wcout << 5 * ws << std::endl;
}

http://liveworkspace.org/code/52f7877b88cd0fba4622fab885907313

How do you multiply a character in C like Python?

None. It's not in the C language.

But you can get them from library which is pretty useful. For your question, you can define an char array and use memset(doc).

char str[9];
memset(str, 'g', 8);
str[8] = '\0';

Then the str is "gggggggg". str[8] should be a terminal \0 when represents string.

Can I multiply a string (in C#)?

In .NET 4 you can do this:

String.Concat(Enumerable.Repeat("Hello", 4))

Multiply char by integer (c++)

I wouldn't call that operation "multiplication", that's just confusing. Concatenation is a better word.

In any case, the C++ standard string class, named std::string, has a constructor that's perfect for you.

string ( size_t n, char c );

Content is initialized as a string formed by a repetition of character c, n times.

So you can go like this:

char star = '*';  
int num = 7;
std::cout << std::string(num, star) << std::endl;

Make sure to include the relevant header, <string>.

C - Multiply char by int

for ( size_t ii = 0; ii < 5; ++ii )
putchar('#');

String and integer multiplication in C++

'10' is a multicharacter literal; note well the use of single quotation marks. It has a type int, and its value is implementation defined. Cf. "10" which is a literal of type const char[3], with the final element of that array set to NUL.

Typically its value is '1' * 256 + '0', which in ASCII (a common encoding supported by C++) is 49 * 256 + 48 which is 12592.



Related Topics



Leave a reply



Submit