Introduction to Strings

Defining a String

A string is an array of characters whose last character is \0. A typical string, such as Pacifique, is graphically represented as follows: 

P a c i f i q u e \0

The last character \0 is called the null-terminating character. For this reason, a string is said to be null-terminated. The string library ships with a lot of functions used to perform almost any type of operation on almost any kind of string. Used under different circumstances, the string functions also have different syntaxes.

The strings that you can use in your program may be defined in various libraries depending on your compiler but most of the time, they are available once you include the string library that is defined in the std namespace. For example, the functions we are about to review are part of the C language. The strings that are part of the (C++) Standard Template Library (STL) are defined in the string class of the std namespace. Based on this, most compilers make all these functions accessible once you include the string library and the std namespace in your program.

String Manipulation Functions

The C++ and its parent the C languages do not have a string data type. In C and C++, strings are created from arrays. Therefore, the C++ language relies on operations performed on the arrays of characters or pointers to char. The functions used for this purpose are numerous and you should know what they are used for.

As a reminder, thanks to the features of arrays of characters and the friendship between arrays of characters and strings, there are two main ways you can declare and initialize a string:

char *Thing = “Telephone”;

char Major[] = “Computer Sciences”;

The Length of a String

In many operations, you will want to know how many characters a string consists of. To find the number of characters of a string, use the strlen() function. Its syntax is:

int strlen(const char* Value);

The strlen() function takes one argument, which is the string you are considering. The function returns the number of characters of the string. Here is an example:

#include <iostream>
using namespace std;

int main()
{
    char *School = "Manchester United";

	int Length = strlen(School);

    cout << "The length of \"" << School
	     << "\" is " << Length << " characters\n\n";
}

This would produce:

The length of "Manchester United" is 17 characters

The strcat() Function

If you have two strings, to append one to another, use the strcat() function. Its syntax is:

char *strcat(char *Destination, const char *Source);

The strcat() function takes two arguments. The second argument, called the source string, is the string you want to add to the first string; this first string is referred to as the destination. Although the function takes two arguments. It really ends up changing the destination string by appending the second string at the end of the first string. This could be used to add two strings. Here is an example:

#include <iostream>
using namespace std;

int main()
{
    char *Make = "Ford ";
    char *Model = "Explorer";

    cout << "Originally, Make = " << Make;

	strcat(Make, Model);

    cout << "\n\nAfter concatenating, Make = " << Make << endl;
}

This would produce:

Originally, Make = Ford

After concatenating, Make = Ford Explorer

The strncat() Function

Like the strcat() function, the strncat() function is used to append one string to another. The difference is that, while the strcat() considers all characters of the source string, the strncat() function allows you to specify the number of characters from the source string that you want to append to the destination string. This means that, if the source string has 12 characters, you can decide to append only a set number of its characters. The syntax is:

char* strncat(char* Destination, const char* Source, int Number);

Besides the same arguments as the strcat() function, the Number argument sets the number of characters considered from Source. To perform the concatenation, the compiler would count characters from left to right on the source string. Here is an example:

#include <iostream>
using namespace std;

int main()
{
    char *Make = "Ford ";
    char *Model = "Explorer";

    cout << "Originally, Make = " << Make;

	strncat(Make, Model, 3);

    cout << "\n\nAfter concatenating, Make = " << Make;
}

This would produce:

Originally, Make = Ford

After concatenating, Make = Ford Exp

Copying One String Into Another

The strcpy() function is used to copy one string into another string. In English, it is used to replace one string with another. The syntax of the strcpy() function is:

char* strcpy(char* Destination, const char* Source);

This function takes two arguments. The first argument is the string that you are trying to replace. The second argument is the new string that you want to replace. There are two main scenarios suitable for the strcpy() function: To replace an existing string or to initialize a string.

The following would initialize a string:

char CarName[20];

strcpy(CarName, "Toyota Camry");

cout << "Car Name: " << CarName;

If you have two strings and copy one into another, both strings would hold the same value: 

#include <iostream>
using namespace std;

int main()
{
    char carName1[] = "Ford Escort";
    char carName2[] = "Toyota 4-Runner";

    cout << "The String Copy Operation";
    cout << "\nFirst Car: " << carName1;
    cout << "\nSecond Car: " << carName2;

	strcpy(carName2, carName1);

    cout << "\n\nAfter using strcpy()...";
    cout << "\nFirst Car: " << carName1;
    cout << "\nSecond Car: " << carName2 << endl;
}

This would produce:

The String Copy Operation
First Car: Ford Escort
Second Car: Toyota 4-Runner

After using strcpy()...
First Car: Ford Escort
Second Car: Ford Escort

The strncpy() Function

The strncpy() function works like the strcpy() function. As a difference, the strncpy() function allows you to specify the number of characters that the compiler would copy from the source string. Here is the syntax:

char* strncpy(char* Destination, const char* Source, int Number);

The Number argument specifies the number of characters that will be copied from the Source string. Here is an example:

#include <iostream>
using namespace std;

int main()
{
    char CarName1[] = "Ford Escort";
    char CarName2[] = "Toyota 4-Runner";

    cout << "The String Copy Operation";
    cout << "\nFirst Car: " << CarName1;
    cout << "\nSecond Car: " << CarName2;

	strncpy(CarName2, CarName1, 8);

    cout << "\n\nAfter using strncpy() for 8 characters";
    cout << "\nFirst Car: " << CarName1;
    cout << "\nSecond Car: " << CarName2 << endl; 
}

This would produce:

The String Copy Operation
First Car: Ford Escort
Second Car: Toyota 4-Runner

After using strncpy() for 8 characters
First Car: Ford Escort
Second Car: Ford Esc-Runner

The strdup() Function

The strdup() function is used to make a copy of create a duplicate of that string. Its syntax is:

char* strdup(const char *S);

This function takes as an argument the string you want to duplication and returns the duplicated string. 

#include <iostream>
using namespace std;

int main()
{
    char *FirstName1 = "Charles";
    char *FirstName2 = "Andy";
    char *LastName1 = "Stanley";
    char *LastName2;

	LastName2 = strdup(LastName1);

    cout << "Father: " << FirstName1 << ' ' << LastName1 << endl;
    cout << "Son: " << FirstName2 << ' ' << LastName2;
}

This would produce:

Father: Charles Stanley

Son: Andy Stanley

Comparing Strings

Various routines are available for strings comparison. The C-String library is equipped with functions that perform the comparisons by characters. Alternatively, some compilers like Borland C++ Buider ships with additional multiple functions to perform these comparisons on null-terminated strings.

The strcmp() Function

The strcmp() function compares two strings and returns an integer as a result of its comparison. Its syntax is:

int strcmp(const char* S1, const char* S2);

This function takes two strings, S1 and S2 and compares them. It returns:

#include <iostream>
using namespace std;

int main()
{
    char *FirstName1 = "Andy";
    char *FirstName2 = "Charles";
    char *LastName1 = "Stanley";
    char *LastName2 = "Stanley";

	int Value1 = strcmp(FirstName1, FirstName2);
	int Value2 = strcmp(FirstName2, FirstName1);
	int Value3 = strcmp(LastName1, LastName2);

    cout << "The result of comparing " << FirstName1
		 << " and " << FirstName2 << " is\t" << Value1 << endl;

    cout << "The result of comparing " << FirstName2
		 << " and " << FirstName1 << " is\t" << Value2 << endl;

    cout << "The result of comparing " << LastName1
		 << " and " << LastName2 << " is\t" << Value3;
}

This would produce:

The result of comparing Andy and Charles is -2
The result of comparing Charles and Andy is 2
The result of comparing Stanley and Stanley is 0

The strncmp() Function

The strncmp() function compares two strings using a specified number of characters and returns an integer as a result of its findings. Its syntax is:

int strncmp(const char* S1, const char* S2, int Number);

This function takes three arguments. The first two arguments are the strings that need to be compared. The 3rd argument specifies the number of characters considered for the comparison. It returns

The stricmp() Function

The stricmp() function compares two strings without regard to their case. In other words, this function does not take into consideration if there is a mix of uppercase and lowercase characters in the strings. The syntax of the function is:

int stricmp(const char* S1, const char* S2);

This function takes two strings, S1 and S2 and compares them. It returns

  • A negative value if S1 is less than S2
  • 0 if S1 and S2 are equal
  • A positive value if S1 is greater than S2 

The strnicmp() Function

The strnicmp() function compares two strings without regard to their case but considers only a specified number of characters. The syntax of the function is:

int strnicmp(const char* S1, const char* S2, int n);

This function takes two strings, S1 and S2 and compares them. It returns

  • A negative value if S1 is less than S2
  • 0 if S1 and S2 are equal
  • A positive value if S1 is greater than S2

Working With Individual Characters

The strchr() Function

The strchr() function looks for the first occurrence of a certain character in a string. Its syntax is:

char* strchr(const char* S, char c);

This function takes two arguments. The second argument specifies what character to look for in the first argument which is a string. If the character c appears in the string S, the function would return a new string whose value starts at the first occurrence of c in S. If the character c does not appear in the string S, then the function would return NULL.

The strrchr() Function

The strrchr() function examines a string starting at the end (right side) of the string and looks for the first occurrence of a certain character. Its syntax is:

char* strrchr(const char* S, char c);

The first argument is the string that needs to be examined. The function will scan the string S from right to left. Once it finds the first appearance of the character c in the string, it would return a new string whose value starts at that first occurrence. If the character c does not appear in the string S, then the function would return NULL.

Working With Sub-Strings

The strstr() Function

The strstr() function looks for the first occurrence of a sub-string in another string and returns a new string as the remaining string. Its syntax is:

char* strstr(const char* Main, const char *Sub);

The first argument of the function is the main string that would be examined. The function would look for the second argument, the Sub string appearance in the main string. If the Sub string is part of the Main string, then the function would return a string whose value starts at the first appearance of Sub and make it a new string. If Sub is not part of the Main string, the function would return a NULL value. 

Working With Character Cases

The strlwr() Function

The strlwr() function is used to convert a string to lowercase. Its syntax is: 

char *strlwr(const char *S);

This function takes, as argument, the string that needs to be converted. During conversion, if a Latin character were in uppercase, it would be converted to lowercase. Otherwise, it would stay “as if”. This means any symbol that is not a readable character would not be converted. 

#include <iostream>
using namespace std;

int main()
{
    char CustomerAddress[] = "4812 LOCKWOOD Drive #F04";

    cout << "Customer Address: " << CustomerAddress << endl;

    char *ShippingAddress = strlwr(CustomerAddress);

    cout << "Shipping Address: " << ShippingAddress << endl;
}

This would produce:

Customer Address: 4812 LOCKWOOD Drive #F04

Shipping Address: 4812 lockwood drive #f04 

The strupr() Function

The strupr() function is used to convert a string to uppercase. Its syntax is: 

char *strupr(const char *S);

Each lowercase character in the function’s argument, S, would be converted to uppercase. Any character or symbol that is not in lowercase would not be changed. 

#include <iostream>

using namespace std;



int main()

{

    char Drink[] = "100% Apple Juice";

    char *SayItLoud;



    cout << "What is that drink? " << Drink << endl;



	SayItLoud = strupr(Drink);

    cout << "Say it loud: " << SayItLoud << endl; 





}

This would produce:

What is that drink? 100% Apple Juice

Say it loud: 100% APPLE JUICE
 

Formatting Strings

 

The sprintf() Function

The sprintf() function is used to format data and specify how it should display. Its syntax is:

int sprintf(char* Buffer, const char* S, Arguments…);

This function takes at least two arguments and could take more. The first argument is a null-terminated string that could display one or a few words and the formula to use when displaying the second or more argument. To display a value as part of the Buffer, type a double-quote followed by the string if any, followed by the % sign, follow by one of the following characters:

 
Character Used to Display character Used to Display
s A string f Floating-point value
c A character d An integer

Previous Copyright © 2000-2026, FunctionX Wednesday 24 September 2025, 17:16 Next