This question already has answers here:
How to convert a char array to a string?
(5 answers)
Closed 9 years ago.
I'm trying to find how to convert a char array into a std::string, but I can't find anything to do this.
If it is impossible, could you explain me how can I read an std::string but by scanf() and gets() I can't.
Best regards.
The simplest way of making a string from a character array is using a constructor that takes a pointer and the length, like this:
char charArray[] = {'w', 'x', 'y', 'z'};
std:string s(&charArray[1], 2); // s is "xy"
// ^ ^
// | |
// Pointer to the |
// starting position Length
Related
This question already has answers here:
How to convert string to wstring in C++
(1 answer)
How to convert char* to wchar_t*?
(9 answers)
How to convert UTF-8 std::string to UTF-16 std::wstring?
(6 answers)
Closed 2 months ago.
This post was edited and submitted for review 2 months ago and failed to reopen the post:
Original close reason(s) were not resolved
If I have this below code -
string str;
wstring wstr;
for (char x : str)
{
wstr += x;
}
Is this line wstr += x wrong? Do I need some conversion function to convert char x to wchar_t to be stored in wstr? If yes, which conversion function do I need?
Edit - I have gone through the linked answers, it mentions about converting the array of wchar_t and char -> for that for sure conversion functions are needed but my question specifically asks if a char -> wchar_t would need conversion function
This question already has answers here:
C or C++. How to compare two strings given char * pointers?
(8 answers)
Closed 4 years ago.
If I have a char* that is the output of another function, and I know it is one of the 10 known words, what is the best way to find what is it?
converting the char* to string bystd::string(char*) , then using string.compare() ?
char* c = "hi";
string s = std::string(c);
if (s.compare("hello") )
Is this the best way? I can not directly write:
char* c ="hi";
if(c == "hello")
Since you already have a C string, just use strcmp. It will likely be faster than the s.compare method since you avoid the overhead of doing a conversion to std::string for both the original string and the string to compare to.
if (strcmp(c, "hello") == 0) {
...
This question already has answers here:
What is the difference between 'a' and "a"?
(6 answers)
Closed 8 years ago.
What is the difference between 'x' and "x"?
Does 'x' mean it is a char value and "x" mean it is a string value?
very sorry for the similarity to the other qn as I don't really get the explanation over there as it is too complicated.
The literal 'x' is a char. The literal "x" is a string literal of type const char[2], a null-terminated char array holding values x and \0.
your assumption is correct,
"x" is a string
'x' is a char
'x' means a char with value 'x'.
"x" means a c-type const char array with value {'x', 0}
In C and C++, "x" is of type const char[] which is an array and it is null-terminated (0x00). Whilst 'x' is of type char.
The word 'string' is a little bit ambiguous, because it can mean two things -
A string of characters (const char[])
The datatype std::string. C++ has support in the standard library for the String datatype, which can be assigned a const char[].
Just to clarify:
"I am"
Is actually this:
{'I', ' ', 'a', 'm', '\0'}
This question already has answers here:
C++ convert char to const char*
(5 answers)
Closed 10 years ago.
I need to convert a character in a character array to a const char * in order to print it to a file using fstream. I'm not sure exactly how to do so. I've tried putting the single char into a string, then using c_str(), but that does not work..
If you want to write a single character, just use operator<<:
char arr[256] = "...";
fstream f(...);
f << arr[2];
You don't need to convert the character to a C string.
Hm... If you have a character array, that already decays into char * when passed to a function.
If you need only one character:
char array[128]; // whatever - you want to extract the char from this
char s[] = { array[64], 0 };
then use s which now can decay into char *.
Edit: D'oh, I just read this:
in order to print it to a file using fstream
Well, then don't bother converting it to a proper C string. operator<< knows its job, and it's overloaded for char too.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Convert char array to single int?
How to convert char array to uintmax_t?
char array contains the uintMax_t value but in string format.
Thank you.
You need to use atoi or atol .