In this tutorial, you’ll learn how to work with strings in C++. You’ll learn how to create strings, assign them values, and use them for input/output operations.
C++ Strings
Strings are a sequence of characters surrounded by quotation marks that are used to handle non-numeric data, such as names and addresses.
For example,
string myString = "Hello World"; In C++ programming language, there are two types of strings typically used:
- C-Style Strings (character arrays)
- String Class
C-Strings
C++ programming supports the use of strings stored in character arrays, which are also known as C-strings.
This type of string is created using an array of characters and is denoted by a char data type. It is a null-terminated string, which is terminated with a null character \0.
Defining a C String
The general syntax for defining C Strings is given below:
data_type string_name [string_size]; In the above general syntax, data_type is the char data type, and string_name is the name of the string. The string’s size (the number of characters) is specified in square brackets [].
For example, in the following statement, a string called myString is created with a size of 12.
char myString = [12]; The header file <string> must be included in order to handle strings, while in certain implementations, it may also be automatically included when the header file <iostream> is included.
Initializing a C String
As an array of characters, strings in C can be initialized the same way arrays are initialized.
For example,
char myString [12] = “Hello World!”; In the above example, a character array of size 12 is declared and initialized with the string "Hello World!". This can be read as an array of 12 characters, with the first 11 characters being ‘H’, ‘e’, ‘l’, ‘l’, ‘o’, ‘ ‘, ‘W’, ‘o’, ‘r’, ‘l’, ‘d’, and the last character being the null terminator \0. This null terminator is used to signify the end of the string.
Example 1: Displaying a String Entered By The User
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
// Declaring two strings
char ask_name [] = "Hey Stranger! Please Enter Your Name = ";
char greet_now [] = "Hello, ";
//Declaring an array to store the user's name
char your_name [100];
//Asking the user to enter his/her name
cout << ask_name;
cin >> your_name;
//line break
cout<<endl;
//printing out the greeting message
cout<< greet_now << your_name << "!";
getch();
return 0;
} OUTPUT OF THE PROGRAM
Case 1:
Hey Stranger! Please Enter Your Name = King
Hello, King!
1. The above program example starts by declaring two strings, ask_name and greet_now. The first string is the question that will be used to ask the user for their name, and the second string is a greeting message.
2. Then, an array called your_name is declared to store the user’s name.
3. The program then displays the ask_name string, prompting the user to enter their name.
4. The user then types in their name, which is stored in the your_name array.
5. Finally, the program prints out the greet_now string, followed by the user’s name.
OUTPUT OF THE PROGRAM
Case 2:
Hey Stranger! Please Enter Your Name = King Khan
Hello, King!
In the second output case, as you can see, when the user enters his full name, King Khan, then only the King is printed, and the other part that is Khan, is not displayed on the screen. But why?
This happens because the program is reading the user’s input character by character and stops once it encounters a space character. To read the entire string, we can use the getline() function instead of cin. getline() function will read the characters until it finds a new line character, which is represented by \n.
This is the modified code :
#include <iostream>
#include <conio.h>
#include <string>
using namespace std;
int main()
{
// Declaring two strings
char ask_name [] = "Hey Stranger! Please Enter Your Name = ";
char greet_now [] = "Hello, ";
//Declaring an array to store the user's name
string your_name;
//Asking the user to enter his/her name
cout << ask_name;
getline(cin, your_name);
//line break
cout<<endl;
//printing out the greeting message
cout<< greet_now << your_name << "!";
getch();
return 0;
} OUTPUT OF THE PROGRAM
Hey Stranger! Please Enter Your Name = King Khan
Hello, King Khan!
In the modified C++ program, we have included the string header file <string>.
We have declared a string variable instead of a character array to store the user’s name.
We have used the getline() function instead of cin to read the user’s input.
The getline() function reads the characters until it finds a new line character \n.
Hence, the greeting message is printed with the complete user’s name.
String Class
Strings in C have several limitations. The class <string> in C++ solves several of these problems and helps overcome them.
This class offers common string operations, including comparison, concatenation, find and replace, and a method for obtaining substrings.
Unlike char arrays with a fixed length, string objects can be extended as needed.
The general syntax for defining a string in C++ is given below:
type string_name; For example,
string str1; Example 2: C++ Program To Get a String From The User
#include <iostream>
#include <string> // include string library
using namespace std;
int main()
{
//declaring and initializing the string object
string str;
// Take input from the user
cout << "Enter a String: ";
getline(cin, str);
//Printing the string object
cout << "You Entered: " << str << endl;
return 0;
} OUTPUT OF THE PROGRAM
Enter a String: I love C++ Programming!
You Entered: I love C++ Programming!
This program takes an input string from the user and displays it on the screen.
The program begins by declaring and initializing a string object called str. The program then prompts the user to enter a string and uses the getline() function to read the user’s input and store it in the str object. Finally, the program prints out the value stored in the str object.
Passing String to a Function
The passing of strings to functions is similar to passing an array to a function. It allows us to pass a string of characters to a function as a parameter. This allows us to pass the same string of characters to multiple functions so they can all operate on the same string.
Example 3: C++ For Passing a String to a Function
#include <iostream>
#include <string>
using namespace std;
// Function declaration
void myFunction(string);
int main()
{
string name;
cout << "Please Enter Your Name: ";
getline(cin, name);
// Call the function
myFunction(name);
return 0;
}
// Function definition
void myFunction(string x)
{
cout << "Hello, " << x << endl;
} OUTPUT OF THE PROGRAM
Please Enter a String: Ali
Hello, Ali The program begins by declaring a function called myFunction that takes a string as an argument.
In the main function, a string variable called name is declared, and it asks the user to enter their name.
The user’s input is stored in the name variable and is then passed to the myFunction function as an argument.
The myFunction function then prints out a string with the user’s name.