"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
skunduAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Regards.
0
yowkeeCommented:
It's better check your compiler documentation before asking the question. Are you doing homework assignment?
0
skunduAuthor Commented:
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
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

captainkirkCommented:
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=
0
skunduAuthor Commented:
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
0
curriCommented:
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
0
nietodCommented:
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.
0
yowkeeCommented:
skundu,

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

suresh kumar
0
rgoughCommented:
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;
}
0
skunduAuthor Commented:
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
0
PrismCommented:
Nietod is right, I am working with this kind of operation in a daily base. If it is not working is because you are doing something wrong. Maybe you are not including the right .h files. Or maybe you are not using the stl files.


#include <string> // no .h
using std::string;  

int main()
{
char *mychar;
mychar = new char [100];
strcpy(mychar, "Hello World");
string mystring;
string = mychar;
}

if you don't put the

'using std::string';

line, you have to write

'std::string' instead of 'string'

every single time you use it.
If you are in Unix environment you don't need it at all.





0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
nietodCommented:
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.
0
PrismCommented:
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.
0
nietodCommented:
>> 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.  
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.