Strings in c++ language

Strings in c++ language

A string is a sequence of characters. C and C++ implement strings as arrays of char. The C++ language additionally supports string objects. In the C language, the only option is a char array. We use the term C string to refer to an array of characters as used in the C language. In this section, any mention of the term string refers to a C string.

Physical layout of a string AndroWep-Tutorials
Physical layout of a string AndroWep-Tutorials
std::cout << "Howdy!\n";

All proper C strings are null terminated. This means the last character in the array is ASCII zero, which C++ represents by the character literal ‘\0’. Figure 11.13 shows the physical layout of the string “Howdy!” in memory.

char *word = "Howdy!";
std::cout << word << '\n';
char word[256];
std::cin >> word;

String Input

char word[10];
fgets(word, 10, stdin);
std::cout << word << '\n';
char *word;
std::cin >> word;

The program’s behavor executing this code is undefined, but it likley will lead to the program crashing. Insidiously, depending on how the operating system manages memory, the program may run fine much of the time and crash only rarely. Regardless, the program contains a serious bug.

#include <iostream>

bool find_char(const char * s, char ch) {
    while ( * s != '\0') { // Scan until we see the null character
        if ( * s == ch)
            return true; // Found the matching character
        s++; // Advance to the next position within the string
    }
    return false; // Not found
}
int main() {
    const char * phrase = "this is a phrase";
    // Try all the characters a through z
    for (char ch = 'a'; ch <= 'z'; ch++) {
        std::cout << '\'' << ch << '\'' << " is ";
        if (!find_char(phrase, ch))
            std::cout << "NOT ";
        std::cout << "in " << '\"' << phrase << '\"' << '\n';
    }
}
'a' is in "this is a phrase"
'b' is NOT in "this is a phrase"
'c' is NOT in "this is a phrase"
'd' is NOT in "this is a phrase"
'e' is in "this is a phrase"
'f' is NOT in "this is a phrase"
'g' is NOT in "this is a phrase"
'h' is in "this is a phrase"
'i' is in "this is a phrase"
'j' is NOT in "this is a phrase"
'k' is NOT in "this is a phrase"
'l' is NOT in "this is a phrase"
'm' is NOT in "this is a phrase"
'n' is NOT in "this is a phrase"
'o' is NOT in "this is a phrase"
'p' is in "this is a phrase"
'q' is NOT in "this is a phrase"
'r' is in "this is a phrase"
's' is in "this is a phrase"
't' is in "this is a phrase"
'u' is NOT in "this is a phrase"
'v' is NOT in "this is a phrase"
'w' is NOT in "this is a phrase"
'x' is NOT in "this is a phrase"
'y' is NOT in "this is a phrase"
'z' is NOT in "this is a phrase"

Recall from Section 5.1 that for Boolean conditions C++ treats a zero value as false and any non-zero value as true. Because of such a loose interpretation of Boolean expressions, the find_char function above may be written more compactly as

char find_char(const char * s, char ch) {
    // Scan until we see the null character or the character
    // we seek
    while ( * s != '\0' && * s != ch)
        s++; // Advance to the next position within the string
    return *s; // Null character = false, any other is true
}