Gaurav K
Gaurav K

Reputation: 2975

Converting string to c-style string and detecting null terminating character

I am trying to convert a C++ string object to C-Style NULL terminated string using c_str() and then trying to access individual character as it can be done for c-style string.

#include <iostream>
#include <string>

using namespace std;

int main()
{
   string str1("Alpha");
   cout << str1 << endl;

   const char * st = new char [str1.length()+1];
   st = str1.c_str(); // Converts to null terminated string

   const char* ptr=st;

   // Manually Showing each character
   // correctly shows each character

   cout << *ptr << endl;
   ptr++;
   cout << *ptr << endl;
   ptr++;
   cout << *ptr << endl;
   ptr++;
   cout << *ptr << endl;
   ptr++;
   cout << *ptr << endl;
   ptr++;
   cout << "# Null Character :" << *ptr << endl;

   // But below loop does not terminate
   // It does not find '\0' i.e. null

   while( ptr != '\0')
   {
      cout << "*ptr : "<< *ptr << endl;
      ptr++;
   }
   return 0;
}

But seems like it does not add '\0' at the end and the loop does not terminate. Where I am going wrong ?

C-style string (e.g. char* st="Alpha";) can be accessed with the loop shown in the code but when the conversion from string object to c-style string happens, it can not.How do I do it?

Upvotes: 1

Views: 1666

Answers (4)

Gaurav K
Gaurav K

Reputation: 2975

This works fine.. Thanks for the responses.

 int main()
    {
        string str1("Alpha");
            cout << str1 << endl;




        const char * st = new char [str1.length()+1];
            st=str1.c_str();
           //strcpy(st, str1.c_str());//Copies the characters
           //throws error:
           //Test.cpp:19: error: invalid conversion from `const char*' to `char*'
           //Test.cpp:19: error:   initializing argument 1 of `char* strcpy(char*, const char*)'

            const char* ptr=st;


            while( *ptr != '\0')
                {
                    cout << "*ptr : "<< *ptr << endl;
                    ptr++;
            }
        return 0;
        }

Upvotes: 0

john
john

Reputation: 8027

Should be

    while( *ptr != '\0')
        {
            cout << "*ptr : "<< *ptr << endl;
            ptr++;
    }

and

    const char * st = new char [str1.length()+1];
    st=str1.c_str();//Converts to null terminated String

should be

    char * st = new char [str1.length()+1];
    strcpy(st, str1.c_str());//Copies the characters

or it could be

    const char * st = str1.c_str();//Converts to null terminated String

Your version is a bad mix of the two because it allocates memory as if it was going to copy the characters, but then doesn't copy anything.

You do realise you can access the individual characters of a std::string too? Just str1[0], str1[1], str1[i] etc.

Upvotes: 4

user195488
user195488

Reputation:

I think you are missing an asterisk here :

while( ptr != '\0')

to make it

while( *ptr != '\0')

You can also access each individual element of a string like this:

string helloWorld[2] = {"HELLO", "WORLD"};
char c = helloWorld[0][0];
cout << c;

You can also iterate over a string:

string str ("Hello World");
string::iterator it;
for (int index = 0, it = str.begin() ; it < str.end(); ++it)
   cout << index++ << *it;

or

string str ("Hello World");
string::iterator it;
for (int index = 0, it = str.begin() ; it < str.end(); ++it, ++index)
   cout << index << *it;

or

string str ("Hello World");
string::iterator it;
int index = 0;
for (it = str.begin() ; it < str.end(); ++it, ++index)
   cout << index << *it;

Understanding that you are looking for the null-terminating character in a C-style string, but if you have your druthers, stay with std::string.

Upvotes: 4

Fred Foo
Fred Foo

Reputation: 363617

while( ptr != '\0')

should be

while (*ptr != '\0')

Upvotes: 4

Related Questions