---
# System prepended metadata

title: Untitled

---

**C++ Strings**



:::danger
## Abstract 
C++ strings is one of the most useful data types supplied in the C++ libraries. A string is a variable that stores a sequence of letters or other characters, such as "Scaler" or "He is a good boy!". In C++,string is an object of **std::string** class .
Just like the other data types, to create a string we first declare it, then we can store a value in it.

:::


:::info
## Scope of article
-This article defines C++ Strings and explains the intuitive logic of this data type.
-This article is going to show you how you can use C++ Strings in programming .
:::

:::success
## Definition
A string is a variable that stores a sequence of letters or other characters.
String class stores the characters as a sequence of bytes with the functionality of allowing access to the single-byte character.
:::



:::warning
## In order to use String data type
The C++ String header **<string>** must be included at the top of the program. Also ,you'll need to include **using namespace std;** to make the short name **string** visible instead of requiring the cumbersome **std::string**. (As a side note,**std** is a C++ namespace for many pieces of functionality that are provided in standard C++ libraries. For the purpose of this class, you won't need to otherwise know about namespaces.) Thus, you would have the following **#include's** in your program in order to use the **string** type.
    
**#include<string>**
**using namespace std;**
:::
 

## Basic Operations on Strings
:::info
1.**Input Functions**
a. **getline()**-- This function is used to store a stream of characters as entered by the user in the object memory.

b.  **push_back()**-- This function is used to input a character at the end of the string.

c. **pop_back()**-- Introduced from C++,this function is used to delete the last character from the string.
    
:::

## Example Program
:::spoiler
#include<iostream>
#include<string>
using namespace std;

int main()
{
    string str;
    getline(cin,str);   //taking the string
    cout<<"the starting string is";
    cout<<str<<endl;
    str.push_bcak('a');  //inserting the string 
    cout<<"the string after the push_back::";
    cout<<str<<endl;
    
    str.pop_back();
    cout<<"the string after the pop_back operation::";
    cout<<str<<endl;
    return 0;
    }
:::
:::info
2. **Counting the number of characters in a string**
a. **str.length()**--The **length** method returns the number of characters in a string, including spaces and punctuation.
**length** is a member function,as we know we invoke member function using *dot notation*.(e.g. **str.length(**))
:::


## Example Program
:::spoiler
#include <string>
#include <iostream>
using namespace std;

int main() {
string small, large;
small = "I am short";
large = "I, friend, am a long and elaborate string indeed";
cout << "The short string is " << small.length()
 << " characters." << endl;
cout << The long string is " << large.length()
 << " characters." << endl;
return 0;
}
    
**Output:**
    The short string is 10 characters.
The long string is 48 characters.
:::
:::info
3.**Accessing individual characters.**
a.**using []**-Using square brackets, you can access individual characters within a string as if it's a **char** array.
:::    


## Example Program
:::spoiler
#include <string>
#include <iostream>
using namespace std;

int main() {
string test;
test = "I am Q the omnipot3nt";
char ch = test[5]; // ch is 'Q'
test[18] = 'e'; // we correct misspelling of omnipotent
cout << test << endl;
cout << "ch = " << ch << endl;
return 0;
}
**Output**
    I am Q the omnipotent
ch = Q
:::
:::info
4. **Capacity Functions**
a.**capacity()**-- This function returns the capacity allocated to the string, which can be equal to or more than the size of the string. 
b.**resize()**-- This function changes the size of the string ,the size can be increased or decreased.

:::

## Example Program
:::spoiler
#include <iostream>
#include <string> // for string class
using namespace std;
    
int main()
{
 string str="hey i am studying in scaler academy";
    cout<<"the intial string is::";
    cout<<str<<endl;
    str.resize(12);
    cout<<"the string after resize operation is::";
    cout<<str<<endl;
    cout<<"the capacity of string is::";
    cout<<str.capacity()<<endl;
    return 0;
}
    
**Output:**
the intial string is::hey i am studying in scaler academy
the string after resize operation is::hey i am stu
the capacity of string is::35
:::
    
:::info
5.**Iterator Functions::**
a. **begin()**-- This functions returns an iterator to the beginning of the string .
b. **end()**-- This function returns an iterator to the end of the string.
c. **rbegin()**-- This function returns a reverse iterator pointing at the end of the string.
d. **rend()**-- This function returns a reverse iterator pointing at beginning of the string.
:::    


## Example Program
:::spoiler
#include <iostream>
#include <string> // for string class
using namespace std;
    
int main()
{
    string str="i am studying in scaler academy";
   string::iterator it;
    string::reverse_iterator it1;
    cout<<"the string for forward iterators is:";
    for(it=str.begin();it!=str.end();it++)
{
    cout<<*it;
    
    }
    cout<<endl;
    cout<<"the reverse string using reverse iterators is:";
    for(it1=str.rbegin();it1!=str.rend();it1++)
{
    cout<<*it1;
    }
cout<<endl;
    return 0;
}
    
**Output**
    the string for forward iterators is:i am studying in scaler academy
the reverse string using reverse iterators is:ymedaca relacs ni gniyduts ma i
:::