I am having some problem here.
let' say that
string c="Hello";
char a[5];
how can i convert the string to a character array? Urgent :D
Printable View
I am having some problem here.
let' say that
string c="Hello";
char a[5];
how can i convert the string to a character array? Urgent :D
you could increment your string and place all individually indexed location of the string into each individual location of the character array. you could do it with one loop, a string, and a char array. i unfortunatly do not recall any premade function to do this instantly.
The string class contains a function that returns the string as a character array:
Code:string c="Hello";
strcat(a, c.c_str()); // the character array a will now contain "Hello"
thank you
Code:#include <iostream>
#include <cstring>
#include <string>
int main()
{
std::string str1 = "Hello";
char str2[str1.length() + 1];
//we can't just use length that would result in not null terminating
std::strcpy(str2,str1.c_str());
std::cout<<"str1.length() = "<<str1.length()<<std::endl;//here is proof :)
std::cout<<"str1 = "<<str1<<std::endl;
std::cout<<"str2 = "<<str2<<std::endl;
std::cin.get();
return 0;
}