How to convert C style strings to std::string and vice versa?
Last Updated :
06 Jul, 2017
What are C style strings?
These strings are array of characters terminating with a NULL character. C style strings can be declared in following ways:
Declaration and initialization
#include<iostream>
using namespace std;
int main()
{
char str1[8] = { 'H' , 'E' , 'L' , 'L' , 'O' ,
'-' , '1' , '\0' };
char str2[] = "HELLO-2" ;
const char *str3 = "HELLO-3" ;
cout << str1 << endl << str2 << endl << str3;
return 0;
}
|
Output:
HELLO-1
HELLO-2
HELLO-3
C style strings are operated with very useful functions like strcpy(), strlen(), strpbrk(), strcat(), strstr() and many more!(All these functions are member functions of ‘cstring‘ header ).
What is a std::string?
C++ standard library contains functions and classes. String is one of its classes. Here we deal with an object of string class. This std::string takes care of itself and manages its own memory.
Declaration and initialization
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s;
s = "HELLO" ;
cout << s;
return 0;
}
|
Output:
HELLO
Converting C-String to a std::string.
But why do we need this transformation? From a C string to a std::string? It is because
- Std::string manages its own space. So programmer don’t need to worry about memory , unlike C strings (Since they are array of characters)
- They are easy to operate. ‘+’ operator for concatenation, ‘=’ for assignment, can be compared using regular operators.
- string::find() and many other functions can be implemented on std::string and not on C-Strings so this becomes handy.
- Iterators can be used in std::string and not in C-strings.
And many more! Here is the code for it:-
#include<bits/stdc++.h>
using namespace std;
int main()
{
const char *a = "Testing" ;
cout << "This is a C-String : " << a << endl;
string s(a);
cout << "This is a std::string : " << s << endl;
return 0;
}
|
Output:
This is a C-String : Testing
This is a std::string : Testing
The above conversion also works for character array.
// Character array to std::string conversion
char a[] = "Testing";
string s(a);
Converting a std::string to a C style string
Why do we need this transformation? From std::string to a C string?
- It is because there are several powerful functions in header that makes our work very much easier.
- atoi() , itoa() , and many more functions work with C strings only.
You can think of other reasons too!
Here is the code for conversion:-
#include<iostream>
#include<string> /* This header contains string class */
using namespace std;
int main()
{
string s = "Testing" ;
cout << "This is a std::string : " << s << endl;
char *a = &(s[0]);
printf ( "%s\n" , a);
return 0;
}
|
Output:
This is a std::string : Testing
This is a C-String : Testing
std::string also has a function c_str() that can be used to get a null terminated character array.
#include<bits/stdc++.h>
using namespace std;
int main()
{
string s = "Testing" ;
cout << "This is a std::string : " << s << endl;
const char *a = s.c_str();
printf ( "%s\n" , a);
return 0;
}
|
Output:
This is a std::string : Testing
This is a C-String : Testing
Both C strings and std::strings have their own advantages. One should know conversion between them, to solve problems easily and effectively.
Related articles:
C++ string class and its applications | Set 1
C++ string class and its applications | Set 2
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...