0

Multi-Array and String C++

Hello sololearners, how can i make any string to accept on a multi-array? Make the string as a value of an array. Code: string Symbols = "@"; int Digits[1][1] = {Symbols}; cout << Digits[0][0];

17th Aug 2021, 10:38 AM
Al3xooom
Al3xooom - avatar
8 odpowiedzi
+ 2
Well, this one creates a char array to copy a std::string content #include <algorithm> // std::copy #include <iostream> #include <cstring> // std::strcpy int main() { std::string str { "Hello world!" }; char* data { new char[ str.size() + 1 ] }; std::copy( str.begin(), str.end(), data ); // C++ way //std::strcpy( data, str.c_str() ); // C way std::cout << data <<"\n"; delete [] data; // don't forget to clean up return 0; } * Credits: https://stackoverflow.com/questions/12361669/converting-an-stdstring-into-a-char-array https://www.geeksforgeeks.org/convert-string-char-array-cpp/
17th Aug 2021, 1:25 PM
Ipang
+ 2
But <Digits> is array of integer. I think this way you might want to use array of string/char instead.
17th Aug 2021, 11:04 AM
Ipang
+ 2
Let me get this straight first, <Symbol> is a string, you want to create an array to contain each character from <Symbol>. Or is it something different?
17th Aug 2021, 11:15 AM
Ipang
+ 2
No worries, you'll get the hang of it 👍 Here are references for std::copy and std::strlen https://en.cppreference.com/w/cpp/algorithm/copy https://en.cppreference.com/w/cpp/string/byte/strlen
17th Aug 2021, 1:30 PM
Ipang
+ 1
Deng, this is way too advance XD. Just for appreciation, imma mark it as the answer. Will try to implement it, thank you!
17th Aug 2021, 1:27 PM
Al3xooom
Al3xooom - avatar
0
What value did you expect in <Digits>? <Symbols> itself doesn't have any digit. I need more details on what you actually want here. So maybe you can put more examples in the Description.
17th Aug 2021, 10:57 AM
Ipang
0
When user input any kind of symbol/character which is a string, it will be the value of the multi-array. Ex. Please enter a symbol :$ Digits[0][0] = "
quot; So, when i call the multi-array Digits the value would be the string inputted.
17th Aug 2021, 11:01 AM
Al3xooom
Al3xooom - avatar
0
Yes, what would be the syntax for that? This is basically what I want to do but its error: String Digits[0][0] = Symbol;
17th Aug 2021, 11:06 AM
Al3xooom
Al3xooom - avatar