How do you convert a string to a char ?
How do you convert a string to a char ?
I'm not sure why you wish to do this because a string is just a char pointer more or less and as you can see you can go through them just like char arrays(because they are char arrays).Code:#include <iostream> #include <string> using namespace std; int main(){ string mystring; char mychar[50]; cout<<"Enter a string:"; getline(cin, mystring); for(int i=0;i<50;i++) mychar[i]=mystring[i]; cout<<mychar; cin.get(); return(0); }
-----
char m_sArray[100];
std::string m_sString = "freedom";
if (m_sString.size() < 100)
strcpy(m_sArray, m_sString.c_str(), m_sString.size());
-----
Kuphryn
I am using Textou and it still doesnt want to work with the code above. I am using dev c++ too.
heres my error:
98 untitled1.cpp
passing `char' to argument 4 of `TextOutA(HDC__ *, int, int, const CHAR *, int)' lacks a cast
you can't convert a string to a char. A string is a group of char. You can convert each char in the string into a separate, independent char if you wish:
char boo[3] = "pi";
char first;
char second;
char third;
boo is a string containg three char, two visible char (p and i) and one invsible (terminating null char).
first = boo[0];
second = boo[1];
third = boo[2];
This converts each char in boo to separate independent char types that can be used however you like, (you could have used the the left hand side representation any place you use the right hand representation too, but what the ----).
Boo is a character array holding 4 elements, 0-3. In this instance, the second and third characters hold a null terminator. Elements zero and one hold "pi";char boo[3] = "pi";
char first;
char second;
char third;
boo is a string containg three char, two visible char (p and i) and one invsible (terminating null char).
Obviously boo should be declared as: char boo[2] = "pi";
No, it shouldn't. [2] means that there are two(2) elements in the array. And now you are initializing three(3) elements there.Dual- Catfish wrote:
Obviously boo should be declared as: char boo[2] = "pi";
Code:$ cat >boo.cpp #include <iostream> int main () { char boo[2] = "pi"; std::cout<<boo<<std::endl; return 0; } $ g++ -o boo boo.cpp boo.cpp: In function `int main ()': boo.cpp:3: initializer-string for array of chars is too long
char boo[3] = "pi";
results in a char array having three elements
boo[0] == 'p'
boo[1] == 'i'
boo[2] == '\0'
If you did this:
char blah[2];
blah[0] = 'p';
blah[1] = 'i';
and then tried this:
cout << blah;
your compiler should complain.
if you did this however:
char blah[3];
blah[0] = 'p';
blah[1] = 'i';
blah[2] = '\0';
cout << blah;
everything would be fine. boo in initialized to a static string, whereas blah is a string assembled by assigning a single char at a time to a char array. Without the null char (aka, '\0' or just 0 which is zero, not capital oh) you don't have a string, you just have a char array.
/* This program copies a character array into a given array */
// this program uses 4 char ptrs
// copy ptr to other char
// convert a string to char [] and also set char ptr
#include <iostream.h>
#include <string>
using namespace std;
main( ){
char strA[80] = "A quick brown fox jumps over a lazy dog. A test string \n";
char strB[80];
char strC[100];
char strD[100];
string hello;
cout<< " Type the string \n ";
getline( cin, hello);
cout<< hello ;
char *ptrA; /* a pointer to type character */
char *ptrB; /* another pointer to type character */
char *ptrC;
char *ptrD;
ptrA = strA; /* point ptrA at string A */
ptrB = strB; /* point ptrB at string B */
ptrC = strC;
for(int i=0;i<100;i++)
strD[i]=hello[i];
ptrD = strD;
cout<< " \n Now type to add in strC , it will be over written soon, note only one word is stored , try more \n ";
cin >> strC;
cout<< " \n Just have a look at strC[] , as it is stored " << strC << endl;
while(*ptrA != '\0')
{
*ptrB++ = *ptrA++; // copying character by character
}
*ptrB = '\0';
while(*ptrD != '\0') {
*ptrC++ = *ptrD++; // copying character by character
} *ptrC = '\0';
cout << "\nString in strA = " << strA << endl; /* show strA on screen */
cout << "\n String in strB = " << strB << endl; /* show strB on screen */
cout << "\n String in strC = " << strC << endl; /* show strB on screen */
cout << "\n String in strD = " << strD << endl; /* show strB on screen */
system("pause");
}
Why are you resurrecting a 7 year old thread???
"I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008
"the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010