> character; cout << "As int " << (int)(character) << endl ; cout << "As char " << (char)(character) << endl; cout << "As bool "; if ((bool)(character)) cout << "true\n"; else cout << "false\n"; return 0; }"> > character; cout << "As int " << (int)(character) << endl ; cout << "As char " << (char)(character) << endl; cout << "As bool "; if ((bool)(character)) cout << "true\n"; else cout << "false\n"; return 0; }">

Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming Character I/O. COMP102 Prog. Fundamentals I: Character I/O/ Slide 2 More on char Type l Constant Declaration: const char star = '*'; l Variable.

Similar presentations


Presentation on theme: "Programming Character I/O. COMP102 Prog. Fundamentals I: Character I/O/ Slide 2 More on char Type l Constant Declaration: const char star = '*'; l Variable."— Presentation transcript:

1 Programming Character I/O

2 COMP102 Prog. Fundamentals I: Character I/O/ Slide 2 More on char Type l Constant Declaration: const char star = '*'; l Variable Declaration: char letter; l Variable Assignment : letter = 'A'; l Input/Output: char t, x, y, z; cin >> t >> x >> y >> z; INPUT: 1X3Y t = '1'; x = 'X'; y = '3'; z = 'Y'; cout << t << x << y << z << endl; OUTPUT: 1X3Y

3 COMP102 Prog. Fundamentals I: Character I/O/ Slide 3 Example 1 #include using namespace std; int main() { char character; cout << "Please enter a character!\n"; cin >> character; cout << "As int " << (int)(character) << endl ; cout << "As char " << (char)(character) << endl; cout << "As bool "; if ((bool)(character)) cout << "true\n"; else cout << "false\n"; return 0; }

4 COMP102 Prog. Fundamentals I: Character I/O/ Slide 4 Classifications of the character type characters control printable space graphical alphanumeric punctuation alphabetic digit upper lower

5 COMP102 Prog. Fundamentals I: Character I/O/ Slide 5 Some Functions on char from ctype.h Classifying Functions: isupper(c) returns nonzero if c is an uppercase letter. islower(c) returns nonzero if c is a lowercase letter. isdigit(c) returns nonzero if c is a digit character.

6 COMP102 Prog. Fundamentals I: Character I/O/ Slide 6 More Functions on char from ctype.h Classifying Functions: isalpha(c) returns nonzero if either islower(c) or isupper(c) returns nonzero. isalnum(c): returns nonzero if either isalpha(c) or isdigit(c) returns nonzero. isspace(c): returns nonzero if c is a space, newline, formfeed, carriage return, tab or vertical tab character.

7 COMP102 Prog. Fundamentals I: Character I/O/ Slide 7 More Functions on char from ctype.h Conversion Functions: tolower(c): if c is uppercase, returns lowercase; otherwise, returns c. toupper(c): if c is lowercase, returns uppercase; otherwise, returns c.

8 COMP102 Prog. Fundamentals I: Character I/O/ Slide 8 Example 2: About ctype.h #include using namespace std; int main() { char character ; cout << "Please enter a character!\n"; cin >> character; cout << "As int " << (int)(character) << endl ; cout << "As char " << (char)(character) << endl;

9 COMP102 Prog. Fundamentals I: Character I/O/ Slide 9 Example 2: About ctype.h cout << "Is upper case: "; cout << isupper(character) ; cout << "Is lower case: "; cout << islower (character) ; cout << "Is digit: "; cout << isdigit(character) ; cout << "Is alphabetic: "; cout << isalpha(character) ; cout << "Is alphanumeric (alphabetic or digit): "; cout << isalnum(character) ; cout << "Is space : "; cout << isspace(character) ; return 0; }

10 COMP102 Prog. Fundamentals I: Character I/O/ Slide 10 Relational Operators and the Type char '0' through '9' have ASCII code values 48 through 57 '0' < '1' <... < '9' 'A' through 'Z' have ASCII code values 65 through 90 'A' < 'B' <...< 'Z' 'a' through 'z' have ASCII code values 97 through 122 'a' < 'b' <...< 'z'

11 COMP102 Prog. Fundamentals I: Character I/O/ Slide 11 Example 3 // Our own version of islower #include using namespace std; bool islower(char character) { if (character >= 'a' && character <= 'z') return true; else return false; }

12 COMP102 Prog. Fundamentals I: Character I/O/ Slide 12 Example 3 (cont. ) int main() { //test islower(character) char character; cout << "Please enter a character! "; cin >> character; if (islower(character)) cout << "\'" << character << "\'" << " " << "is a lower case letter" << endl; else cout << "\'" << character << "\'" << " " << "is not a lower case letter" << endl; return 0; }

13 COMP102 Prog. Fundamentals I: Character I/O/ Slide 13 Reading/Writing One Character at a Time get(char& character) reads a single character named character from the input stream. put(char character) writes a single character named character to the output stream.

14 COMP102 Prog. Fundamentals I: Character I/O/ Slide 14 Example 4: get(c) and put(c) #include using namespace std; int main() { char character; cout << "Please enter some names "; cout << "(0 to stop) \n"; do{ cin.get(character); cout.put(character); }while (character != '0'); return 0; }


Download ppt "Programming Character I/O. COMP102 Prog. Fundamentals I: Character I/O/ Slide 2 More on char Type l Constant Declaration: const char star = '*'; l Variable."

Similar presentations


Ads by Google