EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

C++ String Functions

Home » Software Development » Software Development Tutorials » C ++ Programming Tutorial » C++ String Functions

C++ String Functions

C++ String Functions

String function are the functions that are used to perform operations on a string. C++ uses <string.h> library to provides various string functions like strcat, strlen, strcmp, strcpy, swap, and many more where strcat is used to concatenate string, strlen will calculate the length of the string, strcmp is used to compare two strings, strcpy will copy one value of the string to another, a swap is used to swap value between strings.

What is the string?

To use string functions in C++ we need to add a library named <string> in our code at the top, which gives you string functions. It must be included with the header file #include <string>. As we know there are many behaviors that string object understands and several operations we can perform on the string object.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Examples of String Functions in C++

Here we will discuss how to use string function in C++ programming with the help of examples

Example #1:

String Greeting = “Hello World!”;
Cout<<Greeting;

Which gives the following Output

Output: Hello World!

As we know cout<< is used to print on the screen in c++, and cin>> is to take input on the screen.

Let’s see the following example to be more precise:

Example #2:

String greeting ;
Cin>>greeting;

Cout<< ” The common sentence in programming is: ”<< greeting << endl;

Here our goal is to learn how can we play with the string.

Example #3:

//Suppose we have three string variables

string str1 = “ice”; //initialized with value
string str2 = “cream”; //initialized with value
string str3; // empty string
str3 = str1 + str2; // Here we are concatenating the string
cout << str3;

Popular Course in this category
C++ Training (4 Courses, 5 Projects, 4 Quizzes)4 Online Courses | 5 Hands-on Projects | 37+ Hours | Verifiable Certificate of Completion | Lifetime Access | 4 Quizzes with Solutions
4.5 (4,890 ratings)
Course Price

View Course

Related Courses
Java Training (40 Courses, 29 Projects, 4 Quizzes)C Programming Training (3 Courses, 5 Project)

Output: icecream

How we achieved this?

The standard string class in c++ overloads the assignment operator (=). To be more clear see Example #3. We have three objects str1, str2, str3. We concatenated two strings i.e str1 and str2 and the value are get copied into str3. that means assignment operator got overloaded and new value in our case is icecream got copied into str3.

The string class has a default constructor that initializes string object to an empty string. Standard c++ has another constructor which takes the value(ex.str1 and str2 has the value assigned, and str3 is empty)

Following are some of the C++ String functions we can use:

  • Substr(beginning char index, from that index how many characters you want.)
  • Strcat(str1,str2): Appending the string
  • Strcmp(str1,str2): Returns -ve value if str1 is less than str2;0 if str1 is equal to str2; and >0 (+ve value) if str1 is greater than str2.
  • Strcpy(str1,str2): Replace the content
  • Strlen(str1): Gives the length of the string

Substr() : This function is very simple one. As the name suggests it. take the substring from the given string. This function takes two parameters.

  1. The first parameter suggests starting index no.
  2. The second parameter suggests how many characters you want from the starting index.

Example #4:

string s = “C++ is an easy language”;

c + +   i s   a   e a s y   l a n g u a g e
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21

Index starts at 0
string language = s.substr(0,3); // output of substr storing in language variable.
cout << language << endl;

Output: c++

Starting index is 0 and we need three characters from the 0th index so 3 is the second parameter. The second parameter works from 1 to n. not from 0 to n. so first three char gives us c++.

Strcat(): This string function in C++ combines two different strings, As shown in Example #5.

Example #5:

String str1 = "I love my";
string str2 = " Country";
strcat(str1, str2);
cout << str1 ;

In the above example, the strcat function takes the copy str2 value and put it in str1. It combines to and gives output as below:

Output: I love my Country

Strcmp(): As the name suggests this compares two strings and gives back the result.

Suppose we have two strings str1 and str2.

Following table shows exact output for better understanding:

str1 < str2 Returns –ve value
str1 == str2 Returns 0(Zero)
str1 > str2 Returns +ve value

Example #6:

string str1 = “We have seven Continents in the wolrd”;
string str2 = “We have seven Continents in the wolrd”;
int result = strcmp(str1,str2);
cout << result <<endl;

Output: 0 // both the strings are equal str1 == str2

In other cases, it returns ASCII value of string depending on the character at that position.

Strcpy(): unlike strcat, it will not append string into other . it will replace all the content.

Example #7:

string str1 = “World is beautiful”;
string str2 = “Yes we can”;
string str3 = strcpy(str1,str2); // simply replace all the content in str1 with the content of str2
cout<< str3 <<endl;

Output: Yes we can

Strlen(): The simplest function in a row.

This function defined in <cstring> header file. This function returns the length of the string.

The length of a string is determined by the terminating null-character at the end \0.

Example #8:

string str1 = "c++ is object oriented language";
int length = strlen(str1);
cout << "Length of str1 is : " << length << endl;

Output: 31

getline(): C++ string library functions also provide the getline function to read the whole line.

This function takes arguments as follows:

  1. It takes the first argument as a stream to read from.
  2. Second, it takes the input line
  3. And third, that stops the extraction

Example #9:

cout<<”What is your name: ”<<endl;
string str1;
getline(cin, str1 ‘\n’);
cout<< “your name is : ” << str1 << endl;

Output: What is your name: John

Your name is John

One of the most useful data types supplied in the C++ libraries is the string.

For a better understanding of string, you must code while learning. We can’t get the actual concept without writing the code.

BOOKS:

  1. Balgurusami (Object-Oriented Programming with C++)
  2. Object-Oriented Programming in C++ by Robert Lafore (Publisher: Pearson)

Recommended Articles

This has been a guide to C++ String Functions. Here we discussed how to use string function in C++ programming with the help of examples. You can also go through our other suggested articles to learn more–

  1. C++ Commands
  2. String Functions In Java
  3. strcat() in C++
  4. strcat() in C

C++ Training (4 Courses, 3 Projects, 4 Quizzes)

4 Online Courses

5 Hands-on Projects

37+ Hours

Verifiable Certificate of Completion

Lifetime Access

4 Quizzes with Solutions

Learn More

2 Shares
Share
Tweet
Share
Primary Sidebar
C plus plus Programming Tutorial
  • Functions
    • C++ String Functions
    • Math Functions in C++
    • Friend Function in C++
    • Recursive Function in C++
    • Virtual Functions in C++
    • strcat() in C++
    • swap() in C++
    • strcmp() in C++
    • ceil function in C++
    • C++ begin()
    • size() in C++
    • C++ test()
    • C++ any()
    • C++ Bitset
    • C++ find()
    • C++?Aggregation
    • C++?String append
    • C++ String Copy
    • C++ end()
    • C++ endl
    • C++ push_back
    • C++ shuffle()
    • malloc() in C++
    • C++ reserve()
    • C++ unique()
    • C++ sort()
    • C++ find_if()
    • Reflection in C++
    • C++ replace()
    • C++ search()
    • C++ Memset
    • C++ size_t
    • C++ Substring
    • C++ Max
    • C++ absolute value
    • C++ memcpy
    • C++ wchar_t
    • C++ free()
    • C++ sizeof()
    • C++ Move Semantics
  • Basic
    • Introduction To C++
    • What is C++
    • Features of C++
    • Applications of C++
    • Best C++ Compiler
    • C++ Data Types
    • C++ Double
    • C++ unsigned int
    • User Defined Data Types in C++
    • Variables in C++
    • C++ Keywords
    • Pointers in C++
    • C++ Void Pointer
    • Function Pointer in C++
    • Iterator in C++
    • C++ Commands
    • Object in C++
    • C++ Literals
    • C++ Reference
    • C++ Undefined Reference
    • String in C++
    • C++ Programming Language (Basics)
    • C++ Identifiers
    • C++ Header Files
    • Type Casting in C++
    • C++ Formatter
  • Operators
    • C++ Operators
    • Arithmetic Operators in C++
    • Assignment Operators in C++
    • Bitwise Operators in C++
    • Relational Operators in C++
    • Boolean Operators in C++
    • Unary Operators in C++
    • C++ Operator[]
    • Operator Precedence in C++
    • C++ operator=()
  • Control Statements
    • Control Statement in C++
    • if else Statement in C++
    • Else If in C++
    • Nested if in C++
    • Continue Statement in C++
    • Break Statement in C++
    • Switch Statement in C++
    • goto Statement in C++
    • C++ Struct
    • Loops in C++
    • Do While Loop in C++
    • Nested Loop in C++
  • Array
    • Arrays in C++
    • 2D Arrays in C++
    • 3D Arrays in C++
    • Multi-Dimensional Arrays in C++
    • C++ Array Functions
    • String Array in C++
    • C++ Length of Array
    • C++ arraylist
  • Constuctor and Destructor
    • Constructor and Destructor in C++
    • Constructor in C++
    • Destructor in C++
    • Copy Constructor in C++
    • Parameterized Constructor in C++
  • Overloading and overriding
    • Overloading and Overriding in C++
    • Overloading in C++
    • Overriding in C++
    • Function Overloading in C++
    • Function Overriding in C++
    • Method Overloading in C++
  • Inhertiance
    • Types of Inheritance in C++
    • Single Inheritance in C++
    • Multiple Inheritance in C++
    • Hierarchical Inheritance in C++
    • Multilevel Inheritance in C++
    • Hybrid Inheritance in C++
  • Sorting
    • Sorting in C++ 
    • Heap Sort in C++
    • C++ Vector Sort
    • Insertion Sort in C++
    • Selection Sort in C++
  • Advanced
    • C++ namespace
    • Encapsulation in C++
    • Access Modifiers in C++
    • Abstract Class in C++
    • C++ Class and Object
    • What is Template Class in C++?
    • C++ Algorithm
    • Data Structures and Algorithms C++
    • C++ Garbage Collection
    • Virtual Keyword in C++
    • Access Specifiers in C++
    • Storage Class in C++
    • Call by Value in C++
    • Multimap in C++
    • C++ Multiset
    • C++ Lambda Expressions
    • Stack in C++
    • C++ Static
    • C++ static_cast
    • Deque in C++
    • C++ Vector Functions
    • C++ 2D Vector
    • C++ List
    • C++ Mutable
    • Enum in C++
    • Abstraction in C++
    • Signal in C++
    • C++ Queue
    • Priority Queue in C++
    • Regular Expressions in C++
    • C++ Hash Table
    • File Handling in C++
    • C++ Stream
    • ifstream in C++
    • C++ ofstream
    • C++ fstream
    • C++ Read File
    • C++ iomanip
    • Macros in C++
    • Templates in C++
    • C++ setprecision
    • C++ Int to String
    • C++ thread( )
    • C++ Thread Pool
    • C++ thread_local
  • Programs
    • Patterns in C++
    • Star Patterns In c++
    • Swapping in C++
    • Reverse Number in C++
    • Palindrome Program in C++
    • Palindrome in C++
    • Factorial Program in C++
    • Fibonacci Series in C++
    • Square Root in C++
    • Random Number Generator in C++
    • Prime Number in C++
    • Leap Year Program in C++
    • Anagram in C++
    • Armstrong Number in C++
    • Reverse String in C++
    • Socket Programming in C++
    • Matrix Multiplication in C++
    • C++ using vs typedef
    • C++ vector vs list
    • C++ vector vs array
  • Interview question
    • C++ Interview Questions
    • Multithreading Interview Questions C++

Related Courses

C++ Training Course

Java Training Course

C Programming Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - C++ Training (4 Courses, 3 Projects, 4 Quizzes) Learn More