1 / 17

Character String

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. . Character Strings

acolon
Download Presentation

Character String

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Character String

  2. 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

  3. 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:

  4. 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

  5. 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’

  6. 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

  7. 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

  8. 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.

  9. Character Strings void main() { char junk[4] = “abc”; cout << junk << endl; junk = “XYZ”; // Visual C++ compiler may give error cout << junk << endl; } Furthermore, ...

  10. 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?

  11. 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

  12. 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.

  13. 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

  14. 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.

  15. Character Strings - strings has many routines that operate sensibly upon strings, including - strlen(str) - strcmp (str1, str2) - strcpy (str1, str2)

  16. 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

  17. 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>

More Related