0% found this document useful (0 votes)
136 views

STRING

The document discusses strings in C++. It defines strings as sequences of characters that can be represented using the string class or character arrays in C++. It then covers different ways to define strings in C++, such as using the string keyword or character arrays. It also discusses how to take string input from users, explaining the limitations of extraction operator and how to overcome it by using get() or getline() functions.

Uploaded by

HARSH KHATRI
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
136 views

STRING

The document discusses strings in C++. It defines strings as sequences of characters that can be represented using the string class or character arrays in C++. It then covers different ways to define strings in C++, such as using the string keyword or character arrays. It also discusses how to take string input from users, explaining the limitations of extraction operator and how to overcome it by using get() or getline() functions.

Uploaded by

HARSH KHATRI
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Overview

A string in C++ is an object that represents a sequence of characters. On the other hand, a string
in C is represented by an array of characters. C++ supports the C-style strings as well. The string
objects in C++ are more frequently used compared to the character arrays because the string
objects support a wide range of built-in functions.

Scope of Article
 We will cover the different ways of defining strings in C++.
 We will understand how to take string inputs from the users.
 We will also cover the different functions which can be used on strings.

What is a String in C++?


A string in C++ is a type of object that represents a collection (or sequence) of different
characters. Strings in C++ are a part of the standard string class (std::string). The string
class stores the characters of a string as a collection of bytes in contiguous memory locations.
Strings are most commonly used in the programs where we need to work with texts. We can
perform various operations on the strings in C++. For example, reversing, concatenating, or
passing as an argument to a function.

Syntax

The syntax to create a string in C++ is quite simple. We use the string keyword to create a string
in C++. However, we also need to include the standard string class in our program before using
this keyword.

string str_name = "This is a C++ string";

Besides the method described above, C++ strings can be created in many different ways. The
alternative ways of defining a string are explained in the upcoming sections. Before that, let us
have a brief introduction to the C Style Character Strings.

The C-Style Character String


In the C programming language, a string is defined as an array of characters that ends with a null
or termination character (\0). The termination is important because it tells the compiler where the
string ends. C++ also supports the C-style strings.

Syntax

We can create the C style strings just like we create an integer array. Here is its syntax.
char str_array[7] = {'S', 'c', 'a', 'l', 'e', 'r', '\0'};

Because we also have to add a null character in the array, the array's length is one greater than
the length of the string. Alternatively, we can define the above string like this as well:

char str_array[] = "Scaler";

The array str_array will still hold 7 characters because C++ automatically adds a null character
at the end of the string. If we use the sizeof() function to check the size of the array above, it will
return 7 as well.

Different Ways of Defining a String


Now that we have a basic understanding of strings in C++, let us go through the different ways
of defining a string in C++.

 Using the string keyword is the best and the most convenient way of defining a string.
We should prefer using the string keyword because it is easy to write and understand.
 string str_name = "hello";
 // or
 string str_name("hello");
 Then we have the C style strings. Here are the different ways to create C style strings:
 char str_name[6] = {'h', 'e', 'l', 'l', 'o', '\0'};
 // or
 char str_name[] = {'h', 'e', 'l', 'l', 'o', '\0'};

Alternatively, we have:

char str_name[] = "hello";


// or
char str_name[6] = "hello";
It is not necessary to utilize all the space allocated to a character array (string):

char str_name[50] = "hello";

Taking String Input


To provide our program(s) input from the user, we generally use the cin keyword along with the
extraction operator (>>). By default, the extraction operator considers white space (such as
space, tab, or newline) as the terminating character. So, suppose the user enters "New Delhi" as
the input. In that case, only "New" will be considered as input, and "Delhi" will be discarded.

Let us take a simple example to understand this:

#include <iostream>
using namespace std;

int main() {
char str[30];

cout << "Enter your city name: ";

cin >> str;


// The user input will be stored in the str variable

cout << "You have entered: " << str;

return 0;
}

Input:

Enter your city name: New Delhi

Output:

You have entered: New

In the above example, the user entered "New Delhi" in the input. As " " is the terminating
character, anything written after " " was discarded. Hence, we got "New" as the output.

In order to counter this limitation of the extraction operator, we can specify the maximum
number of characters to read from the user's input using the cin.get() function.

Let us take an example to understand this:

#include <iostream>
using namespace std;

int main() {
char str[50];

cout << "Enter your city name: ";


cin.get(str, 50);

cout << "You have entered: " << str << endl;

return 0;
}

Input:

Enter your city name: New Delhi

Output:

You have entered: New Delhi

Now, we were able to get the whole string entered by the user.

Apart from this method, we can also use the getline() function. The getline() function is
explained below in this article.

You might also like