Presentation is loading. Please wait.

Presentation is loading. Please wait.

February 14, 2005 Characters, Strings and the String Class.

Similar presentations


Presentation on theme: "February 14, 2005 Characters, Strings and the String Class."— Presentation transcript:

1 February 14, 2005 Characters, Strings and the String Class

2 The char datatype char is usually a one byte integer datatype To get the ascii code for a char, you can do this static_cast ( ‘a’) To get the char corresponding to an integer (ascii code) static_cast ( 97)

3 Two Char Functions char intToChar( int n ) { return static_cast (n); } char charToInt( char c ) { return static_cast (c); }

4 Automatic Conversion Since char is an integer data type, the + operator will automatically cast the answer to an int. sizeof(‘A’) = 1 and sizeof(‘A’+’A’)=4

5 charToInt simplified Since + casts a character to an int, we can simply add 0 to a char to get its numerical ascii equivalent. ‘A’+0 equals 65

6 Characters really are integers in C++ cout << (‘A’==65) << endl; prints out true. (which is normally 1 in C++)

7 But Remember Characters that represent numbers are not equal as numbers to the numbers they represent cout << ‘1’+0 << endl; Prints 49, not 1. What does 1 represent? Lets see.

8 Some char functions isalpha – Is a character a letter of the alphabet isalnum – Is a character a letter of the alphabet or a digit isdigit – Is a character a digit islower – Is a character lower case isupper – Is a character upper case isprint - is a character a printable character ispunct – is a character a printable character other than a digit, a letter or a space isspace – is a character a whitespace character. Whitespace includes any of the following: space ‘ ‘, newline ‘\n’, vertical tab ‘\v’, tab ‘\t’,

9 Finding a digit in a string char code[] = “hgdhdgfk3hagsdfkhgasdfh7ahsgdfkahsgdfhk"; char *tmp=code; while (*tmp != '\0') { if (isdigit(*tmp)) { cout << *tmp << endl; } tmp++; }

10 C-Strings in C++ A C-string is an array of characters where the final character is ‘\0’ which equals NULL which equals 0. These three characters all have ascii value 0. A ‘string literal’ is a literal representation of a string in a program. In C++, string literals are enclosed in double quotes. The terminating ‘\0’ is automatically appended

11 cin and strings Suppose you declare a string like this: char firstName[10]; And then use cin to allow the user to type in their first name like this. cin >> firstName Since firstName is just a pointer to the memory location of the first letter, cin has no way of knowing how big the string is. It will assign letters to any number of locations. If a name has more than ten letters, cin will go beyond the ‘\0’ of firstName and continue stuffing letters in memory locations that are not under strict control of the program. What to do?

12 cin.getline(firstName, 10) The function getline is made exactly for this purpose. The second argument specifies the maximum length including the null terminator.

13 C-String Library Functions strlen strcat char first[12] = "Jack"; char last[8] = " Widman"; strcat(first, last); cout << first << endl; strcpy strncpy strcmp strstr

14 String/Numeric Conversions atoi - 3 + atoi(“7”) atol atof itoa - cout << itoa(1256, number, 16) << endl;

15 The C++ String Class Much legacy code (that is, code that is already written and you are modifying or debugging or extending it ) uses C-strings. That is why we are learning them. For new code, however, the C++ String class is vastly superior.

16 #include - need this header Simple code example string firstName; cout << “What is your first name?” << endl; cin >> firstName; cout << “Welcome “ << firstName << endl;

17 An easier way to concatenate string first = “Sue”; string last = “Jones”; cout << first + “ “ + last << endl;

18 Array Access You can still do this: string companyName=“Microsoft”; cout << companyName[0] << endl; will print out ‘M’;

19 This Wednesday More about the String class


Download ppt "February 14, 2005 Characters, Strings and the String Class."

Similar presentations


Ads by Google