> buffer; The system will copy characters from cin into buffer until a white space character is encountered. The user must ensure that buffer is defined to be a character string long enough to hold the input. The functions declared in the header file may be used to manipulate C-strings. These include the string length function strlen(), the string copying functions strcpy() and strncpy(), the string concatenating functions strcat() and strncat(), the string comparing functions strcmp() and strncmp(), and the token extracting function strtok()."> > buffer; The system will copy characters from cin into buffer until a white space character is encountered. The user must ensure that buffer is defined to be a character string long enough to hold the input. The functions declared in the header file may be used to manipulate C-strings. These include the string length function strlen(), the string copying functions strcpy() and strncpy(), the string concatenating functions strcat() and strncat(), the string comparing functions strcmp() and strncmp(), and the token extracting function strtok().">
Download presentation
Presentation is loading. Please wait.
1
C-Strings A C-string (also called a character string) is a sequence of contiguous characters in memory terminated by the NUL character '\0'. C-strings are accessed by variables of type char* (pointer to char). For example, if s has type char*, then cout << s << endl; will print all the characters stored in memory beginning at the address s and ending with the first occurrence of the NUL character. The C header file provides a wealth of special functions for manipulating C-strings. For example, the call strlen(s) will return the number of characters in the C-string s, not counting its terminating NUL character. These functions all declare their C-string parameters as pointers to char. So before we study these C-string operations, we need to review pointers.
2
REVIEW OF POINTERS A pointer is a memory address. For example, the following declarations define n to be an int with value 44 and pn to be a pointer containing the address of n: int n = 44; int* pn = &n; If we imagine memory to be a sequence of bytes with hexadecimal addresses, then we can picture n and pn as shown at right. This shows n stored at the address 64fddc and pn stored at the address 64fde0. The variable n contains value 44 and the variable pn contains the address value 64fddc. The value of pn is the address of n. This relationship is usually represented by a simpler diagram like the one shown at right below. This shows two rectangles, one labeled n and one labeled pn. The rectangles represent storage locations in memory. The variable pn points to the variable n. We can access n through the pointer pn by means of the dereference operator *. For example, the statement *pn = 77; would change the value of n to 77. We can have more than one pointer pointing to the same object: float* q = &x; Now *pn, *q, and x are all names for the same object whose address is 64fddc and whose current value is 77. This is shown in the diagram at right. Here, q is stored at the address 64fde4. The value stored in q is the address 64fddc of n.
3
C-STRINGS In C++, a C-string is an array of characters with the following important features: An extra component is appended to the end of the array, and its value is set to the NUL character '\0'. This means that the total number of characters in the array is always 1 more than the string length. The C-string may be initialized with a string literal, like this: char str[] = "Bjarne"; Note that this array has 7 elements: 'B', 'j', 'a', 'r', 'n', 'e', and '\0'. The entire C-string may be output as a single object, like this: cout << str; The system will copy characters from str to cout until the NUL character '\0' is encountered. The entire C-string may be input as a single object, like this: cin >> buffer; The system will copy characters from cin into buffer until a white space character is encountered. The user must ensure that buffer is defined to be a character string long enough to hold the input. The functions declared in the header file may be used to manipulate C-strings. These include the string length function strlen(), the string copying functions strcpy() and strncpy(), the string concatenating functions strcat() and strncat(), the string comparing functions strcmp() and strncmp(), and the token extracting function strtok().
4
EX 2 C-Strings Are Terminated with the NUL Character This little demo program shows that the NUL character '\0' is appended to the C-string: int main() { char s[] = "ABCD"; for (int i = 0; i < 5; i++) cout << "s[" << i << "] = '" << s[i] << "'\n"; } When the NUL character is sent to cout, nothing is printed—not even a blank. This is seen by printing one apostrophe immediately before the character and another apostrophe immediately after the character.
5
EX3 Ordinary Input and Output of C-Strings This program reads words into a 79-character buffer: int main() { char word[80]; do { cin >> word; if (*word) cout << "\t\"" << word << "\"\n"; } while (*word); } In this run, the while loop iterated 10 times: once for each word entered (including the Ctrl+Z that stopped the loop). Each word in the input stream cin is echoed to the output stream cout. Note that the output stream is not “flushed” until the input stream encounters the end of the line. Each C-string is printed with a double quotation mark " on each side. This character must be designated by the character pair \" inside a C-string literal. The expression *word controls the loop. It is the initial character in the C-string. It will be nonzero (i.e., “true”) as long as the C-string word contains a C-string of length greater than 0. The C-string of length 0, called the empty C-string, contains the NUL character '\0' in its first element. Entering Ctrl+Z+Enter+Entersends the end-of-file character in from cin. This loads the empty C-string into word, setting *word (which is the same as word[0]) to '\0' and stopping the loop. The last line of output shows only the Ctrl+Z echo, as ^Z. The Enter key may have to be pressed twice after Ctrl+Z is entered.
6
SOME cin MEMBER FUNCTIONS The input stream object cin includes the input functions: cin.getline(), cin.get() cin.ignore(), cin.putback(), and cin.peek(). Each of these function names includes the prefix “cin.” because they are “member functions” of the cin object. –The call cin.getline(str,n) reads up to n characters into str and ignores the rest. –The cin.get() function is used for reading input character-by- character. –The cin.putback() function restores the last character read by a cin.get() back to the input stream cin. –The cin.ignore() function reads past one or more characters in the input stream cin without processing them. –The cin.peek() function can be used in place of the combination cin.get() and cin.putback() functions.
7
EX8 The cin.putback() and cin.ignore() Functions int nextInt(); int main() { int m = nextInt(),n = nextInt(); cin.ignore(80,'\n'); // ignore rest of input line cout << m << " + " << n << " = " << m+n << endl; } int nextInt() { char ch; int n; while (cin.get(ch)) if (ch >= '0' && ch <= '9') // next character is a digit { cin.putback(ch); // put it back so it can be cin >> n; // read as a complete int break; } return n; } The nextInt() function scans past the characters in cin until it encounters the first digit. In this run, that digit is 3. Since this digit will be part of the first integer 305, it is put back into cin so that the complete integer 305 can be read into n and returned.
8
STANDARD C CHARACTER FUNCTIONS
10
STANDARD C STRING FUNCTIONS
12
The C header file, also called the C-String Library, includes a family of functions that are very useful for manipulating C- strings. EX13 The strlen() Function This program is a simple test driver for the strlen() function. The call strlen(s) simply returns the number of characters in s that precede the first occurrence of the NUL character '\0' #include int main() { char s[] = "ABCDEFG"; cout << "strlen(" << s << ") = " << strlen(s) << endl; cout << "strlen(\"\") = " << strlen("") << endl; char buffer[80]; cout > buffer; cout << "strlen(" << buffer << ") = " << strlen(buffer) << endl; }
13
EX14 The strchr(), strrchr(), and strstr() Functions #include int main() { char s[] = "The Mississippi is a long river."; cout << "s = \"" << s << "\"\n"; char* p = strchr(s,' '); cout << "strchr(s,' ') points to s[" << p - s << "].\n"; p = strchr(s,'s'); cout << "strchr(s,'s') points to s[" << p - s << "].\n"; p = strrchr(s,'s') ; cout << "strrchr(s,'s') points to s[" << p - s << "].\n"; p = strstr(s,"is") ; cout << "strstr(s,\"is\") points to s[" << p - s << "].\n"; p = strstr(s,"isi" ); if (p == NULL) cout << "strstr(s,\"isi\") returns NULL\n"; }
14
Excise Run all the example command of this lecture notes to check the results. Before you run it in the visual studio, please first debug it and try to find the output manually.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.