C++ Strings with Explanation & Example

String is a collection of characters. There are two types of C++ Strings commonly used in C++ programming language:

  1. C-strings (C-style Strings)
  2. Strings that are objects of string class (The Standard C++ Library string class)
The C-strings (C-style Strings) Character String

C-Style strings were introduced with C programming language, and since C++ supports all features of C language this type can be used in C++ programming as well. This C-style string is actually a one dimensional array of characters which is terminated by a null character ‘\0’

c++ c-style strings

C-Style strings Diagram

 

Declaration and Initialization of C-Style strings

Following are the different ways to creating a C-style strings.

char str[] = "C++";

Although, “C++” has 3 character, the null character \0 is added to the end of the string automatically.

Alternative ways of defining a string
char str[4] = "C++"; 
char str[] = {'C','+','+','\0'};
char str[4] = {'C','+','+','\0'};
Example-1 C-style Strings Program

C++ program to display a string entered by user.

#include <iostream>
using namespace std;

int main()
{
    char str[100];

    cout << "Enter a string: ";
    cin >> str;
    cout << "You entered: " << str << endl;

    cout << "\nEnter another string: ";
    cin >> str;
    cout << "You entered: "<<str<<endl;

    return 0;
}
Output

Enter a string: C++
You entered: C++

Enter another string: Programming is fun.
You entered: Programming

Notice that, in the second example only “Programming” is displayed instead of “Programming is fun”.
This is because the extraction operator >> works as scanf() in C and considers a space ” “ has a terminating character.

Example-2 Program to read full line of text

Run Online

#include <iostream>
using namespace std;

int main()
{
    char str[100];
    cout << "Enter a string: ";
    cin.get(str, 100);

    cout << "You entered: " << str << endl;
    return 0;
}
Output

Enter a string: Programming is fun.
You entered: Programming is fun.

You entered: James

To read the text containing blank space, cin.get function can be used. This function takes two arguments.
First argument is the name of the string (address of first element of string) and second argument is the maximum size of the array.
In the above program, str is the name of the string and 100 is the maximum size of the array.

C-Style Strings Inbuilt Functions

C++ provides many functions to manipulate C-style strings as part of the <cstring> library. Here are a few of the most useful:

Function NameFunction Description &  Usage
strcpy(s1, s2);Copies string s2 into string s1.
strcat(s1, s2);Concatenates string s2 onto the end of string s1.
strlen(s1);;Returns the length of string s1.
strcmp(s1, s2);Returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater than 0 if s1>s2.
strchr(s1, ch);Returns a pointer to the first occurrence of character ch in string s1.
strstr(s1, s2);Returns a pointer to the first occurrence of string s2 in string s1.
The String Class in C++ (Object type strings)

The standard C++ library provides a string class type that supports all the operations mentioned above, additionally much more functionality. Unlike using char arrays, string objects has no fixed length, and can be extended as per your requirement.

Example-1 C++ Strings Program

Run Online

include <iostream>
using namespace std;

int main()
{
    // Declaring a string object
    string str;
    cout << "Enter a string: ";
    getline(cin, str);
    // you can also take input as follows:
    // cin>>str;
    cout << "You entered: " << str << endl;
    return 0;
}
Output

Enter a string: C++ Strings and its types.
You entered: C++ Strings and its types.

You entered: James

Leave a Reply

Your email address will not be published. Required fields are marked *