Facebook Page Twitter Page LinkedIn Page
× C++ Tutorial C++ Advanced C++ Resources


C++ Strings

Strings are one of the most common data types in C++. It is used for storing text. It can be created by enclosing characters in double quotation marks. It can be assigned to a variable using = sign.

string MyString = "Hello World!";

Access character of a String

A character of a string can be accessed with it's index number. In C++, index number starts with 0 in forward direction. The below figure and example describe the indexing concept of a string.

String Indexing:

C++ String Indexing

The below example describes how to access character of a string using its index number.

#include <iostream>
#include <string>
using namespace std;
 
int main (){
  string MyString = "HELLO";
  cout<<MyString[1]<<"\n";
  cout<<MyString[4]<<"\n";
  return 0;
}

The output of the above code will be:

E
O   

String Length

The C++ string has two functions, size() and length(), which can be used to find the total number of characters in the String.

#include <iostream>
#include <string>
using namespace std;
 
int main (){
  string MyString = "Hello World!";
  cout<<MyString.size()<<"\n";
  cout<<MyString.length()<<"\n"; 
  return 0;
}

The output of the above code will be:

12
12

String Concatenation

In C++, two strings can be joined using + operator. Along with this, C++ String has append() function which can also be used to combine two strings.

#include <iostream>
#include <string>
using namespace std;
 
int main (){
  string str1 = "Hello";
  string str2 = " World!";

  cout<<str1 + str2<<"\n";
  cout<<str1.append(str2)<<"\n"; 
  return 0;
}

The above code will give the following output:

Hello World!
Hello World!

Special Strings in String

The backslash \ escape character is used to convert special character like single quote, double quote, new line, etc. into the string character. The below mentioned table describes special characters in C++:

Escape CharacterResultExample
\' ' "\'C++\'" is converted into: 'C++'
\" " "\"Hello\"" is converted into: "Hello"
\\ \ "A\\C" is converted into: A\C
\n new line "Hello\nJohn" is converted into:
Hello
John
\t Tab "Hello\tMarry" is converted into: Hello    Marry
\b Backspace "Hello C\b++" is converted into: Hello ++

String Functions

C++ has number of string functions. Here, few very common string functions are discussed.

  • clear(): Clears all characters of the string.
  • empty(): Checks whether the string is empty or not.
  • front(): Access first character of the string.
  • back(): Access last character of the string.

Example: clear() and empty() string functions

In the below example, the C++ string functions - clear() and empty() are discussed.

#include <iostream>
#include <string>
using namespace std;
 
int main (){
  string MyString = "HELLO";
  if (!MyString.empty()) {
    cout<<"MyString contains: "<<MyString<<"\n";
  } else {
    cout<<"MyString is empty.\n";
  }

  MyString.clear();
  cout<<"\nclear() function is applied on MyString.\n";
  if (!MyString.empty()) {
    cout<<"MyString contains: "<<MyString<<"\n";
  } else {
    cout<<"MyString is empty.\n";
  }

  return 0;
}

The above code will give the following output:

MyString contains: HELLO

clear() function is applied on MyString.
MyString is empty.

Example: front() and back() string functions

In the below example, the C++ string functions - front() and back() are explained.

#include <iostream>
#include <string>
using namespace std;
 
int main (){
  string MyString = "HELLO";

  cout<<"MyString contains: "<<MyString<<"\n"; 
  cout<<"First character of MyString is: "<<MyString.front()<<"\n";
  cout<<"Last character of MyString is: "<<MyString.back()<<"\n";

  return 0;
}

The above code will give the following output:

MyString contains: HELLO
First character of MyString is: H
Last character of MyString is: O