Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 16th, 2006, 11:42 PM   #1
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 936
Rep Power: 4 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
char to string in C++

How do you initialize a string as a character in C++?

In Java you would use:

char c = 'c';
String str = Character.toString(c);

Also, how do you convert a character to a string in general?
titaniumdecoy is offline   Reply With Quote
Old Feb 17th, 2006, 12:35 AM   #2
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 770
Rep Power: 3 Jimbo is on a distinguished road
There is a string constructor that takes a C-string and one that takes a char array and a length (c-string being a null-terminated char array or string literal). If you know what the letter you want is, you can just use:
string str("c");
or if you want it to depend on a variable, whether user input or whatever, you can just use:
char c = /* whatever */;
string str(&c, 1);
Jimbo is offline   Reply With Quote
Old Feb 17th, 2006, 12:39 AM   #3
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 936
Rep Power: 4 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
Thanks for your help.

Also, how do you append characters (chars) to a string?
titaniumdecoy is offline   Reply With Quote
Old Feb 17th, 2006, 12:54 AM   #4
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 770
Rep Power: 3 Jimbo is on a distinguished road
string has an operator+= defined, which as I remember takes another string or the same char* arguments listed above
Jimbo is offline   Reply With Quote
Old Feb 17th, 2006, 12:56 AM   #5
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 936
Rep Power: 4 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
Why doesn't this code give me the first 2 characters of str1 as a string?

string str1("27582347502458270");
string s(str1[0], 1);
s.append(&str1[1]);
cout << s << endl;
titaniumdecoy is offline   Reply With Quote
Old Feb 17th, 2006, 1:04 AM   #6
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 770
Rep Power: 3 Jimbo is on a distinguished road
first off, remember that the constructor takes a char* not a char (as operator[] returns), so the 2nd line should probably be
string s(str1.c_str(),1); // get the char*
// or an alternative
string s(&str[0], 1); // get a pointer to the first char

for the append, unless you specifiy a length, it will append data until it reaches a null character (with integer value 0). Since the string is (I believe) stored as a char* in memory, it keeps reading until it reaches the end, hence you have more than 2 characters. That's easily solved with
s.append(&str[1], 1);
Jimbo is offline   Reply With Quote
Old Feb 17th, 2006, 1:40 AM   #7
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 936
Rep Power: 4 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
Thanks again, Jimbo.

Is there a website that has the official documentation for C++? For Java there is http://java.sun.com/j2se/1.3/docs/api/java/lang/. Thanks.
titaniumdecoy is offline   Reply With Quote
Old Feb 17th, 2006, 1:49 AM   #8
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 770
Rep Power: 3 Jimbo is on a distinguished road
I usually just use MSDN, especially since I use Visual Studio. I believe Microsoft's implementation is starting to follow the standard well enough to use it as a general reference anyways. Otherwise, just google around. I think there's another website like cplusplus.com or something like that, which has some decent documentation on a few standard header functions. I don't think there's a standard site, however.

By the way, your link for Java is a bit old... http://java.sun.com/j2se/1.5.0/docs/api/ is a little more like it...
Jimbo is offline   Reply With Quote
Old Feb 17th, 2006, 2:08 PM   #9
Polyphemus_
Expert Programmer
 
Polyphemus_'s Avatar
 
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4 Polyphemus_ is on a distinguished road
IIRC, you can just do this, since the stl string overloads the = operator:

char *str = "Some C-style string";

string cppStr = str;
Polyphemus_ is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:57 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC