C++ Strings

A string is an object of built-in class string that holds a sequence of characters surrounded by double quotes.

A string is a sequence of characters surrounded by double quotes. The C++ programming language supperts the following two types of string concepts.

  • String as character array
  • String as object of string class

String as character array

In C++, a string may define as a character array. A character array is simply an array of characters that is terminated by a null character "\0".

Example
#include <iostream>

using namespace std;

int main()
{
    char name[30] = {'R', 'a', 'm', 'a'};   // string as an array

    cout << "Hello, " << name << endl;

    return 0;
}
  • The C++ compiler automatically places the '\0' at the end of the string when it initializes the array.
  • A character array string allows us to access individual characters of the string using index numbers.
  • The size of the character array is fixed at compile time.

String as object of string class

The C++ standard library provides a built-in class string. The string class allows us to create an object that holds a sequence of characters - string.

Example code
#include <iostream>

using namespace std;

int main()
{
    string name = "Rama";   // string as an object

    cout << "Hello, " << name << endl;  // Accessing hole string

    cout << name[2] << endl;    // Accessing a character from string

    name[2] = 'n';  // Changing a character at index 2

    cout << "Hello, " << name << endl;

    return 0;
}

String handling functions in C++

The C++ has a standard library header file string.h. The string.h provides various built-in functions to work with string values.

  • strcpy(string s1, string s2)
    - Used to copy string s2 into string s1
  • strcat(string s1, string s2)
    - Used to perform the concatination of two strings
  • strlen(string s)
    - Used to find the length of a string (total number of characters)
  • strchr(string s, char ch)
    - Returns a pointer the first occurance of the given character in the string s.
  • strstr(string s1, string s2)
    - Returns a pointer the first occurance of the string s2 in the string s1.
  • strcmp(string s1, string s2)
    - Compares both string s1 and string s2 and returns 0 if both are the same; less than 0 if s1<s2; greater than 0 if s1>s2.

  • int capacity(string s)
    - It returns the capacity allocated to the string s, which can be equal to or more than the size of the string.
  • swap(string s)
    - It returns the capacity allocated to the string s, which can be equal to or more than the size of the string.
  • resize(int newSize)
    - It changes the size of string, the size can be increased or decreased.
  • shrink_to_fit()
    - It decreases the capacity of the string and makes it equal to the minimum capacity of the string.
Example
#include <iostream>

using namespace std;

int main()
{
    string name = "Rama";   // string as an object
    string message = "Good Morning";

    cout << "Length of the name => " << name.length() << endl;

    cout << "Capacity of the message => " << name.capacity() << endl;

    cout << "Before swap => name = " << name << ", message = " << message << endl;
    message.swap(name);

    cout << "After swap => name = " << name << ", message = " << message << endl;

    return 0;
}
Output
string handling program in c++