Learn C++ - C++ Strings






A string is a series of characters stored in consecutive bytes of memory.

C++ has two ways of dealing with strings: C-style string or a string class.

The last character of every C-style strings is the null character.

This character, written \0, is the character with ASCII code 0, and it serves to mark the string's end.

For example, consider the following two declarations:

char dog[8] = { '1', '1', '1', '1', '1', '1', '1', '1'};       // not a string! 
char cat[8] = {'1', '1', '1', '1', '1', '1', '1', '\0'};       // a string! 

char bird[11] = "this is";     // the \0 is understood 
char fish[] = "this is a test";          // let the compiler count 




Concatenating String Literals

C++ enables you to concatenate string literals into one.

cout << "one" " two.\n"; 
cout << "one" 
"two.\n"; 

Example


#include <iostream> 
#include <cstring>  // for the strlen() function 
using namespace std; 
int main() /* ww w  .  ja va 2 s  . c o  m*/
{ 
     const int Size = 15; 
     char name1[Size];               // empty array 
     char name2[Size] = "java2s.com";  // initialized array 

     cout << "I'm " << name2; 
     cout << "What's your name?\n"; 
     cin >> name1; 
     cout << name1 << ", your name has "; 
     cout << strlen(name1) << " letters and is stored\n"; 
     cout << "in an array of " << sizeof(name1) << " bytes.\n"; 
     cout << "Your initial is " << name1[0] << ".\n"; 
     name2[3] = '\0';                // set to null character 
     cout << "Here are the first 3 characters of my name: "; 
     cout << name2 << endl; 
     return 0; 
} 

The code above generates the following result.





Read String

The following code shows how to read more than one string.


#include <iostream> 
using namespace std; 
int main() /*from w w  w  .  ja  v  a 2 s.c  o  m*/
{ 
     const int SIZE = 20; 
     char name[SIZE]; 
     char dessert[SIZE]; 

     cout << "Enter your name:\n"; 
     cin >> name; 
     cout << "Enter your favorite dessert:\n"; 
     cin >> dessert; 
     cout << "I have some delicious " << dessert; 
     cout << " for you, " << name << ".\n"; 
     return 0; 
} 

The code above generates the following result.

Get line

The following code shows how to read more than one word with getline.


#include <iostream> 
using namespace std; 
int main() //from w  w w.j a  va  2s  . com
{ 
     const int SIZE = 20; 
     char name[SIZE]; 
     char dessert[SIZE]; 

     cout << "Enter your name:\n"; 
     cin.getline(name, SIZE);  // reads through newline 
     cout << "Enter your favorite dessert:\n"; 
     cin.getline(dessert, SIZE); 
     cout << "I have some delicious " << dessert; 
     cout << " for you, " << name << ".\n"; 
     return 0; 
} 

The code above generates the following result.

cin.Get

The following code shows how to read more than one word with get().


#include <iostream> 
using namespace std; 
int main() // w  ww. ja v  a 2s  .com
{ 
     const int SIZE = 20; 
     char name[SIZE]; 
     char dessert[SIZE]; 

     cout << "Enter your name:\n"; 
     cin.get(name, SIZE).get();    // read string, newline 
     cout << "Enter your favorite dessert:\n"; 
     cin.get(dessert, SIZE).get(); 
     cout << dessert; 
     cout << " for you, " << name << ".\n"; 
     return 0; 
} 

The code above generates the following result.