converting c style strings to c++ strings

This is a discussion on converting c style strings to c++ strings within the C++ Programming forums, part of the General Programming Boards category; is there any way to convert c style strings to c++ strings? I know you can go the other way, ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    6

    converting c style strings to c++ strings

    is there any way to convert c style strings to c++ strings? I know you can go the other way, I was just curious as to whether or not you could go this way.

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Code:
    	string text_string;
    	char text[]="hello";
    	for(int i=0; i<strlen(text); i++)
    		text_string += text[i];
    	cout<<text_string<<endl;

  3. #3
    jasondoucette.com JasonD's Avatar
    Join Date
    Mar 2003
    Posts
    278
    I'm surprised it doesn't have a member function that can do this, or even a constructor that allows this.

    ....

    I just looked it up. There exists a constructor like so:

    string( const char *str );

    You can use this.

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    "I just looked it up."

    I suspected there was, but it took me less time to write the code than it would have taken to look it up. All the string functions are very easy to define yourself.

    [edit]
    Code:
    char text[]="hello";
    string text_string(text);
    cout<<text_string<<endl;
    Last edited by 7stud; 04-13-2003 at 08:06 PM.

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    couldn't be easier:

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
        char cStr[] = "muddy waters";
        string cPlusStr(cStr);
    
        cout << cPlusStr << endl;
    
        return 0;
    }

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    6
    Thanks for the replies, but oddly enough, the hardest solution fit my purposes the best.

  7. #7
    Registered User
    Join Date
    Feb 2003
    Posts
    28
    Originally posted by fbplayr78
    Thanks for the replies, but oddly enough, the hardest solution fit my purposes the best.
    Greetings,

    std::string also has the '=' operator defined that you can use like:
    Code:
    #include <iostream>
    #include <string>
    
    int main
    {
        std::string str1("Hello");   // The constructor
        std::string str2 = "Hello";  // This is the same as the above because in both cases the compiler 
                                     // will invoke the constructor not the '=' operator as one might think,  
                                     // this because this string object is still being created. 
        std::string str3;            // An empty string
    
        str3 = "world!";             // The '=' operator
    
        std::cout << str1 << str3 << std::endl;
    }
    I fail to see how iterating through a C string and appending every char to an std::string works better than just using the constructor or the '=' operator, because the result will be exactly the same! Except for the overhead of calling the '+=' operator in every iteration. Unless you are parsing the C string for 'valid' chars, I say use the other methods.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting Integers to Strings
    By momo97 in forum C++ Programming
    Replies: 5
    Last Post: 03-04-2009, 12:36 AM
  2. Converting strings to char
    By Furious5k in forum C++ Programming
    Replies: 13
    Last Post: 01-02-2009, 04:26 PM
  3. converting a vector of strings into an array.
    By LightsOut06 in forum C++ Programming
    Replies: 2
    Last Post: 10-27-2005, 07:14 PM
  4. Converting C++ Style strings...
    By LightsOut06 in forum C++ Programming
    Replies: 10
    Last Post: 10-26-2005, 03:02 PM
  5. array of strings + more
    By null in forum C Programming
    Replies: 10
    Last Post: 10-01-2001, 03:39 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21