Strings in C++ programming

By  Falak Hasija    11 - 23 July, 20
strings in c++ programming

Share article on social media


In C++ programming, C++ string is a sequence of characters implemented as a length-controlled string object.  This means that a C++ string is an instantiation of the string class.

Fixed Length Strings:

A fixed-length string is implemented as an array of characters. When implementing a fixed-length string format, we must first decide what size to make the variable.

Variable-Length Strings:

It's a structure that can expand and contract to accommodate the data.

Two common techniques are to use length-controlled strings and delimited strings:

  1. Length-controlled strings: They add a count that specifies the number of characters in the string. Generally, the count is a single byte, which provides for strings of up to 255 characters. 
  2. Delimited Strings: This technique is used to identify the end of the string using a delimiter. Commas, semicolons, colons, and dashes are some common delimiters. This technique is however used in C language.  

 

 

DECLARATION OF STRINGS:

Declaring a string is as follows in C++:

string string_name; 

           Here string_name is the name given to a string variable. Keep in mind to include a string library in your program before declaring a string. (#include). Also, don't forget to add a terminator i.e. semicolon(;) after declaring a string.

INITIALISATION OF STRINGS:

A string can be initialized as follows:

string string_name="Welcome to NoobtoDev.com";

string string_name ("Welcome to NoobtoDev.com");

 

A C++ Program to declare and initialize string:

#include

#include

using namespace std;

int main()

{  

     string str="NoobtoDev";   //Declaring and initialising a string

     cout<

      return 0;

}

Output: NoobtoDev

 

THE STRING CLASS:

The C++ standard defines the string type. An object instantiated of this type is a length-controlled string.

STRING CONSTRUCTORS:

As a class, the string has a rich set of constructors.

CONSTRUCTOR FORMAT OPERATION
string s1; Default Constructor(empty string)
string s2("Hello World");

Initialization constructor using C string

string s3 (n, 'c'); Initialization constructor using n identical characters
string s4 (s2); Copy constructor
string s5 (s2, num); Copy constructor that copies num characters from the beginning of the string.
string s6(s2,start,num); Copy constructor that copies num characters from index location start in the string s2
string s7("Hello", num); same as s5, but with C string 
string s8("Hello", start, num); same as s6, but with C string

 

A C++ program to demonstrate string constructors:

 

#include

#include

using namespace std;

int main()

 {

      string s1;

      string s2("Hello World");

      string s3(s2);

      string s4(5, 'A');

     string s5(s2, 6);

      string s6("Hello", 2);

      string s7("Hello", 3, 25 );

      cout<<"s1: "<

      cout<<"s2: "<

     cout<<"s3: "<

      cout<<"s4: "<

      cout<<"s5: "<

      cout<<"s6: "<

      cout<<"s7: "<

     return 0;

}

 

Output:

s1: 

s2:Hello World

s3:Hello World

s4:AAAAA

s5:World

s6:He

s7:lo     

 

C++ STRING INPUT & OUTPUT:

  1. String output (<<):    cout<String output is provided by the overloaded insertion operator(<<), which is overloaded in the string class.
  2. String input (>>):        cin>>str; or fsIn>>str;  String input is done by using the EXTRACTION OPERATOR (>>), which is overloaded for string objects. It skips any leading whitespace. Then it extracts all contiguous non-whitespace characters. When it finds any       whitespace character it stops. The terminating whitespace character is left in the input stream.
  3. Getline Function [getline(fsIn, inStr, ';');] : In order to read a string with spaces, we must use getline.  A getline function extracts text from an input stream and makes a string out of it. Getline function has 2 or 3 parameters. The first parameter specifies the input stream. The second parameter specifies the string object where the data are to be stored. The third parameter specifies the terminating character, which defaults to the newline character if none is specified.  

 

C++ program to demonstrate getline operation:

#include

#include

using namespace std;

int main()

{

    cout<<"Enter a name in the form : \n";

    string lastName;

     getline(cin, lastName, ',' );

     string firstName;

     getline(cin, firstName,);

     cout<<"Here is your name:\n\t|"<

     return 0;

}

Output: Enter a name in the form : 

Watson, Emma

Here is your name:

           |Emma Watson|

 

This article is enough to get your confusion clear regardings strings in C++ programming. Read about String manipulation in C++ programming .