Presentation is loading. Please wait.

Presentation is loading. Please wait.

Sahar Mosleh California State University San MarcosPage 1 Character String.

Similar presentations


Presentation on theme: "Sahar Mosleh California State University San MarcosPage 1 Character String."— Presentation transcript:

1 Sahar Mosleh California State University San MarcosPage 1 Character String

2 Sahar Mosleh California State University San MarcosPage 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 Sahar Mosleh California State University San MarcosPage 3 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: Helo\0l

4 Sahar Mosleh California State University San MarcosPage 4 Character Strings - Six elements as you’d expect Character string terminated by the escape sequence ‘\0’ - Often referred to as the null character Helo\0l

5 Sahar Mosleh California State University San MarcosPage 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 Sahar Mosleh California State University San MarcosPage 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 Sahar Mosleh California State University San MarcosPage 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 Sahar Mosleh California State University San MarcosPage 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 Sahar Mosleh California State University San MarcosPage 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 Sahar Mosleh California State University San MarcosPage 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”; } Output is: abc Why?

11 Sahar Mosleh California State University San MarcosPage 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 Sahar Mosleh California State University San MarcosPage 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 Sahar Mosleh California State University San MarcosPage 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'; } Output is what you would expect: abc XYZ

14 Sahar Mosleh California State University San MarcosPage 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 Sahar Mosleh California State University San MarcosPage 15 Character Strings - strings has many routines that operate sensibly upon strings, including - strlen(str) - strcmp (str1, str2) - strcpy (str1, str2)

16 Sahar Mosleh California State University San MarcosPage 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 Sahar Mosleh California State University San MarcosPage 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”) returns1 strcmp (“caterpillar”, “worm”) returns -1 strcmp (“worm”, “Worm”) returns1 strcmp (“Worm”, “worm”) returns -1


Download ppt "Sahar Mosleh California State University San MarcosPage 1 Character String."

Similar presentations


Ads by Google