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.

Slides:



Advertisements
Similar presentations
Character Arrays (Single-Dimensional Arrays) A char data type is needed to hold a single character. To store a string we have to use a single-dimensional.
Advertisements

C Characters & Strings Character Review Character Handling Library Initialization String Conversion Functions String Handling Library Standard Input/Output.
Programming Functions: Passing Parameters by Reference.
Programming Searching Arrays. COMP102 Prog. Fundamentals: Searching Arrays/ Slide 2 Copyright © 2000 by Broks/Cole Publishing Company A division of International.
1 Objectives Understand streamed input and output.
1 Lecture 7  Fundamental data types in C  Data type conversion:  Automatic  Casting  Character processing  getchar()  putchar()  Macros on ctype.h.
If Statements & Relational Operators Programming.
While Loops Programming. COMP102 Prog Fundamentals I: while Loops/Slide 2 Shortcut Assignments l C++ has a set of shortcut operators for applying an operation.
Wednesday, 10/23/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 10/23/02  QUESTIONS??  Today:  Discussion of HW #3  The const modifier for functions.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
Programming Switch command. COMP102 Prog. Fundamentals: Switch command / Slide 2 Multiple Selection: The switch Statement value1 action 1 value2 action.
Announcements Quiz 1 Next Week. int : Integer Range of Typically -32,768 to 32,767 (machine and compiler dependent) float : Real Number (i.e., integer.
Character I/O. COMP104 Character I/O Slide 2 Data Type: char * Constant declaration const char star = '*'; * Variable declaration char resp; * Variable.
Characters. COMP104 Lecture 21 / Slide 2 Data Type: char * Constant declaration const char star = '*'; * Variable declaration char resp; * Variable assignment.
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.
Strings A special kind of array is an array of characters ending in the null character \0 called string arrays A string is declared as an array of characters.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
Summary of Loops Programming. COMP102 Prog Fundamentals I: Summary of Loops /Slide 2 Which Loop to Use? l for loop n for calculations that are repeated.
1 Character Processing How characters can be treated as small integers? How characters are stored and manipulated in a machine? How use is made of certain.
CS161 Introduction to Computer Science Character Data.
C++ Numerical Data Input/Output Programming. COMP 102 Prog Fundamentals I:C++ Numerical Data, Input/Output /Slide 2 Rules for Division l C++ treats integers.
1 Midterm Review COMP 102. Tips l Eat a light meal before the exam l NO electronic devices (including calculators, dictionaries, phones, pagers, etc.)
Chapter 7. 2 Objectives You should be able to describe: The string Class Character Manipulation Methods Exception Handling Input Data Validation Namespaces.
Loops Programming. COMP104 Lecture 9 / Slide 2 Shortcut Assignment l C++ has a set of operators for applying an operation to a variable and then storing.
Programming Introduction to C++.
VARIABLES, TYPES, INPUT/OUTPUT, ASSIGNMENT OPERATION Shieu-Hong Lin MATH/CS Department Chapel.
BBS514 Structured Programming (Yapısal Programlama)1 Character Processing, Strings and Pointers,
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT8: Characters and Strings CS2311 Computer Programming.
CSE 232: C++ Input/Output Manipulation Built-In and Simple User Defined Types in C++ int, long, short, char (signed, integer division) –unsigned versions.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
Introduction to C++ Programming COMP102 Prog. Fundamentals I:Introduction to C++ / Slide 2 Introduction to C++ l C is a programming language developed.
Data Types & I/O Streams Objectives Data Types Understand that integers form the underlying foundation for many data types. Introduce a few frequently.
1 FUNCTIONS - I Chapter 5. 2 What are functions ? Large programs can be modularized into sub programs which are smaller, accomplish a specific task and.
While Loops Programming. COMP102 Prog Fundamentals I: while Loops/Slide 2 Shortcut Assignments l C++ has a set of shortcut operators for applying an operation.
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) CSC 1201 Course at KSU1.
CS31: Introduction to Computer Science I Discussion 1A 4/16/2010 Sungwon Yang
Chapter 2: Introduction to C++. Language Elements Keywords Programmer-defined symbols (identifiers) Operators Punctuation Syntax Lines and Statements.
1 Cannon_Chapter9 Strings and the string Class. 2 Overview  Standards for Strings  String Declarations and Assignment  I/O with string Variables 
Chapter-4 Managing input and Output operation.  Reading, processing and writing of data are three essential functions of a computer program.  Most programs.
Programming Final Review COMP102 Prog. Fundamentals I: Character I/O/ Slide 2 Scope n The scope of an identifier does not apply if the same identifier.
CSC 270 – Survey of Programming Languages
CTYPE.H Introduction  The ctype header is used for testing and converting characters.  A control character refers to a character that.
Objects Variables and Constants. Our Scuba Problem #include // cin, cout, > using namespace std; int main() { const double FEET_PER_ATM = 33.0, LBS_PER_SQ_IN_PER_ATM.
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
Chapter 2 Creating a C++ Program. Elements of a C++ Program Four basic ways of structuring a program Four basic ways of structuring a program 1.Sequencing.
CPT: Chars/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to look at how C processes characters 4. Character.
CSE 251 Dr. Charles B. Owen Programming in C1 Strings and File I/O.
Chapter five exercises. a. false; b. true; c. false; d. true; e. true; f. true; g. true; h. false.
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
LESSON 2 Basic of C++.
Character Processing How characters can be treated as small integers?
CS31 Discussion 1E Jie(Jay) Wang Week5 Oct.27.
Unit-4, Chapter-2 Managing Input and Output Operations
Introduction to C++ October 2, 2017.
Chapter 12: Text Processing
Chapter 12: Text Processing
مبانی برنامه‌سازی با C++ جلسه دوم
اصول کامپیوتر ۱ مبانی کامپیوتر و برنامه‌سازی
10.1 Character Testing.
Standard Input/Output Stream
If Statements.
Character Processing How characters can be treated as small integers?
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Using string type variables
Std Library of C++.
ENERGY 211 / CME 211 Lecture 9 October 10, 2008.
Programming Strings.
C Characters and Strings
Presentation transcript:

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

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; }

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

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.

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.

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.

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;

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; }

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'

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; }

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; }

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.

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; }