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:

C++ Code
#include<iostream>
using namespace std;
int main()
{
   string myString = "codescracker";
   cout<<"Length = "<<myString.length();

   cout<<endl;
   return 0;
}
Output
Length = 12

C++ compare()

The compare() function compares the two strings. If both strings are equal, it returns 0. As an example:

C++ Code
#include<iostream>
using namespace std;
int main()
{
   string myStringOne = "codescracker";
   string myStringTwo = "codescracker";

   int x = myStringOne.compare(myStringTwo);
   if(x==0)
      cout<<"Equal.";
   else
      cout<<"Not equal.";

   cout<<endl;
   return 0;
}
Output
Equal.

C++ swap()

The swap() function swaps the strings. For example:

C++ Code
#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;
}
Output
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:

C++ Code
#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;
}
Output
Before append:
str1 = codes

After append:
str1 = codescracker

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:

C++ Code
#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;
}
Output
Position = 15

C++ push_back()

The push_back() function adds a new character to the string's end. For example:

C++ Code
#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;
}
Output
Before push_back():
str = codescracke

After push_back('r'):
str = codescracker

C++ pop_back()

The pop_back() function removes the last character from the string. For example:

C++ Code
#include<iostream>
using namespace std;
int main()
{
   string str = "codescracker";

   cout<<"Before pop_back():\n";
   cout<<"str = "<<str;

   str.pop_back();
   
   cout<<"\n\nAfter pop_back():\n";
   cout<<"str = "<<str;

   cout<<endl;
   return 0;
}
Output
Before pop_back():
str = codescracker

After pop_back():
str = codescracke

C++ resize()

The resize() function resizes the string's length. As an example:

C++ Code
#include<iostream>
using namespace std;
int main()
{
   string str = "codescracker.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;
}
Output
Before resize(5):
str = codescracker.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:

C++ Code
#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;
}
Output
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:

C++ Code
#include<iostream>
using namespace std;
int main()
{
   cout<<islower('A');
   cout<<endl;
   cout<<isupper('A');
   cout<<endl;

   return 0;
}
Output
0
1

The function "tolower('A')" returns the ASCII value of the lowercase 'a'. Similarly, "toupper('a')" returns the ASCII value of uppercase "A."

C++ Quiz


« Previous Tutorial Next Tutorial »


Follow/Like Us on Facebook


Subscribe Us on YouTube