CPP Tutorial

CPP examples programs

CPP Strings

A sequence of characters, internally stored in the form of an array of characters is called as string.

Declaration of String:

In CPP, a string can be specified in two ways:

Character Array:

A string declared as an array, where each character represents an element of that array, is called as Character Array.

Syntax:

char array_Name[] = {char1,char2,char3………};

String Object:

To represent a sequence of characters, there is an object defined in std::string class, called as String Object.

Syntax:

string string_name = “stringValue”;

Example :

#include <iostream.h>  
using namespace std;  
int main( )
{  
char chr[] = { 'H', 'E', 'L', 'L', 'O', ' ', 'C', 'P', 'P'};  
string str1 = "Hello CPP";    
string str2 = string(chr);    
cout<<str1<<endl;    
cout<<str2;    
}

Output

Hello CPP       
HELLO CPP