CSC 107 - Programming for Science Lecture 8: Character Functions.

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.
 2000 Prentice Hall, Inc. All rights reserved Fundamentals of Strings and Characters String declarations –Declare as a character array or a variable.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Characters and Strings.
CSC Programming for Science Lecture 5: Actual Programming.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
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.
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.
Topic 6A – The char Data Type. CISC 105 – Topic 6A Characters are Numbers The char data type is really just a small (8 bit) number. As such, each symbol.
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.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 6P. 1Winter Quarter I/O in C Lecture 6.
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.
Introduction to programming Language C, Data Types, Variables, Constants. Basics of C –Every program consists of one or more functions and must have main.
CSC 107 – Programming For Science. Today’s Goal  Learn C functions to input and output data  Using printf to print out variables and strings  Read.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
CHAPTER 8 CHARACTER AND STRINGS
CSC 107 – Programming For Science. Announcements  Textbook available from library’s closed reserve.
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.
STRING Dong-Chul Kim BioMeCIS UTA 10/7/
Computer programming Lecture 4. Lecture 4: Outline Making Decisions [chap 6 – Kochan] –The if Statement –The if-else Construct –Logical Operators –Boolean.
CSC Programming for Science Lecture 7: Math Functions.
1 Week 2: Variables and Assignment Statements READING: 1.4 – 1.6 EECS Introduction to Computing for the Physical Sciences.
Structure of a C program Preprocessor directive (header file) Program statement } Preprocessor directive Global variable declaration Comments Local variable.
CSC 107 – Programming For Science. Announcements  Memorization is not important, but…  … you will all still be responsible for information  Instead.
CSC 107 – Programming For Science. Announcements  Lectures may not cover all material from book  Material that is most difficult or challenging is focus.
Data Types & I/O Streams Objectives Data Types Understand that integers form the underlying foundation for many data types. Introduce a few frequently.
Characters. Character Data char data type – Represents one character – char literals indicated with ' '
CSC 107 – Programming For Science. The Week’s Goal.
CS140: Intro to CS An Overview of Programming in C (part 3) by Erin Chambers.
Arrays II (Strings). Data types in C Integer : int i; Double: double x; Float: float y; Character: char ch; char cha[10], chb[]={‘h’,’e’,’l’,’l’,’o’};
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI C-Style Strings Strings and String Functions Dale Roberts, Lecturer.
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.
CSC Programming for Science Lecture 10: Boolean Expressions & More If ­ Else Statements.
Chapter 7 C supports two fundamentally different kinds of numeric types: (a) integer types - whole numbers (1) signed (2) unsigned (b) floating types –
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
Agenda  Take up homework  Loops - Continued –For loops Structure / Example involving a for loop  Storing Characters in variables  Introduction to Functions.
Operating System Discussion Section. The Basics of C Reference: Lecture note 2 and 3 notes.html.
CTYPE.H Introduction  The ctype header is used for testing and converting characters.  A control character refers to a character that.
Characters and Strings
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
CSC Programming for Science Lecture 26: Arrays.
CPT: Chars/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to look at how C processes characters 4. Character.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
CSC Programming for Science Lecture 5: Actual Programming.
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
User Interaction and Variables
C Characters and Strings
Character Processing How characters can be treated as small integers?
Chapter 2 - Introduction to C Programming
Revision Lecture
The Selection Structure
Chapter 2 - Introduction to C Programming
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
Chapter 2 - Introduction to C Programming
Chapter 4 Managing Input and Output Operations
Character Processing How characters can be treated as small integers?
Chapter 2 - Introduction to C Programming
Introduction to C EECS May 2019.
EECE.2160 ECE Application Programming
C Characters and Strings
Boolean in C++ CSCE 121.
Presentation transcript:

CSC Programming for Science Lecture 8: Character Functions

Today’s Goal At the end of today’s lecture, you should know a number of character functions  These are useful when handling data files  They can also be used to make fun programs

Functions C includes many pre-defined functions  Just routines that do useful work  #include statement allow us to use functions Differs from what function means in math  printf & scanf do not return values  All of the math function do, however

#include Statements Listed at start of program  #include -- for math functions  #include -- for printf & scanf  #include -- for useful functions  #include -- for character functions

char Data Type Variables declared char are just integers  Uses standard (ASCII) which translates to characters ‘a’ has value of 97 ‘c’ has value of 100 ‘\n’ has value of 10 Can be used as numeric data type  Typically only handles values -128 to 128 Unfortunately EOF larger than 128

int putchar(int) Functions return value is not really used Prints a single character to the screen  Faster & easier than printf  int argument supports EOF and ‘\n’  Also enables passing a literal value putchar(‘\n’); putchar(97); is equivalent to putchar(‘a’); char val = 97; putchar(97 + 3);

int getchar() Function reads next character from input  Returns as int to support EOF  Normally illegal, but can assign result to char Better than scanf for reading character  Since spaces are characters, includes them  Slow to read actual data however

Converting Letters char toupper(char) – returns uppercase or (if not letter) original character toupper(‘a’)  ‘A’ toupper(‘A’)  ‘A’ toupper(‘7’)  ‘7’ char tolower(char) – returns lowercase or (if not letter) original character tolower(‘a’)  ‘a’ tolower(‘A’)  ‘a’ tolower(‘7’)  ‘7’

Converting Letters toupper & tolower do not change value stored in arguments char b, c, d, e, f, g; b = ‘a’; c = ‘D’; d = toupper(b); e = tolower(c); f = toupper(tolower(d)); g = tolower(c + 1);

Boolean Logic Named after 19 th century mathematician who created system  Forms basis of all computer work World consists of two values: true & false  C uses integers to represent these values  0 equals false in C  Any other value equals true

Boolean Character Functions int isdigit(char)Returns true if a decimal digit; otherwise, returns zero (false) int islower(char)Returns true if a lowercase letter; otherwise, returns zero (false) int isupper(char)Returns true if an uppercase letter; otherwise, returns zero (false) int isalpha(char)Returns true if a letter; otherwise, returns zero (false) int isalnum(char)Returns true if a letter or digit; otherwise, returns zero (false) Many more listed in book

Boolean Results on Gort char ch = ‘a’; char example = ‘D’; printf(“%d %d\n”, isalpha(ch), ispunct(example)); ch = example - 2; example = 48; printf(“%d %d\n”, isalpha(ch), isdigit(example)); ch = tolower(ch + 1); example = ‘\n’; printf(“%d %d\n”, isupper(ch), isspace(example)); Output

Your Turn Get into groups and complete daily activity

For Next Lecture Read Sections 3.1 – 3.3 of book  Do not need to understand all the details  But important knowing what is not understood Complete week #3 weekly assignment Complete lab #2 First programming assignment next week!