C++ Strings with Examples

In this tutorial, you will learn how to manage strings in C++. You’ll discover how to declare, initialize, and employ them in a variety of input/output operations.

A string is a group of characters. There are two main types of strings used in C++ programs:

  • Strings that are objects of the string class
  • C-strings (C-style Strings)

C-strings | Character String

The collection of characters is kept in array form in C programming. Programming in C++ also supports this. Hence, it is known as C-strings.
C-strings are arrays of type char that end with the character “0,” or the null character (ASCII value of null character is 0).

The declaration and initialization that follow produce a string containing the word “Hello”. The character array holding the string must be one character larger than the word “Hello” in order to accommodate the null character at the end of the array.

char arr[6] = {'H', 'e', 'l', 'l', 'o', '\0'};

If you write the above statement using the array initialization rule, you can do so as follows:

char arr[] = "Hello";
#include <iostream>
//geektocode.com
using namespace std;

int main () {

   char arr[6] = {'H', 'e', 'l', 'l', 'o', '\0'};

   cout << "This message: ";
   cout << arr << endl;

   return 0;
}

Output:

This message: Hello

Numerous functions that deal with null-terminated strings are available in C++ –

Sr.NoFunction & Purpose
1strcpy(s1, s2);
strings s2 and s1 are copied.
2strcat(s1, s2);
String s1 is joined to string s2 at the end.
3strlen(s1);
gives the string s1’s length in the back.
4strcmp(s1, s2);
if s1 and s2 are equal, the result is 0. If s1 is greater than s2, the result is greater than 0.
5strchr(s1, ch);
a pointer to the first instance of character ch in string s1 is returned.
6strstr(s1, s2);
a pointer to the first instance of string s2 in string s1 is returned.
#include <iostream>
#include <cstring>

using namespace std;
//geektocode.com
int main () {

   char str1[10] = "Hello";
   char str2[15] = "Geektocode";
   char str3[20];
   int  len ;

   // copy str1 into str3
   strcpy( str3, str1);
   cout << "strcpy( str3, str1) : " << str3 << endl;

   // concatenates str1 and str2
   strcat( str1, str2);
   cout << "strcat( str1, str2): " << str1 << endl;

   // total lenghth of str1 after concatenation
   len = strlen(str1);
   cout << "strlen(str1) : " << len << endl;

   return 0;
}

Output:

strcpy( str3, str1) : Hello
strcat( str1, str2): HelloGeektoCode
strlen(str1) : 15

The C++ String Class

A string class type that supports all the aforementioned operations as well as a tone more functionality is offered by the default C++ library. Let’s examine the subsequent example.

#include <iostream>
#include <string>

using namespace std;
//geektocode.com
int main () {

   string str1 = "Hello";
   string str2 = "GeektoCode";
   string str3;
   int  len ;

   // copy str1 into str3
   str3 = str1;
   cout << "str3 : " << str3 << endl;

   // concatenates str1 and str2
   str3 = str1 + str2;
   cout << "str1 + str2 : " << str3 << endl;

   // total length of str3 after concatenation
   len = str3.size();
   cout << "str3.size() :  " << len << endl;

   return 0;
}

Output:

str3 : Hello
str1 + str2 : HelloGeektoCode
str3.size() :  15

Passing a string to a function

Similar to how arrays are passed to a function, strings are also passed to functions.

#include <iostream>
//geektocode.com
using namespace std;

void display(char *);
void display(string);

int main()
{
    string str1;
    char str[100];

    cout << "Enter a string: ";
    getline(cin, str1);

    cout << "Enter another string: ";
    cin.get(str, 100, '\n');

    display(str1);
    display(str);
    return 0;
}

void display(char s[])
{
    cout << "Entered char array is: " << s << endl;
}

void display(string s)
{
    cout << "Entered string is: " << s << endl;
}

Output:

Enter a string:  Programming By Geektocode.
Enter another string:  Hey I am
Entered string is: Programming By Geektocode.
Entered char array is: Hey I am

The parameter is the only distinction between the two functions. The first display() function accepts a parameter of the type char array, while the second accepts a parameter of type string.

The practice of overloading functions is known as that. More information on Function Overloading

Leave a Reply

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