lecture10_4on1 - CS1010E Lecture 10 Character Strings Two...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CS1010E Lecture 10 Character Strings Two Dimensional Arrays Henry Chia hchia@comp.nus.edu.sg Semester 1 2011 / 2012 Department of Computer Science School Of Computing National University Of Singapore CS1010E Lecture 10 – p.1/33 Lecture Outline Characters Character ordering Declaring variable and arrays Strings String definition String functions Two dimensional arrays Declaration and initialization Passing into functions Passing rows as 1D arrays Array of Pointers CS1010E Lecture 10 – p.2/33 Character Data This course focuses on only three primitives: int , double and char (character). A character value is enclosed in single quotes, such as ’A’ , ’b’ , and ’3’ . A character value (just like integer) is an integral type; can be used within a switch control expression. Each character value is mapped to a unique integer value following the ASCII table. There are 128 characters. Because characters and integers are alike, they can be arithmetically manipulated. CS1010E Lecture 10 – p.3/33 Character Ordering · · · digit-chars · · · uppercase-chars · · · lowercase-chars · · · digit-chars : ’0’ , ’1’ , ..., ’9’ . uppercase-chars : ’A’ , ’B’ , ..., ’Z’ . lowercase-chars : ’a’ , ’b’ , ..., ’z’ . Others: control characters and punctuations. Character arithmetic ’A’ +1 → ’B’ ; ’c’- ’a’ → 2 ; ’9’- ’0’ → 9 ( ’d’- ’a’ )+ ’A’ → ’D’ (i.e. case conversion) Character relations ’0’ < ’9’ < ’A’ < ’Z’ < ’a’ < ’z’ ’3’ negationslash = 3 (character digits negationslash = integer digits) CS1010E Lecture 10 – p.4/33 Using Characters Declaration (Single Variable/Array) Uninitialized: char ch, chArray[10] ; Initialized: char c= ’d’ ; char vowels[5]={ ’a’ , ’e’ , ’i’ , ’o’ , ’u’ }; Output printf( "The character is : %c\n" , c); Input scanf( "%c" , &ch); CS1010E Lecture 10 – p.5/33 Reading Characters int main( void ) { char ch; // Repeatedly reads uppercase characters scanf( "%c" , &ch); while (ch >= ’A’ && ch <= ’Z’ ) { ch = (ch - ’A’ ) + ’a’ ; printf( "lowercase: %c\n" , ch); scanf( "%c" , &ch); } return 0; } Will the above work? CS1010E Lecture 10 – p.6/33 Character Array Using an array as a lookup-table. 1 2 3 4 5 6 7 8 9 10 11 12 13 B A E H J L M N R U W X Y int main( void ) { int num; char c, chk[14]={ ’#’ , ’B’ , ’A’ , ’E’ , ’H’ , ’J’ , ’L’ , ’M’ , ’N’ , ’R’ , ’U’ , ’W’ , ’X’ , ’Y’ }; scanf( "%d" , &num); // input matric no. c = chk[13 - sumDigits(num)%13]; printf( "Check letter: %c\n" , c); return 0; } CS1010E Lecture 10 – p.7/33 Character Strings A character array is defined as an array in which the individual elements are stored as characters....
View Full Document

Page1 / 9

lecture10_4on1 - CS1010E Lecture 10 Character Strings Two...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online