GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • CODESCRACKER
  • Python
  • Java
  • C++
  • JavaScript
  • SQL
  • Quiz
  • C
  • C#
  • HTML
  • CSS
  • PHP
  • Networking
  • Operating System

This view

  • Original page
  • New address

GeezerVison is showing codescracker.com — String and character functions in C++

  • 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:

  • length()
  • compare()
  • swap()
  • append()
  • find()
  • push_back()
  • pop_back()
  • resize()
  • replace()

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 »
Practice Quizzes

Test your programming and computer science knowledge with interactive MCQs and topic-based quizzes.

All Quizzes SQL Quiz Python Quiz Java Quiz C++ Quiz
Popular Topics

Explore the most popular programming and computer science tutorials on CodesCracker.

Python Tutorial Java Tutorial C++ Tutorial SQL Tutorial JavaScript Tutorial HTML Tutorial CSS Tutorial Computer Networking
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.