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
Filestorage for Opencv Python API
Initialize Std::Array with a Range (Pair of Iterators)
In C++ I Cannot Grasp Pointers and Classes
C++ Crashes in a 'For' Loop with a Negative Expression
How to Vectorize My Loop with G++
C++, Function Pointer to the Template Function Pointer
Opencv Read Jpeg Image from Buffer
What Are Differences Between Std, Tr1 and Boost (As Namespaces And/Or Libraries)
Are All Temporaries Rvalues in C++
What Is the Size of a Pointer? What Exactly Does It Depend On
Why Does the "Static" Keyword Have So Many Meanings in C and C++
How Can Boost::Serialization Be Used with Std::Shared_Ptr from C++11
How to Generate a Random Number Using the C++11 Standard Library
Is Std::String Thead-Safe with Gcc 4.3
Use #Ifdefs and #Define to Optionally Turn a Function Call into a Comment