Learn strcpy, strcat and strlen function in C++ Programming

0
2919
C++Strings

C++Strings

In the previous article, we had an introduction to Strings. We have learned the original implementation of strings as arrays of characters. In this article, we are going to utilize what we have learned in the previous one, and build upon.

Using Arrays of Strings
C++ supports multi-dimensional arrays. The simplest form of this type is the two-dimensional array. A two dimensional-array is an array of arrays; i.e. each element of the array is itself an array. Following this definition, and given that a string is an array of characters, we can conclude that an array of strings is actually a two-dimensional array of characters.

To define an array of strings, use the following syntax:

Where:
STRARRAY is the name of the array of strings. It could be any valid C++ identifier.
ROWS is the number of elements (strings) that the array contains.
MAXSTRLENGTH is the maximum possible length of the strings.

Example
The following program stores the month names in an array of strings called months.

When executed, we should get the following output:
1

In the array definition:

  • The number 12 is the number of elements (strings) in the outer array. This is self-explanatory: 12 elements for 12 months.
  • The number 10 is the maximum length an individual string could be. Although the longest month name is 9 letters (September), the terminating null character ‘\0’ makes the required number of (inner) character array elements 10.

Useful String Functions
C++ has a rich set of functions that manipulate strings. In this section, we are going to investigate some of these functions and their uses, and will illustrate our talk with examples.

The strcpy() Function
This function copies a string from source to destination. The function takes two string variables as arguments: the destination, and the source, then returns the updated destination variable.

Example
The following program defines a class named student, which has two member data items: name and benchnum. The constructor uses the strcpy() function to initialize the name variable in new objects.

That is what we get when executing this code:
2

There are two points that need some explanation in this program:

  • First: the use of the string.h header file, which is necessary for the program to compile successfully, and not complain about the strcpy() function.
  • Second: in the member function definition:

The expression char *  (in the return value type) is another method to define strings. We will talk about this method in more details later in this series when talking about Pointers. But for now, remember that char * defines a string.

Learn the Basics of C Programming Language

The strcat() Function
The strcat() function concatenates two strings. It appends a copy of the source string to the end of the destination string, and then returns the destination string.

This function also require including the string.h library file.

Example
The following example concatenates the user’s first and last names into a string variable fullname, and prints it.

This program should behave like this:
3

After receiving the user’s input (first and last), the program copies the string variable first (first name) into the variable fullname:

Now, the fullname contains the first name. Next, the program uses the strcat() function to append a white space after the first name into the fullname string variable:

Finally, the last name is appended to fullname string variable:

The strlen() Function
I know you have been waiting for this!!! This function calculates the length of  string.

Example
In the previous string concatenation example, consider adding the following line of code just before the getch() function at the end of the main() function:

This line will print the lengths of the string variables first, last, and fullname, respectively.
4

Summary
In this article, we have continued our talk about Strings.

  • You can define an array of strings. In its reality, it is a two-dimensional array (array of arrays).
  • C++ has a rich set of useful functions that manipulate strings, like strcpy(), strcat(), and strlen().

In the next article, we are going to learn more string functions. See you there.

LEAVE A REPLY

Please enter your comment!
Please enter your name here