Link to home
Start Free TrialLog in
Avatar of skundu
skundu

asked on

"Char *" to String (URGENT Please) conversion

Hi,

I have a variable of type "char *". I want to copy its (this variable's) content to a string object. How can I do that?
Example:
char *mychar;
mychar = new char [100];
strcpy(mychar, "Hello World");
string mystring;
//How to copy mychar into mystring.
//I know, I can copy "Hello World" directly into mstring by writing mystring("Hello World"). But I need to copy the content of the "char *" variable, mychar, into the string, mystring.
How can I do this?
Help me if you know...
thanks,
sunanda
Avatar of yowkee
yowkee

string constructor could accept char* as argument, just simply mystring(mychar) .

Regards.
It's better check your compiler documentation before asking the question. Are you doing homework assignment?
Avatar of skundu

ASKER

Hi...
Thanks, but mystring(mychar) is not working. It says, that it does not get matching method in the string class...
Probably I need to see the compiler manual...
thanks..
skundu
If mystring is a class you designed, it looks like you need to overload operator = to accept right hand arguments of type char*, so that when you do an assignment like:

mystring string = "Hello world!";

it will perform the copy...

string& string::operator=(const char* s)
{
    ... do the internal copy here...
} // end operator=
Avatar of skundu

ASKER

Hi,

Thanks for your answer...
But even if you try to overload the "=" operator for the string class, in the overloaded method you need to somehow copy the content of s ("char *" variable) into the LHS that is a string. And that is the problem! Because I cannot write mystring(mychar), nor can I write mystring = mychar !!

But thank you....I will try somehow...
sunanda
I was checking the Dinkum C++ reference, and, if that is right and complete, the standard doesn't require a string to be created from a char *, but it is required for a char * and an int (actually a size_t or something like that :), so this should hopefully work:

string mystring(mychar, strlen(mychar));

Anyway, I have yet to see a library implementation that doesn't support that, so your library may be garbage :) (so you may think about getting gcc or its ports :)

Orlando
This is incorrect.  According to the C++ standard the STL string may be constructed from a char * parameter (the size_t is not needed) also it may be assigned a value from a char * parameter.  so you should be able to do

char *mychar;
mychar = new char [100];
strcpy(mychar, "Hello World");

string mystring1(mychar);

or

mystring = mychar;

If this is not working, it should be.  Whay don't you tell us what errors you are getting with this.
skundu,

  As nietod said, provide the error message you got. And what's the compiler you use? Did you include the correct header?
can you put your code and error here.then we will help you.

suresh kumar
The following test program works when compiled with Visual C++ 6.0 as
follows:

cl -GX test.cpp

Things to check:
make sure your includes are declared correctly (no .h's)
make sure you have "using namespace std" statement

#include <iostream>
#include <string>

using namespace std;

void main()
{
   char mychar[100];
   strcpy(mychar, "Hello, World");
   string mystring;

   mystring = mychar;

   cout << "Results: mychar =" << mychar << "  mystring =" << mystring << endl;
}
Avatar of skundu

ASKER

Hi...
Thank a lot for helping me..
But I have solved my problem (programming assignment) by some other way (not going into the conversion from char* to string), rather by string to char* using the method c_str(), and I could solve my problem perfectly....

Thank you all very much...
sunanda
ASKER CERTIFIED SOLUTION
Avatar of Prism
Prism

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Prism,

  skunda said he solved the problem, so you shouldn't answer.  You also should not answer if you don't have an answer.  You just made some guesses.  

>>If you are in Unix environment you don't need it at all.
Why would that be?  C++ is OS-independant.
oh..hmm... I didn't see it. Sorry.. Maybe I skipped it. I wouldn't have bothered to answer the question otherwise. I wonder if I can delete my answer...I'll try it

>>If you are in Unix environment you >don't need it at all.
>Why would that be?  C++ is OS->independant.

Why would that be?
Because Unix (my experience up to now) doesn't name the new libraries by std::string (example), string is just enough. Actually it gives errors if you try to compile std::anything. Thats what i meant.

And... C++ is not OS-independant, not in the real world. It should be (theorically), it is almost. But not quite. I've been porting a proyect lately (you helped me with a couple of questions, actually :) ) and you have to tweak the code quite a bit to make it work in both platforms.
>> string is just enough
That just means that you are using a very old compiler that doesn't support namespaces.  Old compilers on ANY platform will not support namespaces.  Up-to-date compilers on any platform including UNIX, which has some of the most up-to-date compilers, will support namespaces.

>>  C++ is not OS-independant,
It is 100% platform independant.  The C++ standard has no provision for any specific platrform and includes no operations that cannot be performed on virtually any OS ever divised.  For example, it has no features for handling directories, such as enumerating files in directories, deleting files, determine a file's attributes, getting a file's date, etc because not all OSs support these sorts of concepts.