What is a String?

A string is a sequence of characters. A C++ string is an object of the std::string class. The characters are stored sequences of bytes with access to a single character byte allowed.

C++ strings allocate memory dynamically. More memory can be allocated to the string during run time if needed. Since there is no memory pre-allocation, no wastage of memory. We can perform various operations on strings, including comparisons, concatenation, conversion, etc.

In this C++ tutorial, you will learn:

Declaring Strings

C++ supports two types of string declarations:

  • C-style character string
  • String class type

C-Style Character String

This type of string declaration was introduced in C programming language. C++ continues to support it. It's simply a one-dimensional array of characters terminated with a null character (\0). A null-terminated string has characters that make up the string then followed by a null.

Consider the string declaration given below:

char name[5] = {'J', 'o', 'h', 'n', '\0'};

The above declaration creates a string that forms the word John. The word has 4 characters, but the string has a size of 5. The extra space allows for holding of the null character.

Using the array initialization rule, we can write the above statement as follows:

char name[] = "John";

Note that you don't have to place the null character at the end of the string constant. The C++ compiler will automatically place the '\0' at the string's end when initializing the array.

std::string

The standard C++ library provides the string class which supports various string operations. It is written as std::string.

To use this class, we must first include it into our workspace using the #include preprocessor as shown below:

#include<string>

Next, we can declare our string using the string keyword. For example:

string name = "John";

The above statement will create a string named name to hold the value John.

Accessing string Values

In C++, we can access the string values using the string name. For example:

#include <iostream>
using namespace std;
int main() {
	char name[5] = { 'J', 'o', 'h', 'n', '\0' };
	cout << "String value is: ";
	cout << name << endl;
	return 0;
}

Output:

Here is a screenshot of the code:

Code Explanation:

  1. Including the iostream header file in our code. It will allow us to read from and write to the console.
  2. Including the std namespace so as to use its classes and functions without calling it.
  3. Calling the main() function inside which the logic of the program should be added. The { marks start of body of the main() function.
  4. Declaring a string of characters and giving it the name name. The string will store the value John. The extra space will store the null character.
  5. Printing some text on the console.
  6. Printing the value of the string named name on the console.
  7. The main() function should return an value if the program runs fine.
  8. End of the body of the main() function.

Here is another example using the C++ standard string class:

#include <iostream>
#include <string>
using namespace std;
int main() {

	string name = "Guru99";
	
	cout << "The name is : " << name << endl;

	return 0;
}

Output:

Here is a screenshot of the code:

Code Explanation:

  1. Including the iostream header file in our code. It will allow us to read from and write to the console.
  2. Including the standard string class in our code.
  3. Including the std namespace so as to use its classes and functions without calling it.
  4. Calling the main() function inside which the logic of the program should be added. The { marks start of body of the main() function.
  5. Declaring a string and giving it the name name. The string will store the value Guru99.
  6. Printing the value of the string name alongside some text on the console.
  7. The main() function should return an value if the program runs fine.
  8. End of the body of the main() function.

String Functions:

You will often want to manipulate strings. C++ provides a wide range of functions that you can use for this. These functions are defined in the CString class, hence, we have to include it in our code in order to use the functions. Let us discuss some:

strcpy()

This is the string copy function. It copies one string into another string.

Syntax:

strcpy(string1, string2);

The two parameters to the function, string1 and string2, are strings. The function will copy the string string1 into the string 1.

strcat()

This is the string concatenate function. It concatenates strings.

Syntax:

strcat(string1, string2);

The two parameters to the function, string1 and string2 are the strings to be concatenated. The above function will concatenate the string string2 to the end of the string string1.

strlen()

This is the string length function. It returns the length of the string passed to it as the argument.

Syntax:

strnlen(string1)

The parameter string1 is the name of the string whose length is to be determined. The above function will return the length of the string string1.

strcmp()

This is the string compare function. It is used for string comparison.

Syntax:

strcmp(string1, string2);

The above function will return 0 if strings string1 and string2 are similar, less than 0 if string1<string2 and greater than 0 if string1>string2.

Example:

The following example demonstrates how to use the above string functions:

#include <iostream>
#include <cstring>
using namespace std;
int main() {

	char name1[10] = "Guru99";
	char name2[10] = "John";
	char name3[10];

	int  len;
	strcpy(name3, name1);
	cout << "strcpy( name3, name1) : " << name3 << endl;

	strcat(name1, name2);
	cout << "strcat( name1, name2): " << name1 << endl;

	len = strlen(name1);
	cout << "strlen(name1) : " << len << endl;
	return 0;
}

Output:

Here is a screenshot of the code:

Code Explanation:

  1. Including the iostream header file in our code. It will allow us to read from and write to the console.
  2. Including the standard CString class in our code.
  3. Including the std namespace so as to use its classes and functions without calling it.
  4. Calling the main() function inside which the logic of the program should be added. The { marks start of body of the main() function.
  5. Declaring a string of 10 characters and giving it the name name1. The string will store the value Guru99.
  6. Declaring a string of 10 characters and giving it the name name2. The string will store the value John.
  7. Declaring a string of 10 characters and giving it the name name3.
  8. Declaring an integer variable named len.
  9. Copying the string name1 into the string name3.
  10. Printing the value of the string name1 alongside some text on the console. It should print Guru99.
  11. Concatenating the strings name2 to the end of string name1. The value of name1 is now Guru99John.
  12. Printing the value of the string name1 alongside some text on the console. It should print Guru99John
  13. Determining the length of the string named name1 and assigning the value of length to variable len.
  14. Printing the value of len variable alongside some other text on the console.
  15. The main() function should return an value if the program runs fine.
  16. End of the body of the main() function.

Summary

  • A string is a sequence of characters.
  • Strings belong to the standard string class in C++.
  • We can declare strings using the C-style character string or standard string class.
  • The strcpy() function copies one string into another.
  • The strcat() function concatenates two functions.
  • The strlen() function returns the length of a function.
  • The strcmp() function compares two strings.

 

YOU MIGHT LIKE: