Download presentation
Presentation is loading. Please wait.
Published byScot Nelson Modified over 9 years ago
1
CSC 107 - Programming for Science Lecture 8: Character Functions
2
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
3
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
4
#include Statements Listed at start of program #include -- for math functions #include -- for printf & scanf #include -- for useful functions #include -- for character functions
5
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
6
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);
7
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
8
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’
9
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);
10
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
11
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
12
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 2 0 1 4 0 8
13
Your Turn Get into groups and complete daily activity
14
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!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.