240-222 CPT: Chars/41 240-222 Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to look at how C processes characters 4. Character.

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 Systems Programming Concepts. Introduction to C A simple C Program A simple C Program –Variable Declarations –printf ( ) Compiling and.
Week 5 Part I Kyle Dewey. Overview Exam will be back Thursday New office hour More on functions File I/O Project #2.
C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
File Management in C. A file is a collection of related data that a computers treats as a single unit. File is a collection of data stored permanently.
1 Lecture 7  Fundamental data types in C  Data type conversion:  Automatic  Casting  Character processing  getchar()  putchar()  Macros on ctype.h.
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.
Engineering Computing I Chapter 1 – Part B A Tutorial Introduction continued.
Chapter 8 Characters and Strings Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Character Input and Output C and Data Structures Baojian Hua
1 Review of Class on Oct Outline of Chapter 4  How to write a function?  Function Prototypes  Function Invocation  Function Definition  The.
Character I/O. COMP104 Character I/O Slide 2 Data Type: char * Constant declaration const char star = '*'; * Variable declaration char resp; * Variable.
Fundamental data types
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
Imperative Programming Prof. Béat Hirsbrunner Amine Tafat, PhD Student Matthias Buchs and Raphaël Lesceux, Graduate Students Department of Informatics.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
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.
Prof. Béat Hirsbrunner Fulvio Frapolli, PhD Student (exercises) Bachelor students : - Major in computer science (first year, 2nd term) - Major in information.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 6P. 1Winter Quarter I/O in C Lecture 6.
Character Input and Output
A simple C program: Printing a line of text #include main() { printf(“hello, world\n”); } Program output: hello, world.
Console and File I/O - Basics Rudra Dutta CSC Spring 2007, Section 001.
C - Input & Output When we are saying Input that means to feed some data into program. This can be given in the form of file or from command line. C programming.
Chapter 18 I/O in C. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Standard C Library I/O commands.
C Programming A Modern Approach
CMPE13 Cyrus Bazeghi Chapter 18 I/O in C. CMPE Standard C Library I/O commands are not included as part of the C language. Instead, they are part.
Chapter 5: Data Input and Output Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
CHAPTER 8 CHARACTER AND STRINGS
Lecture 13. Outline Standard Input and Output Standard Input and Output (I/O)– Review & more Buffered/unbuffered input Character I/O Formatted I/O Redirecting.
CP104 Introduction to Programming File I/O Lecture 33 __ 1 File Input/Output Text file and binary files File Input/output File input / output functions.
BBS514 Structured Programming (Yapısal Programlama)1 Character Processing, Strings and Pointers,
Programming I Introduction Introduction The only way to learn a new programming language is by writing programs in it. The first program to.
Outline Symbolic Constants Character Input and Output if … else switch…case.
Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
C Programming Lecture 10 Instructor: Wen, Chih-Yu Department of Electrical Engineering National Chung Hsing University.
Chapter 18 I/O in C.
CS140: Intro to CS An Overview of Programming in C (part 3) by Erin Chambers.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 4.
CS115 FALL Senem KUMOVA-METİN1 The Fundamental Data Types CHAPTER 3.
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
Representing Strings and String I/O. Introduction A string is a sequence of characters and is treated as a single data item. A string constant, also termed.
Chapter 7 C supports two fundamentally different kinds of numeric types: (a) integer types - whole numbers (1) signed (2) unsigned (b) floating types –
Prof. Béat Hirsbrunner Ammar Halabi, PhD student (exercises) Dani Rotzetter, Master student (exercises) Bachelor students : Major in computer science (3rd.
1 Homework Done the reading? –K&R –Glass Chapters 1 and 2 Applied for cs240? (If not, keep at it!) Gotten a UNIX account? (If not, keep at it!)
CSC Programming for Science Lecture 8: Character Functions.
Program Development Cycle 1.Edit program 2.Compile program - translates it from C to machine language 3. Run/execute your program. 4. If not satisfied,
Sudeshna Sarkar, IIT Kharagpur 1 I/O in C + Misc Lecture –
CCSA 221 Programming in C INPUT AND OUTPUT OPERATIONS IN C – PART 1 1.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 2.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 6.
Dr. Sajib Datta Sep 8,  char type technically is an integer type  Computer uses numeric codes to represent characters, and store characters.
Basic concepts of C++ Presented by Prof. Satyajit De
Character Processing How characters can be treated as small integers?
INTRODUCTION Every language has some features that provides interaction between the program and the user of the program. C language uses the reference.
Revision Lecture
Unit-4, Chapter-2 Managing Input and Output Operations
Chapter 18 I/O in C.
Plan for the Day: I/O (beyond scanf and printf)
I/O in C + Misc Lecture Sudeshna Sarkar, IIT Kharagpur.
CSE1320 Files in C Dr. Sajib Datta
Structured Programming (Top Down Step Refinement)
Chapter 18 I/O in C.
Your questions from last session
Homework Applied for cs240? (If not, keep at it!) 8/10 Done with HW1?
Computer Programming Techniques Semester 1, 1998
Character Processing How characters can be treated as small integers?
C Characters and Strings
Chapter 18 I/O in C.
Presentation transcript:

CPT: Chars/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to look at how C processes characters 4. Character Processing

CPT: Chars/42 Overview 1.Character Variables 2.Printing a Character 3.Reading a Character 4.Two Short Examples 5.A Skeleton for Character I/O 6.Counting Words

CPT: Chars/43 1. Character Variables #include int main() { char c;/* declare a character var */ char c1 = 'A', c2 = 'b', c3 = '*'; c = '1'; return 0; }

CPT: Chars/44 2. Printing a Character 2.1.Using printf() 2.2.Printing ‘Funny’ Characters 2.3.Using putchar()

CPT: Chars/ Using printf() printf("%c", 'a'); printf("%c%c%c", 'A', 'B', 'C'); l A character can be treated as an integer: Character:'a' 'b' 'A' 'B' '1' '2' '&'... Integer: l printf("%d", 'a'); printf("%c", 97);

CPT: Chars/ Printing ‘Funny’ Characters Name of charWritten in CInteger value alert\a7 backspace\b8 carriage return\r13 tab\t9 newline\n10 single quote\'39 l printf("%c", '\a'); /* the machine will beep */

CPT: Chars/ Using putchar() p.873 #include int main() { putchar('S'); putchar('l'); putchar('o'); putchar('w'); putchar('.'); putchar('\n'); return 0; }

CPT: Chars/48 3. Reading a Character 3.1.Using getchar() 3.2. twice.c 3.3.Using twice.c

CPT: Chars/ Using getchar() p.873 /* Repeatedly input characters and print them twice on the screen */ #include int main() { char c; while(1) { /* always true */ c = getchar(); putchar(c); putchar(c); } return 0; }

CPT: Chars/ twice.c /* 2nd version */ #include int main() { int c; while( (c = getchar()) != EOF ) { putchar(c); putchar(c); } return 0; }

CPT: Chars/411 ((c = getchar()) != EOF) is not the same as: (c = (getchar() != EOF))

CPT: Chars/ Using twice.c % gcc -Wall -o twice twice.c % twice aabbccdd /* I typed abcd on the keyboard, followed by a carriage return and d */

CPT: Chars/413 % twice < input.txt aaccddee/* file contains acde */ % twice out.txt % twice > out.txt Other Forms of Input / Output

CPT: Chars/ Two Short Examples 4.1.Copy a File 4.2.Capitalize and Double

CPT: Chars/ Copy a File (ccopy.c) /* Copy from stdin to stdout a character at a time */ # include int main() { int c; while ((c = getchar()) != EOF) putchar(c); return 0; }

CPT: Chars/416 Compile and run % gcc -Wall -o ccopy ccopy.c % ccopy my_proj.txt

CPT: Chars/ Capitalize and Double (caps.c) l Read characters from stdin to stdout –capitalize lowercase letters –double newlines

CPT: Chars/418 #include int main() { int ch; while ((ch = getchar()) != EOF) { if ('a' <= ch && ch <= 'z') putchar(ch + 'A' - 'a'); else if (ch == '\n') { putchar('\n'); putchar('\n'); } else putchar(ch); } return 0; }

CPT: Chars/419 ASCII mapping from lower to upper 'a' = 97 'A' = 65 'b' = 98'B' = 66 : : 'z' = 122'Z' = 90

CPT: Chars/420 /* More portable version */ #include #include /* p.859/520 */ int main() { int ch; while ((ch = getchar()) != EOF) { if (islower(ch)) putchar(toupper(ch)); else if (ch == '\n') { putchar('\n'); putchar('\n'); } else putchar(ch); } return 0; }

CPT: Chars/421 /* Third version */ #include #include int main() { int ch; while ((ch = getchar()) != EOF) { if (ch == '\n') { putchar('\n'); putchar('\n'); } else putchar(toupper(ch)); } return 0; }

CPT: Chars/ A Skeleton for Character I/O /* some comments */ #include #include /* often required */ int process_char(int ch); int main() { int ch, new_ch; while ((ch = getchar()) != EOF) { new_ch = process_char(ch); putchar(new_ch); } return 0; } continued

CPT: Chars/423 int process_char(int ch) /* manipulate the char as an integer */ { /* do something to ch */ return /* an integer, representing a char */ ; }

CPT: Chars/ Counting Words /* Count the words read from stdin. */ #include #include int found_next_word(void); int main() { int word_cnt = 0; while (found_next_word() == 1) word_cnt++; printf("Num words = %d\n", word_cnt); return 0; } continued

CPT: Chars/425 int found_next_word(void) /* Skip spaces, then read the non-white space characters until white space is reached (or EOF). Return 1 if found non-white space characters, 0 otherwise. */ {.... } continued

CPT: Chars/426 int found_next_word(void) { int c; while (isspace( c = getchar() )) ; /* skip white space */ if (c != EOF) { /* found a word */ while ((c = getchar()) !=EOF && !isspace(c)) ; /* skip everything except EOF and white space */ return 1; } return 0; }