CS161 Introduction to Computer Science Character Data.

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

Introduction to C Programming
I/O Streams as an Introduction to Objects and Classes
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.
Chapter 10.
1 9/15/06CS150 Introduction to Computer Science 1 Combined Assignments, Relational Operators, and the If Statement.
1 11/3/08CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
Characters. COMP104 Lecture 21 / Slide 2 Data Type: char * Constant declaration const char star = '*'; * Variable declaration char resp; * Variable assignment.
1 CS 201 String Debzani Deb. 2 Distinction Between Characters and Strings When using strcat, one may be tempted to supply a single character as one of.
1 10/29/07CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
計算機概論實習 Stream Stream: sequence of bytes Input: from device (keyboard, disk drive) to memory Output: from memory to device (monitor, printer,
1 Lecture 5: Input/Output (I) Introduction to Computer Science Spring 2006.
How to Program in C++ CHAPTER 3: INPUT & OUTPUT INSTRUCTOR: MOHAMMAD MOJADDAM.
1 10/25/06CS150 Introduction to Computer Science 1 Reading from and Writing to Files.
Chapter 7. 2 Objectives You should be able to describe: The string Class Character Manipulation Methods Exception Handling Input Data Validation Namespaces.
Introduction to C Programming
CS161 Topic #14 1 Today in CS161 Lecture #14 Practicing! Writing Programs to Practice Write a program that counts the number of vowels in a sentence, ended.
CHAPTER 8 CHARACTER AND STRINGS
Chapter 8 Streams and Files Lecture Notes Prepared By: Blaise W. Liffick, PhD Department of Computer Science Millersville University Millersville, PA
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT8: Characters and Strings CS2311 Computer Programming.
STREAMS AND FILES OVERVIEW.  Many programs are "data processing" applications  Read the input data  Perform sequence of operations on this data  Write.
You gotta be cool. Stream Stream Output Stream Input Unformatted I/O with read, gcount and write Stream Manipulators Stream Format States Stream Error.
Computer programming Lecture 4. Lecture 4: Outline Making Decisions [chap 6 – Kochan] –The if Statement –The if-else Construct –Logical Operators –Boolean.
Chapter 10. Characters, Strings and the string class Csc 125 Introduction to C++ Fall 2005.
Character Arrays Based on the original work by Dr. Roger deBry Version 1.0.
Data Types & I/O Streams Objectives Data Types Understand that integers form the underlying foundation for many data types. Introduce a few frequently.
Introduction to C++ Version 1.1. Topics C++ Structure Primitive Data Types I/O Casting Strings Control Flow.
Copyright © 2012 Pearson Education, Inc. Chapter 10: Characters, C- Strings, and More About the string Class.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 3: Input/Output.
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 10: Characters, Strings, and the string class.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 21 - C++ Stream Input/Output Basics Outline 21.1Introduction 21.2Streams Iostream Library.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 10: Characters, C- Strings, and More About.
CSC Programming for Science Lecture 8: Character Functions.
Chapter 15 Strings as Character Arrays
Today in CS161 Lecture #5 Learn about… Data types (char, int, float) Input and Output (cin, cout) Writing our First Program Write the Inches to MM Program.
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
More about strings in C++. String member functions The next three slides present information about functions that are members of the C++ string class.
Chapter Characters, Strings, and the string class 10.
Program Input and the Software Design Process ROBERT REAVES.
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
Input/Output in C++ C++ iostream.h instead of stdio.h Why change? –Input/output routines in iostream can be extended to new types declared by the user.
CS162 External Data Files 1 Today in CS162 External Files What is an external file? How do we save data in a file?
DCT1063 Programming 2 CHAPTER 3 STRINGS Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Streams and Files Problem Solving, Abstraction, and Design using.
13/15/2016CS150 Introduction to Computer Science 1 Summary  Assignment due on Wednesday, October 29,  Tutor will be in the lab on Tuesday evening,
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
Lecture 3 Expressions, Type Conversion, Math and String
Introduction Programs which manipulate character data don’t usually just deal with single characters, but instead with collections of them (e.g. words,
CS161 Introduction to Computer Science
The Selection Structure
Chapter 3: Expressions and Interactivity.
Characters, C-Strings, and More About the string Class
Copyright © 2012 Pearson Education, Inc.
Chapter 2 part #3 C++ Input / Output
Standard Input/Output Streams
Standard Input/Output Streams
Decision Making.
Today in CS161 Week #3 Learn about… Writing our First Program
Alternate Version of STARTING OUT WITH C++ 4th Edition
Basic Input and Output C++ programs can read and write information using streams A simple input stream accepts typed data from a keyboard A simple output.
10.1 Character Testing.
Chapter 3: Input/Output
Formatted Input, Output & File Input, Output
CS150 Introduction to Computer Science 1
Chapter 2 part #3 C++ Input / Output
ECE 103 Engineering Programming Chapter 18 Iteration
Input/Output Streams, Part 1
Presentation transcript:

CS161 Introduction to Computer Science Character Data

How do I read in Characters? Until now, we have read in character data using the following format: char ch; cin >> ch; This skips leading whitespace, –reads in 1 character and stores that character in variable ch

How do I read in Characters? An alternate form of reading in characters come in the form of the cin.get function: char ch;ch = cin.get(); //or cin.get(ch); this reads in the next character from the input buffer, even if that next character is whitespace!

How do I read in Characters? There are two different versions of the get function that read single characters. We can supply no arguments to this function and receive the next character in the input stream as the returned value. Or, we can supply a single character argument and receive the next character in the input stream through the argument (via call by reference).

How do I read in Characters? The advantage of using either of these functions is that we are able to read and store whitespace characters. If the next character in the input stream is a space, the character received will be a ' '. If the next character in the input stream is a newline, the character received will be a '\n', etc. Therefore, we can read and echo a single line of text exactly as the user types it in.

How do I read in Characters? The following two loops are equivalent and demonstrate the differences that we must adjust for when using these two versions of the get function: while ((ch = cin.get()) != '\n') cout << ch; while (cin.get(ch) && ch != '\n') //accomplishes the same cout << ch;

How do I read in Characters? For the most part, it is purely style which version of the get function we use. However, there are times when one version is more efficient than the other. For example, when no argument is supplied, the character read is the residual value and therefore can be used within a greater expression.

How do I read in Characters? This version works efficiently when we want to loop, flushing the input buffer until a newline is encountered: //flush buffer until newline while (cin.get() != '\n'); This is handy when there is a possibility of the user entering more characters than are expected It reads and "throws out" all additional characters up until (and including) the newline.

How do I read in Characters? On the other hand, the one argument version of the get function is useful when we want to detect whether or not the input operation has been successful or not. It returns a success or fail indication. It returns a non-zero value (i.e., an object of class istream) if the character is read correctly and a zero if the input operation fails or an end of file occurs.

How do I read in Characters? The following loop demonstrates this by reading characters (and echoing them) until end of file. char ch; //read until end of file (input fails) while (cin.get(ch)) cout <<ch;

Operations on Character Data With external libraries we can peform a number of operations on character data We can determine if a character is alphabetic, punctuation, a digit, upper case, or lower case using the ctype.h library All of the functions in the ctype.h library use call by value, which means none of the arguments can be modified by the functions

Operations on Character Data The ctype.h functions most useful are: isalpha(ch) which returns true (non-zero) if the value is either an upper or lowercase character and false (zero) otherwise. isdigit(ch) which returns true if the character represents one of the digits between 0-9 and false otherwise. ispunct(ch) which returns true if the character is any printable character, except a space, digit, or alphabetic and false otherwise. islower(ch) which returns true if the character is a lower case alphabetic character and false otherwise. isupper(ch) which returns true if the character is an upper case alphabetic character and false otherwise. Plus many more!

Operations on Character Data The ctype.h functions also include those we learned about earlier this term: tolower and toupper. It is important to realize that these functions are also pass by value –Which means it is impossible for tolower and toupper to modify the value of the argument; the character you send in as an argument remains unchanged Therefore, when using tolower and toupper, you must either –Use them directly in a logical expression as we learned earlier, or –Save the results in a variable: char result, ch; result = tolower(ch);