Built-in C Strings Function

Learn how built-in string functions work.

Built-in string functions in C++

We have discussed string functions in detail so far. Now we’ll discuss the built-in functions of the string libraries. Previously, we had to write detailed code for each function. Did you know instead of reinventing the wheel, we can simply achieve all that functionality by calling existing library functions?

For example, we need to call the functions to copy or reverse the string. We don’t need to write complete code. This will make the program simple. To use the string functions, we’ll include standard libraries:

  • cstring: For the strcpy and strcat functions
  • cctype: For the tolower and toupper functions

We have a complete chart of string functions below:

String Functions

Function

Description

strlen()

To check the string length

strcpy()

To copy the content of the string

strcat()

To concatenate two strings

strcmp()

To compare two strings

tolower()

To convert the string into lowercase

toupper()

To convert the string into uppercase

Here’s the example program which uses all these helper functions, which are available in different libraries. There are several other functions in the library that we may explore online.

Press + to interact
#include<iostream>
#include<stdio.h>
//The library below must be included for the strcpy and strcat functions to work
#include<cstring>
//The library below must be included for the tolower and toupper functions to work
#include<cctype>
using namespace std;
int main()
{
char S[15] = "THIS IS a cat";
char T[15] = "THIS";
char ch;
// 1. Using strlen()
cout << "String length using strlen() function ";
cout << "\nstrlen(THIS IS a cat) " << strlen(S) << "\nstrlen(THIS) " << strlen(T) << endl;
// 2. Using tolower()
cout << "\nConverting 'THIS' to 'this' using tolower() function: " << endl;
for (int i = 0; i < strlen(T); i++)
{
ch = tolower(T[i]);
cout << ch;
}
// 3. Using toupper()
cout << "\nConverting 'THIS IS a cat' to 'THIS IS A CAT' using toupper() function: " << endl;
for (int i = 0; i < strlen(S); i++)
{
ch = toupper(S[i]);
cout << ch;
}
cout << endl;
// 4. Using strcmp() and strcpy()
cout << "\nComparison(strcmp-function): ";
strcpy(T,"abc");
strcpy(S,"abc");
cout << "\nstrcmp(" << T << " " << S << ") = " << strcmp(T,S) << endl;
// 5. Using strcpy() and strcmp() again
strcpy(S, "This");
strcpy(T, "dog");
cout << "strcmp(" << T << " " << S << ") = " << strcmp(T,S);
// 6. Using strcat()
strcpy(S, "This ");
strcpy(T, "is a dog.");
cout << "\nstrcat(" << S << " , " << T << ") = " << strcat(S,T);
// strcat not only concatenates the two strings into the
// first string but also erases the first string
return 0;
}

In line 40, strcmp compares character by character. If the characters from the 0th index are equal, it moves forward. Otherwise, it returns the difference in ASCII between the two characters. Therefore, a positive returned value means the first string is lexicographically greater than the second. A negative returned value means the second is greater, but when 0 is returned, it means both strings are equal.

Find the mistake

Look at the code below and see if you can find out why strcat() gives the unexpected output:

Press + to interact
#include<iostream>
#include<stdio.h>
//The library below must be included for the strcpy and strcat functions to work
#include<cstring>
using namespace std;
int main()
{
char S[] = "THIS IS a cat";
char T[] = "THIS";
strcpy(S, "This ");
cout << "Before copying in T[], S: "<<S<<endl;
strcpy(T, "is a dog.");
cout << "After copying in T[], S: "<<S<<endl; // T is of size 4 but we have copied more than 4 characters
// causing the characters to be written in the region of S
cout << "T: "<<T<<endl;
cout << "\nstrcat(" << S << " , " << T << ") = " << strcat(S,T);
return 0;
}