Introduction to String in C++ Programming Language

 Introduction to String in C++





Overview

A C++ string is an object that represents a sequence of characters. However, in C, strings are represented by arrays of characters. C++ also supports C-style strings. String objects are more commonly used in C++ than character arrays. This is because String objects support a wide range of built-in functions.


You Learn about following Concepts
  • Describes the different ways strings are defined in C++ Programming
  • Please understand how to get string input from the user. It also describes various functions used with strings.

What is a C++ string?

A C++ string is an object type that represents a collection (or sequence) of individual characters. Strings in C++ are part of the standard String class (std::string). The String class stores the characters of a string as a collection of bytes in contiguous locations. Strings are most commonly used in programs that need to manipulate text. In C++ you can perform various operations on strings. For example, pass arguments to functions.

Syntax

The syntax for creating strings in C++ is very simple.

Use the string keyword to create strings in C++. However, before using this keyword, you must also include the standard String class in your program.


string string-Name  =  " This is a C++ string. " ;


In addition to the above methods, C++ strings can be constructed in a number of ways. Another way to define strings is described in the next section. Before that, let's take a quick introduction to C-style strings.


C-Style Strings

In the C programming language, a string is defined as an array of characters terminated by a zero or a terminator (\0). Termination is important because it tells the compiler where to end the string. C++ also supports C-style strings.

Syntax

You can create C-style strings that look like integer arrays. Here is its syntax.



char string-Array [ 6 ] = { ' W ' ,  ' o ' ,  ' r ' ,  ' l ' , ' d ' ,  ' s ' , ' \o  '} ;


null The length of the array is one greater than the length of the string, because the characters also need to be inserted into the array. Alternatively, the above string could be defined as:


char Array[ ] = " Hello World " ;


Array str_array still contains 13 characters because C++ automatically appends a null character to the end of the string. increase. If I check the size of the above array using the sizeof( ) function, it also returns 13.






Different Ways to Define Strings

Now that you have a basic understanding of strings in C++, let's look at different ways to define strings in C++ Programming Language.

1. The best and most convenient way to define a string is with the string keyword. We recommend using the string keyword.

String string-Name = " Hello "
// or
string string-Name( " Hello " ) ;


2. Then we have a C-style string. Here are different ways to create a C-style string.
char string-Name[ 6 ] = { ' h ' , ' e ' , ​​' l ' , ' l ' , ' o ' , ' \0 ' } ;
// or
char strName[ ] = { ' h ', ' e ' , ​​' l ' , ' l ' , ' o ' , ' \0 ' } ;

like

char string-Name[ ] = " hello " ;
// or
char string-Name[ 6 ] = " Hello " ;


You don't have to use all the space allocated for the character array ( string ):

char string-Name[ 50 ] = " hello " ;



String Input

To provide user input to your program, you typically use the cin keyword and the extraction operator ( >> ). By default, the extract operator considers whitespace ( spaces, tabs, newlines, etc.) to be terminators. Suppose the user enters "Pakistan" as input. In this case, only "New" is considered input and "Lahore" is discarded.

Here is an example that should clarify the concept.



Code in C++

#include<iostream>
using namespace std ;
int main ( )
   char country [ 50 ]  ;
   cout << "Please enter a country name:" << endl ;
   // user input is stored in city variable
   cin >> country ;
   cout << " you typed: " << country ;
   return 0 ; 
}


Output

Please enter a country name: Pakistan
you typed: Pakistan








I hope You can easily Understand and Learn this Article

Best of Luck 😇





Post a Comment

0 Comments