Showing posts with label Functions of strings. Show all posts
Showing posts with label Functions of strings. Show all posts

Jul 17, 2020

Strings Functions in C++ | C++ Language | Coding Winds

Stings Functions in C++

Hello people, today we are going to read about the pre-defined functions of String datatype. If you work on  C++ then you must have, atleast once, gone through the confusion regarding the strings functions which are there in C++. String is the most important data structure provided by the C++ language. To work more efficiently on this data structure you must have the basic knowledge of the following functions which are predefined in C++ or in <string> library :

1.    1.    String.length();  or strlen();

2.    String.append();

3.    String.erase();

4.    Str.replace();

5.    Strcat();

6.    Strcmp();

7.    Strcpy();

8.    Reverse();

Let’s discuss each of these further in detail :


string.append();

Many people know about the basic functionality of this function of string, i.e. APPEND but what people don’t about it is that we can use this function with no. of variations in inputs. As the name suggests append works like adding the two strings, i.e.    STRING1 = STRING1 + STRING2

So the basic way in which we use this function to add two string is as follows:

                                                STRING1.append(STRING2)

But other ways in which we can change the functionality of this append function is as follows:

STRING1 : “Coding”

STRING : "Winds"


  STRING1.append(STRING2);                       // "CodingWinds"

 

  STRING1.append(STRING2,0,3);                   // "CodingWin"

 

  STRING1.append("Winds",3);          // "CodingWin"

 

  STRING1.append("Platform");                   // "CodingPlatform"

 

  STRING1.append(5u,'.');                    // "Coding....."

 

string.length(); or strlen();

These two functions are used to calculate the length of strings. When using these we don’t need to write the whole function for calculating length of the string. We can just define an integer variable and evaluate it with these functions because these  functions return integers which is equal to the length of the string.

SYNTAX:

                                    int l = StringName.length();

                                                            OR

                                    int l = strlen(StringName);

           

str.cmp(STRING1,STRING2)

This function of string helps in the comparision of strings it returns 0 if STRING1 = STRING2, some negative value if STRING1 < STRING2 and some positive value if STRING1>STRING2.


str.erase();

This function is used to delete a part of string, for doing the we can use this pre-defined function “string.erase();” in the following ways : 

str = “Coding Winds”

str.erase();    // This erases the whole string

str.erase(1,5);          // ”C Winds”

str.erase(str.begin()+7);    //”Winds”

str.erase(str.begin()+0,str.end()-6);       //”Winds”

  

str.replace();

 This function is responsible for replacing a part of string with other string or replacing a part of string with a part of other string. We can do this by using following methods :

str = “Coding”

str2 = “g Winds”

str3 = “g Best Platform” 


str.replace(5,1,str2);          // "Coding Winds" 
str.replace(5,1,str3,0,6);     // "Coding Best"  
str.replace(6,1,"just a ");     // "Coding just a Best"

strcpy(str1,str2);

This copies str2 into the place of str1, i.e., this replaces the str1 completely with str2. We can use this functions as follows :

str1 : “Code for good!”

str2 : “Coding Winds”

strcpy(str1,str2);

str1 : “Coding Winds”

str2 : “Coding Winds”

 

strcat(str1,str2);

This strcat() function concatenates str2 onto the ending of str1. We can use this function in the following way :

str1 = “Coding”

str2 = “Winds”

strcat(str1,str2);

str1 = “CodingWinds”

 

str.erase();

This erase function of string datatypes helps us in deleting a part of string. We can do this by following this code given below :

str = “Coding Winds”

str.erase();    // This erases the whole string

str.erase(1,5);          // ”C Winds”

str.erase(str.begin()+7);    //”Winds”

str.erase(str.begin()+0,str.end()-6);       //”Winds”

 

reverse();

Reverse is an inbuilt function, which helps in reversing a string. And we can do this as follows :

String1 : “Coding Winds”

Syntax :      reverse(str.begin(),str.end());

This implies reverse(String1.begin() , String1.end()) will give an output string as :

            “sdniW gnidoC”

 

So yes, these are all basic functions of string in C++ which you must know to use string datatype more efficiently. Hope you gain some knowledge from this article. If you are aware of some other such string related pre-defined functions then do let us know in the comment section.

And don’t forget to have us on your instagram (@coding.winds) and also follow us on Linkedin (Coding Winds).

Thank You for giving this a read.

Keep Coding!