String in c++

Strings In C++ | C++ Strings Example

This tutorial will teach you how to work with strings in C++. You’ll learn how to declare, initialize, and use them for a variety of input/output activities.

A string is a group of characters. In the C++ programming language, there are two types of strings:

  • Strings that are objects of the String class (the string class in the Standard C++ Library)
  • C-strings (C-style Strings)

Strings are objects that represent character sequences.

  • A string is an object that represents a collection (or series) of various characters. Strings are members of the standard string class in C++ (std::string).
  • String characters are stored as a collection of bytes in contiguous memory locations by the string class. Strings are most typically employed in programs that require us to work with text.

In C++, We may execute a variety of operations on strings. For example, reversing, concatenating, or passing a function argument.

The standard string class supports such objects with an interface similar to that of a regular container of bytes, but with additional features developed expressly for use with strings of single-byte characters.

  • To use string functions in C++, we must include a library named in our code at the top that provides string functions.
  • It must be included in the #include header file. As we know, the string object knows numerous characteristics and has several operations that we may do on it.
  • The string class is an instantiation of the basic string class template, which utilizes char (i.e., bytes) as its character type and its default char traits and allocator types (for more information on the template, see basic string).

Note: It is important to note that this class handles bytes regardless of the encoding: If used to handle sequences of multi-byte or variable-length characters (such as UTF-8), all members of this class (such as length or size), as well as its iterators, will still operate in terms of bytes (not actual encoded characters).

C-strings

Arrays are used to store character collections in C programming. C++ programming also supports this. As a result, it’s called C-strings.

C-strings are arrays of type char that end with a null character, i.e., 0. (ASCII value of null character is 0).

How to define a C – string?

char str[] = “C++”;

The string str in the preceding code holds four characters.

Despite the fact that “C++” comprises three characters, the null character 0 is automatically appended to the end of the string.

Alternative ways of defining a string

char str[4] = “C++”;
char str[] = {‘C’,’+’,’+’,’\0′};
char str[4] = {‘C’,’+’,’+’,’\0′};

It is not required to use all of the string’s allocated space, as it is with arrays. As an example:

char str[100] = “C++”;

Example 1: C++ Strings to read a word

C++ program to display a string entered by the user.

#include <iostream>
using namespace std;

int main()
{
    char str[100];

    cout << "Enter a string: ";
    cin >> str;
    cout << "You entered: " << str << endl;

    cout << "\nEnter another string: ";
    cin >> str;
    cout << "You entered: "<<str<<endl;

    return 0;
}

Output

Enter a string: C++
You entered: C++
Enter another string: Programming is fun.
You entered: Programming

In the second example, only “Programming” is printed rather than “Programming is pleasant.”

This is due to the fact that the extraction operator >> operates similarly to scanf() in C and regards a space ” ” to be a terminating character.

Example 2: C++ Strings to read a line of text

C++ program to read and display an entire line entered by the user using strings.

#include <iostream>
using namespace std;

int main()
{
    char str[100];
    cout << "Enter a string: ";
    cin.get(str, 100);

    cout << "You entered: " << str << endl;
    return 0;
}

Output

Enter a string: Programming is fun.
You entered: Programming is fun.

The cin. get function can be used to read text that contains blank spaces. This function accepts two parameters.

The first argument is the string’s name (the location of the first element of the string), and the second argument is the maximum size of the array.

In the preceding program, str is the string’s name, and 100 is the array’s maximum size.

strings Object

You may also create a string object in C++ to hold strings.

Unlike char arrays, string objects have no set length and can be extended as needed.

Example 3: C++ string using strings data type

#include <iostream>
using namespace std;

int main()
{
    // Declaring a string object
    string str;
    cout << "Enter a string: ";
    getline(cin, str);

    cout << "You entered: " << str << endl;
    return 0;
}

Output

Enter a string: Programming is fun.
You entered: Programming is fun.

String str is declared in this program. The string is then requested from the user.

You can get the entered line of text using getline instead of cin>> or cin. get() ().

The first parameter of the getline() function is cin, and the second parameter is str, which represents the position of the line to be stored.

Passing String to a Function

Strings are supplied to functions in the same way as arrays are passed to functions.

#include <iostream>

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 is fun.
Enter another string: Really?
Entered string is: Programming is fun.
Entered char array is: Really?

Two strings are requested in the preceding application. These are kept in str and str1, where str is a char array and str1 is a string object, respectively.

Then there is two display() calls that output the string onto the string.

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

This is referred to as function overloading. More information on Function Overloading can be found here.

You may Like:

C++ Programming Default Arguments (Parameters) with Example

Leave a Reply