Character Processing How characters can be treated as small integers?

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.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Characters and Strings.
Character and String definitions, algorithms, library functions Characters and Strings.
Principles of Programming Fundamental of C Programming Language and Basic Input/Output Function 1.
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.
This presentation includes custom animations. To view the animations, you must view the presentation in Slide Show mode and activeX controls must be allowed.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
Chapter 8 Characters and Strings Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 8 - Characters and Strings Outline 8.1Introduction.
1 Lecture 2  Input-Process-Output  The Hello-world program  A Feet-to-inches program  Variables, expressions, assignments & initialization  printf()
1 Review of Class on Oct Outline of Chapter 4  How to write a function?  Function Prototypes  Function Invocation  Function Definition  The.
1 Key Concepts:  Why C?  Life Cycle Of a C program,  What is a computer program?  A program statement?  Basic parts of a C program,  Printf() function?
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
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.
1 Agenda Variables (Review) Example Input / Output Arithmetic Operations Casting Char as a Number (if time allow)
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
 2007 Pearson Education, Inc. All rights reserved C Characters and Strings.
First Program in C With Output. C Language Keywords.
 2007 Pearson Education, Inc. All rights reserved C Characters and Strings.
C Programming Lecture 3. The Three Stages of Compiling a Program b The preprocessor is invoked The source code is modified b The compiler itself is invoked.
EPSII 59:006 Spring Introduction Fundamentals of Strings and Characters Character Handling Library String Conversion Functions Standard Input/Output.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
CHAPTER 8 CHARACTER AND STRINGS
Input & Output: Console
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
February 14, 2005 Characters, Strings and the String Class.
BBS514 Structured Programming (Yapısal Programlama)1 Character Processing, Strings and Pointers,
C Programming Lecture 4 : Variables , Data Types
STRING Dong-Chul Kim BioMeCIS UTA 10/7/
12 주 강의 The preprocessor. The use of #include #include #include “ filename ” ::: current directory  system-dependent places #include ::: system dependent.
Constants in C A Presentation On Department of Computer & Information Technology, M.S.P.V.L. Polytechnic College, Pavoorchatram.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
Characters. Character Data char data type – Represents one character – char literals indicated with ' '
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.
Chapter-4 Managing input and Output operation.  Reading, processing and writing of data are three essential functions of a computer program.  Most programs.
Chapter 4 Literals, Variables and Constants. #Page2 4.1 Literals Any numeric literal starting with 0x specifies that the following is a hexadecimal value.
Agenda Character representation Numerical Conversions ASCII EBCDIC
Chapter 7 C supports two fundamentally different kinds of numeric types: (a) integer types - whole numbers (1) signed (2) unsigned (b) floating types –
C++ for Engineers and Scientists Second Edition Chapter 7 Completing the Basics.
Minimal standard C program int main(void) { return 0 ; }
CSC Programming for Science Lecture 8: Character Functions.
CTYPE.H Introduction  The ctype header is used for testing and converting characters.  A control character refers to a character that.
Characters and Strings
Sudeshna Sarkar, IIT Kharagpur 1 I/O in C + Misc Lecture –
CPT: Chars/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to look at how C processes characters 4. Character.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
BIL 104E Introduction to Scientific and Engineering Computing Lecture 2.
Numbers in ‘C’ Two general categories: Integers Floats
C Characters and Strings
INC 161 , CPE 100 Computer Programming
C Characters and Strings
Character Processing How characters can be treated as small integers?
Tokens in C Keywords Identifiers Constants
Computing Fundamentals
Introduction to C CSE 2031 Fall /3/ :33 AM.
I/O in C + Misc Lecture Sudeshna Sarkar, IIT Kharagpur.
Chapter 8 - Characters and Strings
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Decision Making.
Lectures on Numerical Methods
Program Breakdown, Variables, Types, Control Flow, and Input/Output
WEEK-2.
ECE 103 Engineering Programming Chapter 8 Data Types and Constants
Introduction to C EECS May 2019.
Lexical Elements & Operators
C Characters and Strings
Introduction to C CSE 2031 Fall /15/2019 8:26 AM.
Presentation transcript:

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 standard header files?

Data Type char Fundamental Data Type stored in one Byte. 28=256 different combos. Letters, Digits, Special Characters. A character constant is written between single quotes. char c = ‘s’;

In C a char has the integer value corresponding to the Binary Coding Scheme in effect (ASCII). See page 174. Letters: ’a’, ‘b’ ……….’z’ 97 98 ……….112 ‘A’, ‘B’ ………’Z’ 65 66 ………. 90 Digits: ‘0’, ‘1’ ……….. ‘9’ 48 49 57 Others: ‘&’ ‘+’ ……… 38 43

printf ( “%c”, ‘a’) ? printf ( “%d”, ‘a’) ? printf ( “%c”, 98) ? A “ %c” is used to designate the char format. The content of the Byte can be thought of as either a character or as a small integer. printf ( “%c”, ‘a’) ? printf ( “%d”, ‘a’) ? printf ( “%c”, 98) ?

Nonprinting and Hard-to-print Characters Escape sequence: (see pg. 176). ‘\n’ - new line, ‘\t’ - tab ‘\\’ - get backslash ‘\”’ - get double quote mark ‘\” - get single quote mark printf(“\“ABC\””); “ABC” printf(“\‘ABC\’”); ‘ABC’ printf(“ \n\t This is a test”);

I/O of char Returns the ORDINAL VALUE of the character read. getchar( ) reads a character from the keyboard. int  getchar(void); Returns the ORDINAL VALUE of the character read. putchar( ) writes a character to the screen. int  putchar(int c); Functional Prototype C is an Integer Valued Expression representing the code of the character to be output.

p.g. 178 char c; while (1){ c = getchar(); putchar(c);} How Terminate? ctrl+c ctrl+d(unix), ctrl+z(dos) int c; while ((c = getchar()) != EOF){ putchar(c);}

EOF stdio.h contains: #define EOF (-1) How interpret EOF? Negative integer stored in 2’s complement form. 1. Take Pos value (+1) 00000001 2. Reverse Bits 11111110 3. Add 1 +1 11111111

Capitalize Lowercase Letters p.g. 183 #include <stdio.h> #include <ctype.h> int c; while ((c = getchar()) != EOF){ if (islower(c)) putchar( toupper(c) ); }

# include <ctype.h> ctype.h Header file Contains macros & prototypes of functions that are often used when processing characters: macros - The C preprocessor recognizes lines of the source text that begin with ________? The macros in ctype.h are used to test chars & includes a set of FP’s of functions that are used to convert characters. # include <ctype.h> #

Use of Macros Improve I/O: # define READ(c) c = getchar( ) If (READ(c) == ‘x’) Expand to : If ((c = getchar( )) ==‘x’)

printf(“%d %d %d\n”, i, j, temp); 2) Define some often used operations: # define SWAP(val1, val2, temp) {temp = val1; \ val1=val2; \ val2 = temp;} int i = 4, j = 8, temp; SWAP( i, j, temp); printf(“%d %d %d\n”, i, j, temp);

Conditional Compilation Directives: # define IBMPC 1 # if IBMPC # include <IBM.h> # else # include <generic.h> # endif IF target machine is IBMPC use IBM specific routines else use machine independent routines Do what if not using IBMPC _________? Change 1 to Zero

Class Functions They examine a character and tell if it belongs to a given class: int is… (int testchar); General Prototype

iscntrl- The ASCII control chars are all the values below the space(32) and the delete char(127). True if one of these-false otherwise. isprint- Is printable, the comple ment of iscntrl. True if > 31 and < 127. isspace- Checks for whitespace(blank(32), tab(9), line feed(10), vertical tab(11), form feed(12), carriage return(13)).

isgraph- All ASCII chars > 32(space) and less than 127(delete) are considered graphic characters. isalnum- The alphabetic characters and the numeric digits are considered the alphanumeric set. ispunct- The graphic complement of the alphanumeric chars. If testchar is > 32 (space) and < 127 (delete) but NOT an alphanumeric, it returns true.

isalpha- The upper and lower case alphabetic characters. islower- Lower case letter. isupper- Upper case letters. isdigit- The decimal digits. isxdigit- Test for hexidecimal digits, (0..9, a..f, A..F).