Strings and String Functions in C++

Sriram Hariharan  Print 
24 Oct 2018
 
Intermediate
156

String is an object that represents a group or a sequence of characters. String is represented as a one-dimensional array of characters and ends with a \0 (null character). String is defined in the same way as the other data types (integer, character). The first step will be to declare a string variable and then assign a value to the variable. The string is an object that belongs to the std: string class.

 
 data_type string_name[string_count];

Here’s an example of a string declaration in C++ :

 
 char string[5];

Similar to arrays, you can also define string as shown below :

 
 char string[]=”Welcome”;

In this case, the array will hold the values “Welcome” and a null character ‘\0’ that is added to the string at the end automatically by the compiler.

W
E
L
C
O
M
E
\0

Here’s a sample program for understanding the string declaration and definition.

 #include <iostream>
 using namespace std;
 
 int main()
 {
 char string[]="Welcome";
 cout <<string;
 }
 

The output of this program will be :

String Class

C++ does not support any built-in string types. In the previous example, we used the array of characters terminated by a \0 to store the string value and perform operations on it (e.g., print the value of string). This type of string is called the C Strings. C++ offers a new string class very similar to the C Strings but with efficient programming capabilities. Internally, the working of the string class is the same as C Strings (by using character array to store the characters), while the memory definition, allocation and addition of null character at the end is all handled by the string class itself. In this section, we will take a detailed look into the different functions that are available as a part of the String Class in C++.

String Functions

The different string functions in the String class are :

  • Strcpy

  • Strcat

  • Strlen

  • Strcmp

  • Strchr

  • Strstr

Strcpy(s1, s2)

This string function copies the string s2 into string s1.

Example Program
 
 #include <iostream>
 #include <cstring>
 using namespace std;
 
 int main()
 {
 char string1[]="Good";
 char string2[]="Morning";
 strcpy(string1, string2);
 cout << "The result of strcpy is : " <<string1 << '\n';
 }

The result of this program will be that the value of string2 (Morning) will be copied to string1 (Good) and string2 value will be printed (Morning).

Strcat(s1, s2)

This string function concatenates the string s2 at the end of string s1.

Example Program
 
 
 #include <iostream>
 #include <cstring>
 using namespace std;
 
 int main()
 {
 char string1[]="Good";
 char string2[]="Morning";
 strcat(string1, string2);
 cout << "The result of strcat is : " <<string1 << '\n';
 }

The result of this program will be that the value of string2 (Morning) will be concatenated to string1 (Good) and string2 value will be printed (GoodMorning).

Strlen(s1)

This string function calculates and returns the length of the string s1.

Example Program
 
 #include <iostream>
 #include <cstring>
 using namespace std;
 
 int main()
 {
 char string1[]="Good Morning";
 cout << "The result of strlen is : " <<strlen(string1)<< '\n';
 } 

The result of this program will be length of string1 which will be 12.

Strcmp(s1, s2)

This string function compares the two string values and returns 0 if the two strings are same, -1 if the value of string1<string2, and 1 when string1>string2.

Example Program #1

 
 #include <iostream>
 #include <cstring>
 using namespace std;
 
 int main()
 {
 char string1[]="Good";
 char string2[]="Morning";
 cout << "The result of strcmp is : " <<strcmp(string1, string2) << '\n';
 }

The result of this program for the following input will be -1.

Example Program #2
 
 
 #include <iostream>
 #include <cstring>
 using namespace std;
 
 int main()
 {
 char string1[]="Morning";
 char string2[]="Good";
 cout << "The result of strcmp is : " <<strcmp(string1, string2) << '\n';
 }

The result of this program for the following input will be 1.

Example Program #3
 #include <iostream>
 #include <cstring>
 using namespace std;
 
 int main()
 {
 char string1[]="Morning";
 char string2[]="Morning";
 cout << "The result of strcmp is : " <<strcmp(string1, string2) << '\n';
 }

The result of this program for the following input will be 0.

Strchr(s1, ch)

This string function searches for the character in the string and returns the pointer to the first occurrence of the character.

Example Program
 
 #include <iostream>
 #include <cstring>
 using namespace std;
 
 int main()
 {
 char string1[]="Good Morning";
 char ch = 'a';
 if (strchr(string1,ch))
 cout << "The character "<< ch <<" is present in the string " << string1 << '\n';
 else
 cout << "The character "<< ch <<" is not present in the string " << string1 << '\n';
 } 

The output of this program will check to see if the character ‘a’ is present in the string ‘Good Morning’. The result will be printed as :

If we change the character as ‘o’, it will return the pointer value of the first occurrence of the character o in the string Good Morning.

Strstr(s1,s2)

This string function returns a pointer to the first occurrence of the string s2 in the string s1. Otherwise, the function returns a null pointer if s2 is not a part of s1.

Example Program
 #include <iostream>
 #include <cstring>
 using namespace std;
 
 int main()
 {
 char string1[]="Good Morning. How are you?";
 char string2[] = "Good Morning";
 char *ptr=strstr(string1,string2);
 if (ptr)
 cout << string2 <<" is present in the string " << string1 << '\n';
 else
 cout << string2 <<" is not present in the string " << string1 << '\n';
 }

The result of this program will be :

Summary

In this section, we have covered the following concepts :

  • Introduction to Strings

  • What is a String Class?

  • String Functions

  • Types of String Functions with examples

Hands-on Learning
+