Upload
nancy-clementine-bridges
View
215
Download
1
Embed Size (px)
Citation preview
February 14, 2005
Characters, Strings and theString Class
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<int>( ‘a’)
• To get the char corresponding to an integer (ascii code)
static_cast<char>( 97)
Two Char Functions
char intToChar( int n )
{
return static_cast<char>(n);
}
char charToInt( char c )
{
return static_cast<int>(c);
}
Automatic Conversion
• Since char is an integer data type, the + operator will automatically cast the answer to an int.
sizeof(‘A’) = 1andsizeof(‘A’+’A’)=4
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
Characters really are integers in C++
cout << (‘A’==65) << endl;
prints out true. (which is normally 1 in C++)
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.
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’,
Finding a digit in a string
char code[] = “hgdhdgfk3hagsdfkhgasdfh7ahsgdfkahsgdfhk";
char *tmp=code;
while (*tmp != '\0'){
if (isdigit(*tmp)) {cout << *tmp << endl;
}tmp++;
}
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
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?
cin.getline(firstName, 10)
The function getline is made exactly for this purpose. The second argument specifies the maximum length including the null terminator.
C-String Library Functions
• strlen
• strcat
char first[12] = "Jack";
char last[8] = " Widman";
strcat(first, last);
cout << first << endl;
• strcpy
• strncpy
• strcmp
• strstr
String/Numeric Conversions
• atoi - 3 + atoi(“7”)
• atol
• atof• itoa - cout << itoa(1256, number, 16) << endl;
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.
• #include <string> - need this header
• Simple code example
string firstName;
cout << “What is your first name?” << endl;
cin >> firstName;
cout << “Welcome “ << firstName << endl;
An easier way to concatenate
string first = “Sue”;
string last = “Jones”;
cout << first + “ “ + last << endl;
Array Access
You can still do this:
string companyName=“Microsoft”;
cout << companyName[0] << endl;
will print out ‘M’;
This Wednesday
More about the String class