Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C++: C-Strings #214

Open
Qingquan-Li opened this issue Jul 11, 2022 · 0 comments
Open

C++: C-Strings #214

Qingquan-Li opened this issue Jul 11, 2022 · 0 comments
Labels

Comments

@Qingquan-Li
Copy link
Owner

Qingquan-Li commented Jul 11, 2022

Concept: In C++, a C-string is a sequence of characters stored in consecutive memory locations, terminated by a null character.

1. String Objects and C-Strings

String is a generic term that describes any consecutive sequence of characters. A word,
sentence, a person's name, and the title of a song are all strings.

In the C++ language, there are two primary ways that strings are stored in memory: as string objects or as C-strings.

A C-string is a string whose characters are stored in consecutive memory locations and are followed by a null character, or null terminator. A null characteror or null terminator is a byte holding the ASCII code 0.
Strings that are stored this way are called C-strings because this is the way strings are handled in the C programming language.


2. String Literals

In C++, all string literals (Ex: "Hi", "Hello World!", "How are you?") are stored in memory as C-strings. In C++, string literals are enclosed in double quotation marks, such as:

"Hello World!"

How the string literal "Hello World!" is stored in memory, as a C-string:

H e l l o W o r l d ! \0

\0 ("slash zero") is the escape sequence representing the null terminator. It stands for the ASCII code 0.

The purpose of the null terminator is to mark the end of the C-string. Without it, there would be no way for a program to know the length of a C-string.

cout << "Hello World!";

It's important to realize that a string literal has its own storage location, just like a variable or an array. When a string literal appears in a statement, it's actually its memory address that C++ uses.
In this statement, the memory address of the string literal "Hello World!" is passed to the cout object. cout displays the consecutive characters found at this address. It stops displaying the characters when a null terminator is encountered.


3. C-Strings Stored in Arrays

The C programming language does not provide a string class like that which C++ provides. In the C language, all strings are treated as C-strings. When a C programmer wants to store a string in memory, he or she has to create a char array that is large enough to hold the string, plus one extra element for the null character.

You might be wondering why this should matter to anyone learning C++. You need to
know about C-strings for the following reasons:

  • The string class has not always existed in the C++ language. Several years ago, C++
    stored strings as C-strings. As a professional programmer, you might encounter older
    C++ code (known as legacy code) that uses C-strings.
  • Some of the C++ library functions work only with C-strings.
  • In the workplace, It is not unusual for C++ programmers to work with specialized
    libraries that are written in C. Any strings with which C libraries work will be C-strings.

Example:

const int SIZE = 21;
char name[SIZE];

The code defines a char array that has 21 elements, so it is big enough to hold a C-string that is no more that 20 characters long.

You can initialize a char array with a string literal, as shown here:

const int SIZE = 21;
char name[SIZE] = "Alice";

After this code executes, the name array will be created with 21 elements. The first six elements will be initialized with the characters 'A', 'l', 'i', 'c', 'e', and '\0'.
The null character is automatically added as the last character.

C-string input can be performed by the cin object.
For example the following code allows the users to enter a string (with no whitespace characters) into the name array.

const int SIZE = 21;
char name[SIZE];
cin >> name;

Example:

#include <iostream>
using namespace std;

int main() {
    const int SIZE = 5;
    // char ch_arr[SIZE];

    // ch_arr[0] = 'H';
    // ch_arr[1] = 'i';
    // ch_arr[2] = '\0';
    // char ch_arr[SIZE] = {'H', 'i', '\0'};
    // C-string:
    char ch_arr[SIZE] = "Hi";
    // In memory: "Hi" is: 'H' 'i' '\0'
    cout << ch_arr << endl;

    // for (int i = 0; ch_arr[i] != '\0'; i++)
    // '0' is false
    // for (int i = 0; ch_arr[i]; i++)
    //     cout << ch_arr[i];
    int i = 0;
    while (ch_arr[i] != '\0') {
        cout << ch_arr[i];
        i++;
    }
}

Output:

Hi
Hi

4. Library Functions for Working with C-Strings

Concept: The C++ library has numerous functions for handling C-strings. These functions perform various tests and manipulations, and require the <cstring> header file be included.

Because C-strings are stored in arrays, working with them is quite different from working with string objects. Fortunately, the C++ library provides many functios for manipulating and testing C-strings.

4.1 The strlen Function

Example01:

The following code segment uses the strlen function to determine the length of the string stored in the name array:

#include <cstring>

char name[] = "Thomas Edison";
int length;
length = strlen(name); // 13

The strlen function accepts a pointer to a C-string as its argument. It returns the length of the string, which is the number of characters up to, but not including, the null terminator.
As a result, the variable length will have the number 13 stored in it.
Remember, the only information being passed to strlen is the beginning address of a C-string. It doesn't know where the array ends, so it looks for the null terminator to indicate the end of the string.

When using a C-string-handling function, you must pass one or more C-string as arguments. This means passing the address of the C-string, which may be accomplished by using any of the following as arguments:

  • The name of the array holding the C-string
  • A pointer variable that holds the address of the C-string
  • A literal string

Anytime a literal string is used as an argument to a function, the address of the literal string is passed. Here is an example of the strlen function being used with such an argument:

length = strlen("Thomas Edison"); // 13

Example02:

#include <iostream>
#include <string>
#include <cstring>

using namespace std;

int main() {
    const int SIZE = 5;
    // C-string:
    char ch_arr[SIZE] = "Hi";
    string str = "Hello";

    cout << "The length of the C-string is " << strlen(ch_arr) << endl;
    cout << "The length of the string is " << str.length() << endl;

    return 0;
}

Output:

The length of the C-string is 2
The length of the string is 5

4.2 Some of the C-Sting Functions in <cstring>

Function Description
strlen Accepts a C-string or a pointer to a C-string as an argument. Returns the length of the C-string (not including the null terminator.)
Example Usage: len = strlen(name);
strcat Accepts two C-strings or pointers to two C-strings as arguments. The function appends the contents of the second string to the first C-string. (The first string is altered, the second string is left unchanged.)
Example Usage: strcat(string1, string2);
strcpy Accepts two C-strings or pointers to two C-strings as arguments. The function copies the second C-string to the first C-string. The second C-string is left unchanged.
Example Usage: strcpy(string1, string2);
strncat Accepts two C-strings or pointers to two C-strings, and an integer argument. The third argument, an integer, indicates the maximum number of characters to copy from the second C-string to the first C-string.
Example Usage: strncat(string1, string2, n);
strncpy Accepts two C-strings or pointers to two C-strings, and an integer argument. The third argument, an integer, indicates the maximum number of characters to copy from the second C-string to the first C-string. If n is less than the length of string2, the null terminator is not automatically appended to string1. If n is greater than the length of string2, string1 is padded with \0 characters.
Example Usage: strncpy(string1, string2, n);
strcmp Accepts two C-strings or pointers to two C-strings arguments. If string1 and string2 are the same, this function returns 0. If string2 is alphabetically greater than string1, it returns a negative number. If string2 is alphabetically less than string1, it returns a postive number.
Example Usage: if (strncmp(string1, string2))
strstr Accepts two C-strings or pointers to two C-strings arguments. Searched for the first occurence of string2 in string1. If an occurrence of string2 is found, the function returns a pointer to it. Otherwise, it returns nullptr (address 0).
Example Usage: cout << strstr(string1, string2);

Demonstrates use of the strcpy function:

#include <iostream>
#include <cstring>

class MyClass {
private:
    char ch[10];

public:
    // Getter function for ch
    char* getCh() {
        return ch;
    }

    // Setter function for ch
    void setCh(const char* newCh) {
        strcpy(ch, newCh);
    }
};

int main() {
    MyClass obj;

    // Set the value of ch
    obj.setCh("Hello");

    // Get the value of ch
    std::cout << obj.getCh() << std::endl;

    return 0;
}

Output:

Hello

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant