Strings in C++

Introduction:

In this article, we are going to learn about the string data type in C++ libraries. the C++ programming language has support for string handling, implemented in its standard library.

String:

 A string is a variable that stores a sequence of letters or other characters. A string variable is surrounded by double quotes. String class is part of the C++ library that supports functionality over C-style strings. Strings are most commonly used in the programs where we need to work with texts. In C++ the operators like memory management, allocation, and null termination are handled by string class which is easy to use.

What are the two ways to store the strings?

C style string (using characters)

String class

C STYLE STRINGS

There is a little difference between the character array and the string array. In the string array, its terminates with the extra character ‘\0’.The size of the character array is always one more than the number of characters in the actual string. The C++ compiler automatically sets ‘\0’ at the end of the string array.

Initializing a string in  C++


char str[] = {'P', 'r', 'o', 'g', 'r', 'a', 'm' '\0'};
char str[8] = {'P', 'r', 'o', 'g', 'r', 'a', 'm', '\0'};
char str[] = "Program";
 char str[8] = "Program";

Example Program using C style

// Strings using C style in C++

#include <iostream>
using namespace std;

int main()
{

	// Declaring and initializing the string
	char str[] = "Program";
        char str[8]="Program";

	// let's Print string
	cout << str;

	return 0;
}

Output:

See also  COMPILER VS INTERPRETER.

Program

Concatenation of Strings:

Adding two strings together using the ‘+’ operator is known as concatenation. Concatenation occurs only at run time.

There are three ways to concatenate the string.

strcat()

append()

using the ‘+’ operator.

1.Using strcat():

The strcat() function takes two character arrays as the input. It concatenates the second array to the end of the first array.

#include <iostream>
#include <cstring>
using namespace std;

int main() {
  char str1[] = "Easy";
  char str2[] = "Peasy.";

  strcat(str1, str2);

  cout << str1 << endl;

  return 0;
}

OUTPUT

Easy Peasy.

2.Using append() :

The append() function appends the first string to the second string. The variables of type string are used with this function.

#include <iostream>
using namespace std;

int main() {
  string str1 = "Easy";
  string str2 = "Peasy.";

  str1.append(str2);

  cout << str1 << endl;

  return 0;
}

output

Easy Peasy.

3. Using the ‘+’ operator:

This is the easiest way to concatenate strings. The + operator takes two (or more) strings and returns the concatenated string.

#include <iostream>
using namespace std;

int main() {
  string str1 = "Easy";
  string str2 = "Peasy.";

  str1 = str1 + str2;

  cout << str1 << endl;

  return 0;
}

Output:


Easy Peasy.

String class:

The string class has an excess number of built-in functions. It is used for storing text. In a string class, the memory is allocated dynamically. In the string, variables are the objects of this class. The string class defines several functionalities that allow manifold operations on strings. strings are represented as objects, and no array decay occurs. 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.

See also  Expressions in C++

Example program for string class in C++

#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: Andhra Pradesh

OUTPUT

You have entered: Andhra

The user entered “Andhra Pradesh” in the input. As ” ” is the terminating character, anything written after ” ” was discarded. Hence, we got “Andhra” as the output.

That’s it for this article, In the next article we will learn about the string class functions in c++

In this article, we learned about how to store the strings in two ways, concatenation of strings and briefly about the string class.

In the next article, we will learn about the string class functions with the example programs.

Leave a Comment

Your email address will not be published.

Ads Blocker Image Powered by Code Help Pro
Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Refresh