Ways to Define a String in C++

Strings can be defined in several ways in C++. Strings can be accessed from the standard library using the string class. Character arrays can also be used to define strings. String provides a rich set of features, such as searching and manipulating, which are commonly used methods. Despite being less advanced than the string class, this method is still widely used, as it is more efficient and easier to use. Ways to define a string in C++ are:

  • Using String keyword
  • Using C-style strings

1. Using string Keyword

It is more convenient to define a string with the string keyword instead of using the array keyword because it is easy to write and understand.

Syntax:

string s = "w3wiki";
string s("w3wiki");

Example:

C++




// C++ Program to demonstrate use of string keyword
#include <iostream>
using namespace std;
 
int main()
{
 
    string s = "w3wiki";
    string str("w3wiki");
 
    cout << "s = " << s << endl;
    cout << "str = " << str << endl;
 
    return 0;
}


Output

s = w3wiki
str = w3wiki

2. Using C-style strings

Using C-style string libraries functions such as strcpy(), strcmp(), and strcat() to define strings. This method is more complex and not as widely used as the other two, but it can be useful when dealing with legacy code or when you need performance.

char s[] = {'g', 'f', 'g', '\0'};
char s[4] = {'g', 'f', 'g', '\0'};
char s[4] = "gfg";
char s[] = "gfg";

Example:

C++




// C++ Program to demonstrate C-style string declaration
#include <iostream>
using namespace std;
 
int main()
{
 
    char s1[] = { 'g', 'f', 'g', '\0' };
    char s2[4] = { 'g', 'f', 'g', '\0' };
    char s3[4] = "gfg";
    char s4[] = "gfg";
 
    cout << "s1 = " << s1 << endl;
    cout << "s2 = " << s2 << endl;
    cout << "s3 = " << s3 << endl;
    cout << "s4 = " << s4 << endl;
 
    return 0;
}


Output

s1 = gfg
s2 = gfg
s3 = gfg
s4 = gfg

Another example of C-style string:

C++




#include <iostream>
using namespace std;
 
int main()
{
    string S = "Geeeks for Geeks";
    cout << "Your string is= ";
    cout << S << endl;
 
    return 0;
}


Output

Your string is= Geeeks for Geeks

Strings in C++

C++ strings are sequences of characters stored in a char array. Strings are used to store words and text. They are also used to store data, such as numbers and other types of information. Strings in C++ can be defined either using the std::string class or the C-style character arrays.

1. C Style Strings

These strings are stored as the plain old array of characters terminated by a null character ‘\0’. They are the type of strings that C++ inherited from C language.

Syntax:

char str[] = "w3wiki";

Example:

C++




// C++ Program to demonstrate strings
#include <iostream>
using namespace std;
 
int main()
{
 
    char s[] = "w3wiki";
    cout << s << endl;
    return 0;
}


Output

w3wiki

2. std::string Class

These are the new types of strings that are introduced in C++ as std::string class defined inside <string> header file. This provides many advantages over conventional C-style strings such as dynamic size, member functions, etc.

Syntax:

std::string str("w3wiki");

Example:

C++




// C++ program to create std::string objects
#include <iostream>
using namespace std;
 
int main()
{
 
    string str("w3wiki");
    cout << str;
    return 0;
}


Output

w3wiki

One more way we can make strings that have the same character repeating again and again.

Syntax:

std::string str(number,character);

Example:

C++




#include <iostream>
using namespace std;
 
int main()
{
    string str(5, 'g');
    cout << str;
    return 0;
}


Output:

ggggg

Similar Reads

Ways to Define a String in C++

...

How to Take String Input in C++

...

How to Pass Strings to Functions?

...

Pointers and Strings

Strings can be defined in several ways in C++. Strings can be accessed from the standard library using the string class. Character arrays can also be used to define strings. String provides a rich set of features, such as searching and manipulating, which are commonly used methods. Despite being less advanced than the string class, this method is still widely used, as it is more efficient and easier to use. Ways to define a string in C++ are:...

Difference between String and Character array in C++

...

C++ String Functions

...

Contact Us