Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 10 ctype.h, Exercises METU Dept. of Computer Eng. Summer 2002 Ceng230 - Section 01 Introduction To C Programming by Ahmet Sacan Wed July 31, 2002.

Similar presentations


Presentation on theme: "Lecture 10 ctype.h, Exercises METU Dept. of Computer Eng. Summer 2002 Ceng230 - Section 01 Introduction To C Programming by Ahmet Sacan Wed July 31, 2002."— Presentation transcript:

1 Lecture 10 ctype.h, Exercises METU Dept. of Computer Eng. Summer 2002 Ceng230 - Section 01 Introduction To C Programming by Ahmet Sacan Wed July 31, 2002

2 is a library which contains single character conversion/testing functions.

3 Character Testing. int isalnum(int c) -- True if c is alphanumeric. int isalpha(int c) -- True if c is a letter. int isascii(int c) -- True if c is ASCII. int iscntrl(int c) -- True if c is a control character. int isdigit(int c) -- True if c is a decimal digit int isgraph(int c) -- True if c is a graphical character. int islower(int c) -- True if c is a lowercase letter int isprint(int c) -- True if c is a printable character int ispunct (int c) -- True if c is a punctuation character. int isspace(int c) -- True if c is a space character (space, newline or a tab). int isupper(int c) -- True if c is an uppercase letter. int isxdigit(int c) -- True if c is a hexadecimal digit

4 Character Conversion int toascii(int c) -- Convert c to ASCII. int tolower(int c) -- Convert c to lowercase. int toupper(int c) -- Convert c to uppercase.

5 Kendin pishir kendin ye. int isalnum(int c) -- True if c is alphanumeric. int isalpha(int c) -- True if c is a letter. int isdigit(int c) -- True if c is a decimal digit int islower(int c) -- True if c is a lowercase letter int isspace(int c) -- True if c is a space character (space, newline or a tab). int isupper(int c) -- True if c is an uppercase letter. int tolower(int c) -- Convert c to lowercase. int toupper(int c) -- Convert c to uppercase.

6 Goal: Stripper Write a program that reads a line of string, removes all the non-alphabetic characters, and prints the new stripped form.

7 void main(){ char str[256]; ReadLine(str); StripNonAlphas(str); PrintLine(str); } Game Plan...

8 void ReadLine(char s[ ]){ fgets(s, 256, stdin); } void PrintLine(char s[ ]){ printf("%s", s); }

9 void StripNonAlphas(char * s){ int it, is; char t[256]; strcpy(t, s);/*#include */ for(it=0, is=0; it<strlen(t); it++){ if(isalpha(t[i])){/*include */ s[is] = t[it]; is++; } s[is] = '\0'; }

10 int a=1, b=2, *p, *q, **x, **y; void PrintVars(){ printf("%d %d %d %d %d %d\n", a, b, *p, *q, **x, **y); } void main(){ x = &p;y = &q;p = &a;q=&b; PrintVars(); *p = 3; *q = 4; **x = 5; **y = 6;PrintVars(); p = q; PrintVars(); q = &a;x = &(&a); y = (*(&x)); a = *(&b); PrintVars(); }

11 Goal: myGetLine function Write and test a function that reads a line from the user and stores it into a character array. –note, that, you've learnt how to do this using fgets(char *str, int len, FILE *stream) function. You are asked to write "a function that has the same function." –use "int getchar( )" function defined in getchar reads one character from the standard input, and returns the ascii value of this character. char c; c = getchar();

12 void myGetLine_1(char [ ] str){ char *p = str; while(1) { *p = getchar(); if(*p = '\n'){ *p = '\0'; break; } p++; }

13 void myGetLine_2(char [ ] str){ int i=0; char c; while(1) { c = getchar(); if(c = '\n'){ str[i] = '\0'; break; } str[i] = c; i++; }

14 void myGetLine_3(char [ ] str, int maxLen){ int i=0; char c; while(i<maxLen-1) { c = getchar(); if(c = '\n'){ str[i] = '\0'; return; } str[i] = c; } str[i] = '\0'; }

15 int a=0, b=1, c='c'-'a', x=0; if(a) if(b) x=1; else if(c) x=2; if(a) x=1; if(b) x=2; else if(c) x=3; if(b) x=1; else if(c) x=2; if(b) x=1; x = 2; else if(c) x=3; if(b--) x=1; else if(++a) x=2 if(--b) x=1; else if(++a) x=2;

16 int a=10,b=20, c=30, x=0,y=1,z=-1; a = c + (x ? 29*(b-3)-27*13+5 ? 0 ? 3 : 0 : a*b*c : z ? a+b+c : -1); printf("%d\n",a); a = x++ ? y-- : --z ? y+z : z*z; printf("%d\n", a);

17 int a=1; char x='a', y='b'; PrintVars(){ printf("%d %c %c\n", a, x, y); } void main(){ PrintVars(); x += 'A'-'a'; a = y + 'Z' - 'z' - x; PrintVars(); y = 3; x += y; a = y-x; PrintVars(); }

18 Goal: Insertion into Array Write a function that inserts a given number at a given position of an integer array.

19 Goal: Deletion from an Array Write a function that deletes the number at a given position of an integer array.

20


Download ppt "Lecture 10 ctype.h, Exercises METU Dept. of Computer Eng. Summer 2002 Ceng230 - Section 01 Introduction To C Programming by Ahmet Sacan Wed July 31, 2002."

Similar presentations


Ads by Google