C++

Using C Style Strings In C++

This tutorial will explain the basic C style strings/ characters that typically belong to the C language by later on are supported by C++.  This string is a container/ data type that contains its characters as an array. This array is a one-dimensional data type. This sort of array is terminated by a null character ‘\0’.  The functions applied on C or C++ are those functions that perform operations in the presence of a string library. This library provides many functions like strcat, strcopy, etc.

We will explain this concept on the Linux operating system, so you need to have Ubuntu installed and in the running form on your system. So you must install Virtual Box and, after downloading and installing, configure it. Now add the Ubuntu file to it. You can access Ubuntu’s official website and download the file according to your system requirement and operating system. It will take hours, then after installation, configure it on the virtual machine. In the configuration process, make sure you have created the user because it is essential for any operation on the Ubuntu terminal. Moreover, Ubuntu needs the authentication of the user before doing any installation.

We have used the 20.04 version of Ubuntu; you may use the latest one. For the implementation, you need to have a text editor and access the Linux terminal because we will be able to see the output of the source codes on the terminal through the query.

String

It is a very commonly used data type that is supplied by the library used in the programming language. It is a variable that contains a sequence of the letters or characters like space etc. Firstly, a string is declared, and then the value is given to it to initialize the string. To use the C programming language functions, we need a library <string> at the top of the source code or in a program. This library provides all the functions that are to be performed on a string. This string library should be included in a header file.

We have declared that the string or a character array has a terminating character at the end of the array. The string is declared and initialized by having the word ‘Aqsa’. We will now see how this name is held by an array having a null character. For instance, the name ‘Aqsa’ contains 4 letters, but the total words will be 5, including the terminating character.

# char name[5] = { 'A', 'q', 's', 'a', '\0'} ;

But according to the rule of initialization of array, we can write the above-mentioned statement as:

# char name[] = 'Aqsa';

There are many functions of strings that are supported by C++. Some of them are explained here:

  • strcpy(s1, s2): Its function is to Copy string s2 at the end of the first string s1.
  • strcat(s1, s2): It concatenates the string s2 onto the end of string s1.
  • strlen(s1): Its function is to Return the length of string s1.
  • strchr(s1, ch): Its function is to return the pointer to the character that has occurred the first time in the string.

We will explain each type later in the article. First, go to the basic example of the strings in C++.

Example 1

Use the input-output stream library. Inside the main program, we declare a character array with size [6]. As we have described earlier about the terminating character that is also used at the end of the array. Now display the value of the variable by using its name in the cout statement.

# char name[6] = {'A','Q', 'S', 'A', 'A'A, '\0'};

Write the code in the file and save it with the extension of C. To see the output of the file, compile the code and then execute it; for every C++ code, we need a compiler name G++. In the Linux operating system, the same compiler is used.

$ g++ -o str str.c

$ ./str

‘-o’ is used to save the resultant value of the source code.

Example 2

Concatenation is the process of joining two strings. This is a built-in feature of strings. But besides this, concatenation is also performed by directly adding two strings without having a function. For this purpose, first, use a string library.

# include <cstring>

Then in the main program, take two strings. Now to store its value, use the third string.

# Str3 = str1 + str2;

Add both the values inside the string and then store them in the third variable. Then take print of the last string.

From the resultant value, you can see that both the words/ strings that we have provided in the program are combined.

Example 3

This program contains the usage of three built-in functions of strings. For this purpose, first, you need three variables like the previous example. Assign values to two of them. Take another integer value to count the total length of the words. The first function is to copy string 1 into an empty character array str3. For this purpose, use the following strcpy features.

# Strcpy(str3, str1);

After that, the str3 string is displayed to check if the data is being copied. The next feature is to concatenate by using the built-in feature of strings ‘strcat’. Here we have used both the strings str1 and str2. Both these strings are used inside the parameter of the function. You do not need any third variable to store value this time. Now display the str1 string after combining both.

# Strcat( str1, str2);

After the concatenation process, we applied a feature to measure the total length of the first string after the concatenation process. For this purpose, use the length function having a single argument, string str1. The value is stored in the integer value

# Len = strlen(str1);

After that, print the value by using the ‘len’ variable. When we execute the code, and the resultant value is displayed on the terminal, you can see that str3 contains the same value as str1. Similarly, concatenation makes two strings combined. And at the end, the number of letters in the string after concatenation is displayed.

Example 4

The most commonly used feature of C++ is the ‘getline’ function. It takes a variable containing the value user has entered and ‘cin’ as arguments of a function.

# Getline (cin, a);

The variable is now displayed. Execute the code in the terminal; you will see that first, you are asked for the string to be entered. Then this same string is displayed in the next line.

Example 5

The next example is to match two strings. Take two strings. And then pass them as a parameter of the function.

# String result = strcmp (str1, str2);

This result is stored in a new variable.

When we execute the code, the answer will be 0 because both the strings are equal.

Conclusion

This article contains examples of almost all the basic features of strings in the Linux environment. Not all features of strings are built-in. You can also use manual functions, as we have explained. We hope this article will prove to be helpful for the users.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.