170 likes | 194 Views
Learn how character strings work in C++ programming, from literal strings to array declarations, initialization, and processing. Explore practical examples and common operations like strlen, strcmp, and strcpy.
E N D
Character String
Character Strings - Character strings are common - Found in the very first C++ program cout << “Hello World!” << endl; - All character strings seen thus far have been literal - Delimited by double quotes
H e l l o \0 Character Strings - You have seen single-dimensional arrays - Primarily arrays of integers or arrays of floats - A string is nothing more than an array of chars -“Hello!” consists of seven consecutive ASCII characters - Seven? - “Hello!” is stored as follows:
H e l l o \0 Character Strings - Six elements as you’d expect Character string terminated by the escape sequence ‘\0’ - Often referred to as the null character
Character Strings - Character strings can be defined as a variable - Similar to defining any other type of array seen so far char salutation[20]; - This declaration consumes 20 bytes of storage and could contain a character string with 19 readable characters - 19 characters plus the ‘\0’
Character Strings char salutation[20] = “Hello”; - Character strings can be initialized - This declaration still consumes 20 bytes of storage despite the perceived content - Only 6 elements of the 20 will contain meaningful information - The remaining 13 elements will contain garbage
Character Strings char salutation[20] = { ‘H’, ‘e’, ‘l’, ‘l’, ‘o’, ‘! ’, ‘\0’ }; - This is effectively the same declaration, but somewhat more “array-like” and certainly less convenient
Character Strings - Given a declaration such as char salutation[20]; - it is quite possible that you may do statements like the following: salutation = “Good Day!”; - We saw in integer/ float array that such a thing is not possible but this is not necessarily true for array of char.
Character Strings void main() { char junk[4] = “abc”; cout << junk << endl; junk = “XYZ”; // Visual C++ compiler may give error cout << junk << endl; } Furthermore, ...
Character Strings void test (char str[ ]); //------------------------------------------- void main() { // Don’t do this! char junk[4]= “abc”; cout << junk << endl; test (junk); cout << junk << endl; } //------------------------------------------- void test (char str[ ]) { str = “XYZ”; } < See: Example 3> Output is: abc abc Why?
Character Strings - Assuming char string1[20], string2[20]; - Never try to: – Assign strings string1 = string2; - Compare strings if (string1 = = string2) … - Basically, don’t perform aggregate character string assignment and comparison
Character Strings - C++ character strings are tricky - Sometimes they work like an aggregate type and sometimes they work just like any other array - A character string can be processed just like an array if you choose. This is shown in the next two examples.
Character Strings void test (char str[ ]); //------------------------------------ void main() { // OK, but awkward char junk[4]= “abc”; cout << junk << endl; test (junk); cout << junk << endl; } //------------------------------------ void test (char str[ ]) { str[0]= 'X'; str[1]= 'Y'; str[2]= 'Z'; } < See: Example 4> Output is what you would expect: abc XYZ
Character Strings int StrLength (const char str[ ]) { int i= 0; while (str[i] != ‘\0’) i++; return i; } - Count readable characters in string by processing elements of the array until ‘\0’ is encountered.
Character Strings - strings has many routines that operate sensibly upon strings, including - strlen(str) - strcmp (str1, str2) - strcpy (str1, str2)
Character Strings - strlen (str) - An integer function that returns the “current” length of str - Length does not include the ‘\0’ - strcpy (str1, str2) - An integer function that effectively copies str2 to str1
Character Strings - strcmp (str1, str2) - An integer function that compares two strings - If str1 = = str2, then strcmp returns 0 - If str1 < str2, then strcmp returns -1 - If str1 > str2, then strcmp returns 1 strcmp (“worm”, “worm”) returns 0 strcmp (“worm”, “caterpillar”) returns 1 strcmp (“caterpillar”, “worm”) returns -1 strcmp (“worm”, “Worm”) returns 1 strcmp (“Worm”, “worm”) returns -1 < See: Example 5>