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

February 14, 2005 Characters, Strings and the String Class

Embed Size (px)

Citation preview

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

February 14, 2005

Characters, Strings and theString Class

Page 2: February 14, 2005 Characters, Strings and the String 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)

Page 3: February 14, 2005 Characters, Strings and the String Class

Two Char Functions

char intToChar( int n )

{

return static_cast<char>(n);

}

char charToInt( char c )

{

return static_cast<int>(c);

}

Page 4: February 14, 2005 Characters, Strings and the String Class

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

Page 5: February 14, 2005 Characters, Strings and the String Class

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

Page 6: February 14, 2005 Characters, Strings and the String Class

Characters really are integers in C++

cout << (‘A’==65) << endl;

prints out true. (which is normally 1 in C++)

Page 7: February 14, 2005 Characters, Strings and the String Class

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.

Page 8: February 14, 2005 Characters, Strings and the String Class

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’,

Page 9: February 14, 2005 Characters, Strings and the String Class

Finding a digit in a string

char code[] = “hgdhdgfk3hagsdfkhgasdfh7ahsgdfkahsgdfhk";

char *tmp=code;

while (*tmp != '\0'){

if (isdigit(*tmp)) {cout << *tmp << endl;

}tmp++;

}

Page 10: February 14, 2005 Characters, Strings and the String Class

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

Page 11: February 14, 2005 Characters, Strings and the String Class

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?

Page 12: February 14, 2005 Characters, Strings and the String Class

cin.getline(firstName, 10)

The function getline is made exactly for this purpose. The second argument specifies the maximum length including the null terminator.

Page 13: February 14, 2005 Characters, Strings and the String Class

C-String Library Functions

• strlen

• strcat

char first[12] = "Jack";

char last[8] = " Widman";

strcat(first, last);

cout << first << endl;

• strcpy

• strncpy

• strcmp

• strstr

Page 14: February 14, 2005 Characters, Strings and the String Class

String/Numeric Conversions

• atoi - 3 + atoi(“7”)

• atol

• atof• itoa - cout << itoa(1256, number, 16) << endl;

Page 15: February 14, 2005 Characters, Strings and the String Class

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.

Page 16: February 14, 2005 Characters, Strings and the String Class

• #include <string> - need this header

• Simple code example

string firstName;

cout << “What is your first name?” << endl;

cin >> firstName;

cout << “Welcome “ << firstName << endl;

Page 17: February 14, 2005 Characters, Strings and the String Class

An easier way to concatenate

string first = “Sue”;

string last = “Jones”;

cout << first + “ “ + last << endl;

Page 18: February 14, 2005 Characters, Strings and the String Class

Array Access

You can still do this:

string companyName=“Microsoft”;

cout << companyName[0] << endl;

will print out ‘M’;

Page 19: February 14, 2005 Characters, Strings and the String Class

This Wednesday

More about the String class