- C++ Course Basics
- C++ Tutorial
- C++ Basic Syntax
- C++ Identifiers
- C++ Character Set
- C++ Input/Output Operator
- C++ Variables
- C++ Data Types
- C++ Formatting Output
- C++ Operators
- C++ Assignment Operator
- C++ Type Conversion
- C++ Program Control
- C++ if and if-else
- C++ switch
- C++ loops
- C++ break and continue
- C++ Functions
- C++ Functions
- C++ Prototype and Definition
- C++ Function Call
- C++ Function Types
- C++ Friend Function
- C++ Function Overloading
- C++ Arrays and Strings
- C++ Arrays
- C++ One-Dimensional Arrays
- C++ Strings
- C++ String Functions
- C++ Structures
- C++ Structures
- C++ Nested Structure
- C++ Structure Array
- C++ Pass Structure to Function
- C++ Pointers
- C++ Pointers
- C++ Memory Map
- C++ Declare Initialize Pointers
- C++ Pointers and Structures
- C++ Object-Oriented
- C++ Object-Oriented
- C++ Classes and Objects
- C++ Constructors and Destructors
- C++ Objects as Function Arguments
- C++ Pointers and Objects
- C++ Data Structure
- C++ Linked List
- C++ Stack
- C++ Queues
- C++ File Handling
- C++ File Handling
- C++ Opening and Closing Files
- C++ Steps to Process Files
- C++ Sequential I/O Operations
- C++ Detecting EOF
- C++ File Pointers Random Access
- C++ Binary Files Operations
- C++ Error Handling
- C++ Misc
- C++ typedef
- C++ #define
- C++ Date and Time
- C++ Examples
- C++ Examples
String and character functions in C++
This post will teach you about the built-in C++ functions that can be used to perform tasks on characters and strings. So, without further ado, let's begin with the "C++ string functions" and then move on to the character functions.
C++ string functions
C++ treats a string as a null-terminated array of characters. In a standard implementation, the string functions require the header file string to provide their prototypes.
Many built-in functions were created by C++ developers to perform tasks related to the strings in the program. However, I will only list and describe the most commonly used ones, or those that are widely used and well-known. So here is a list of the most fundamental and well-known C++ string built-in functions:
Following this paragraph are examples of all of the above C++ string functions, one by one.
C++ length()
The length() function determines the length of the given string. As an example:
#include<iostream> using namespace std; int main() { string myString = "fresherearth"; cout<<"Length = "<<myString.length(); cout<<endl; return 0; }
Length = 12
C++ compare()
The compare() function compares the two strings. If both strings are equal, it returns 0. As an example:
#include<iostream> using namespace std; int main() { string myStringOne = "fresherearth"; string myStringTwo = "fresherearth"; int x = myStringOne.compare(myStringTwo); if(x==0) cout<<"Equal."; else cout<<"Not equal."; cout<<endl; return 0; }
Equal.
C++ swap()
The swap() function swaps the strings. For example:
#include<iostream> using namespace std; int main() { string str1 = "codes"; string str2 = "cracker"; cout<<"Before swap:\n"; cout<<"str1 = "<<str1<<"\tstr2 = "<<str2; str1.swap(str2); cout<<"\n\nAfter swap:\n"; cout<<"str1 = "<<str1<<"\tstr2 = "<<str2; cout<<endl; return 0; }
Before swap: str1 = codes str2 = cracker After swap: str1 = cracker str2 = codes
C++ append()
The append() function adds new text to the string. For example:
#include<iostream> using namespace std; int main() { string str1 = "codes"; string str2 = "cracker"; cout<<"Before append:\n"; cout<<"str1 = "<<str1; str1.append(str2); cout<<"\n\nAfter append:\n"; cout<<"str1 = "<<str1; cout<<endl; return 0; }
Before append: str1 = codes After append: str1 = fresherearth
C++ find()
The find() function finds the index number of the specified word (substring) in a string. It returns the index number of the first character of the substring. As an example:
#include<iostream> using namespace std; int main() { string str = "I had a lot of fun while learning C++."; string word = "fun"; cout<<"Position = "<<str.find(word); cout<<endl; return 0; }
Position = 15
C++ push_back()
The push_back() function adds a new character to the string's end. For example:
#include<iostream> using namespace std; int main() { string str = "codescracke"; cout<<"Before push_back():\n"; cout<<"str = "<<str; str.push_back('r'); cout<<"\n\nAfter push_back('r'):\n"; cout<<"str = "<<str; cout<<endl; return 0; }
Before push_back(): str = codescracke After push_back('r'): str = fresherearth
C++ pop_back()
The pop_back() function removes the last character from the string. For example:
#include<iostream> using namespace std; int main() { string str = "fresherearth"; cout<<"Before pop_back():\n"; cout<<"str = "<<str; str.pop_back(); cout<<"\n\nAfter pop_back():\n"; cout<<"str = "<<str; cout<<endl; return 0; }
Before pop_back(): str = fresherearth After pop_back(): str = codescracke
C++ resize()
The resize() function resizes the string's length. As an example:
#include<iostream> using namespace std; int main() { string str = "jobails.com"; cout<<"Before resize(5):\n"; cout<<"str = "<<str; str.resize(5); cout<<"\n\nAfter resize(5):\n"; cout<<"str = "<<str; cout<<endl; return 0; }
Before resize(5): str = jobails.com After resize(5): str = codes
C++ replace()
When we need to replace a substring from a string, we use the replace() method. As an illustration:
#include<iostream> using namespace std; int main() { string x = "I'm fine. So, how about you?"; string y = "not feeling well"; cout<<"Before replace():\n"; cout<<"x = "<<x; x.replace(4, 4, y); cout<<"\n\nAfter replace():\n"; cout<<"x = "<<x; cout<<endl; return 0; }
Before replace(): x = I'm fine. So, how about you? After replace(): x = I'm not feeling well. So, how about you?
In the preceding example, the first parameter, "4", refers to the position (index); the second parameter, "4", refers to the length or number of characters to be replaced by another string; and the third parameter, "y" (with a value of "not feeling well"), refers to the substring that will replace the content defined by the position and length using the first and second parameters.
C++ character functions
The following list summarizes some important and well-known characters functions available in the C++ programming language:
- isalpha() returns nonzero if the argument is an alphabet. The "isalpha" stands for "is alphabet."
- islower() returns nonzero if the argument is a lowercase letter.
- isupper() returns nonzero if the argument is an uppercase letter.
- tolower() returns the lowercase equivalent of the specified argument.
- toupper() returns the uppercase equivalent of the specified argument.
For example:
#include<iostream> using namespace std; int main() { cout<<islower('A'); cout<<endl; cout<<isupper('A'); cout<<endl; return 0; }
0 1
The function "tolower('A')" returns the ASCII value of the lowercase 'a'. Similarly, "toupper('a')" returns the ASCII value of uppercase "A."
« Previous Tutorial Next Tutorial »