Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Initialization of Character Arrays
3.
Initialization of Strings
4.
Functions for String Manipulation
4.1.
Example 1
4.2.
Example 2
5.
FAQs
6.
Key Takeaways:
Last Updated: Mar 27, 2024

Introduction to String

Author Vivek Goswami
2 upvotes

Introduction

Strings are a data type used for storing characters. We can visualize them as an array data structure of bytes of words that hold characters, i.e., a one-dimensional array of characters. Strings are an essential data type as problems on a length of characters can be easily solved using string manipulation techniques.

Since we have already stated that strings are an array Data Structure of bytes of words used to store characters, did it cross your mind to the difference between them? The primary difference lies in the way both are defined and used. However, another significant factor of difference is that strings with the null character '\0'. 

We can easily use strings. Use of Numerous functions is possible on strings that make the handling of the character of the words easy when stored as strings. 

Also see, Literals in C.Fibonacci Series in C++

Initialization of Character Arrays

As a rule, you must always initialize before use.

Syntax:

Char str_name [size of string word] = ”string_word”;

Examples for this are:

char name[]= "LearningStrings";
char name [16]= "LearningStrings";

The string size is equal to the size of the array + 1. Extra space in the string size is because of the null character at the end of the string.

Now, we have learned about character arrays. Let's start our understanding of string data type use and implementation by learning how to initialize them.

Initialization of Strings

We can declare strings with the use of the following syntax:

Syntax:

string variable_name;

Example:
string hello; //empty string
string name=”LearningStrings”;
string greeting = “GoodMorning”;

Also check out - Substr C++

Functions for String Manipulation

Several functions are available for the manipulation of strings in C++. Let us start learning about these functions, which will help us. 

Some essential functions to deal with strings are as follows:

  1. getline(): We can take input of the strings using the function. It takes in and stores all the input streams until a new line comes in the way.
    Syntax for use: getline (cin,string_varaible_name);
     
  2. Push_back(): It pushes a specific character in a string to the end.
    Syntax for use: stringVariable.push_back(‘a’);
     
  3. front(): provides the character at the start of the string
    Syntax for use: stringVariable.front();
     
  4. back(): provides the character at the end of the string.
    Syntax for use: stringVaraible.back(); 

After learning about these basic functions, let us learn how to use them in our code.

Example 1

#include <string.h>
#include <iostream>
using namespace std;
int main()
{
    // Initialising two string varaibles
    string string1, string2;
    // taking one word as string input in a string variable
    cin >> string1;
    // taking one word as string input in a string variable
    cin >> string2;
    
    // declaration of two character type variables
    char f, b;
    // finding and storing first character of string
    cout<<string1<<" "<<string2<<endl;
    f = string1.front();
    b = string2.front();
    cout << f << endl;
    cout << b << endl;
    // using push_back() function to push back a certain character in the string
    string2.push_back('!');
    // Printing modified string2
    cout << string1 << " " << string2 << endl;
    return 0;
}

Input :

Coding
Ninjas

Output :

C
N
Coding Ninjas!

Some other functions that deal with the strings are as follows:

  1. length(): used to return the value of the length of the string defined.
    Syntax for use: int l= string_variable.length();// The integer l stores the value of the length of the string.
     
  2. size ():  used to get the value of the length of the string.
     
  3. begin(): It gives us the reference of the first character.
     
  4. end(): It provides us with the reference of the last character.
     
  5. clear(): It deletes the contents of the string.
     
  6. capacity(): It gives the memory storage size allocated for the string.
    Syntax for use: string_variable.capacity();
     
  7. shrink_to_fit(): To save memory used in the program, we decrease the capacity of
    the string to its size so that no extra memory is utilized.
    Syntax for use: string_varaible.shrink_to_fit();

The functions begin(), end() are used in the string interaction process. A code block that illustrates this is as follows:

Example 2

#include <bits/stdc++.h>
using namespace std;
int main()
{
    // Initialisation of a string
    string fun1 = "let us start string manipulation";
    // Declaration of iterator through the string:
    string::iterator str;
    // printing out the string character
    // use of for loop to traverse through the start to the end of the loop
    for (str = fun1.begin(); str != fun1.end(); str++)
    {
        cout << *str;
    }
    // erasing the content of the string variable;
    fun1.clear();
    return 0;
}

Output:

let us start string manipulation

 

Try and compile with online c++ compiler.

FAQs

  1. What is the difference between an array and a string?
    The main difference is that an array is a data structure while a string is an object. The array can hold any data type, while strings only have character data types. Arrays can be changed, which means they are mutable while strings are not.
     
  2. What is the difference between the char[] and string data types in C++?
    A char array is just an array of characters, whereas a string is a class that contains a char array, but it automatically manages it.
     
  3. How do we compare strings in C++?
    Using std::string::compare(). It compares the value of the given string object (or a substring of the given string) to the sequence of characters that its arguments have specified.

Key Takeaways:

This blog begins with the introduction of a new data type- string. We understand an array of characters. Then we dive into several concepts of string. We learned how to declare and initialize strings. Further, we learned about several functions dealing with the string data types. For learning more such programming related concepts, visit here

Check out this article - C++ String Concatenation

Recommended problems -

Live masterclass