1 / 21

CS31: Introduction to Computer Science I

CS31: Introduction to Computer Science I. Discussion 1A 5/7/2010 Sungwon Yang swyang@cs.ucla.edu www.cs.ucla.edu/~swyang. Quick Review. What did we learn last week? Arrays create the same type of multiple variables at one time index numbers start from 0

Download Presentation

CS31: Introduction to Computer Science I

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS31: Introduction to Computer Science I Discussion 1A 5/7/2010 Sungwon Yang swyang@cs.ucla.edu www.cs.ucla.edu/~swyang

  2. Quick Review • What did we learn last week? • Arrays • create the same type of multiple variables at one time • index numbers start from 0 • each item of an array is a regular variable • array declaration • specify the length of the array • the number must be a constant positive integer • value itself or const type of int variable • initialization of an array • values enclosed in curly braces • number of values can be less than the length of an array • if omit the length of an array, it is determined based on the number of values • out-of-bound index • c++ compiler does not check the bound of index • accessing out-of-bound items does not cause a compile-error • be careful not to access out-of-bound items in loop • arrays in functions • can pass individual items either by value or reference • can pass the entire array • can be regarded as passing by reference • const keyword • protects the items of the array from accidentally being changed

  3. C-strings • C++ is enhanced from C Language • The C Language is a proper subset of C++ • C does not have string type • it has its own way of representing strings • we can still use the older way in C++ • called C-strings

  4. C-strings • Can recall what string is? • a sequence of zero or more characters • we have learned arrays last week • a sequence of variables of the type we define • a string can be represented by an array of characters!

  5. C-strings • let’s define an array of characters • char s[10]; • 10 character variables created. • Now, which strings can be stored in the array? • “” : empty string • “abcde” : the length is 5 • “abcdefghi” : the length is 9 • “abcdefghij” : the length is 10 “abcdefghijklmnopqrstuvwxyz” : the length is 26 (X) (X)

  6. zero byte/null character • C-string must end with a marker • the marker indicates “this is the end of the string” • without this marker, it is a character array, not a string • this marker is called zero byte or null character • ‘\0’ (ASCII number is 0) • the null character is automatically filled in the last slot

  7. \0 a a \0 b ? ? ? c ? ? ? 1 ? 2 ? ? ? ? 3 ? \0 ? ? ? ? ? ? ? 0 0 0 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5 6 6 6 7 7 7 8 8 8 9 9 9 C-strings C String Examples char st1[10] = ""; char st2[10] = "a"; char st3[10] = "abc 123";

  8. a a b b c c d d e e f \0 g h i \0 0 0 1 1 2 2 3 3 4 4 5 5 6 7 8 9 C-strings More C String Examples char st1[] = "abcde"; char st2[] = "abcdefghi";

  9. c-string output • can use cout << to print out • will print characters until it meets ‘\0’ int main () { char s[] = "Hello, how are you?"; cout << s <<endl; } int main () { char s[] = "Hello, how are you?"; s[5] = '\0'; cout << s <<endl; } Hello, how are you? Hello

  10. c-string output • cout works with character arrays • for other array types, it will print something different int main () { char s1[10]="abcde"; char s2[10]="12345"; char c[10]={'1' ,'2','3','4','5'}; int i[10]={1 , 2 , 3 , 4 , 5}; double d[10]={1.0 , 2.0 , 3.0 , 4.0 , 5.0}; bool b[10]={true,false,true,false,true}; cout << s1 << endl; cout << s2 << endl; cout << c << endl; cout << i << endl; cout << d << endl; cout << b << endl; } abcde 12345 12345 0028F810 0028F838 0028F88C

  11. C-string input • cin • read one word • cin.getline(C-string variable, max) • read up to (max number-1) of characters in one line • ‘\0’ is stored in the last Hi Sungwon Hi Sungwon int main () { char s1[20]; char s2[20]; cin >> s1; cin.ignore(10000,'\n'); cin.getline(s2,10); cout << s1 << endl; cout << s2 << endl; } Hi Hi Sungwo

  12. initialization vs. assignment int main () { char s[] = "Hello"; cout << s <<endl; } int main () { char s[10]; s = "Hello"; cout << s <<endl; } OK! Compile Error

  13. Quick question • what will be displayed? int main () { char s[10] = {'a','b','c','d','e','f','g','h','i'}; cout << s << endl; } int main () { char s[10] = {'a','b','c','d','e','f','g','h','i', 'j'}; cout << s << endl; } abcdefghi abcdefghijts잔O憬7

  14. More quick question • what will be displayed? int main () { char s[10]; s[0] = 'a'; s[1] = 'b'; s[2] = 'c'; s[3] = 'd'; s[4] = 'e'; cout << s << endl; } int main () { char s[10]; s[0] = 'a'; s[1] = 'b'; s[2] = 'c'; s[3] = 'd'; s[4] = 'e'; s[5] = '\0'; cout << s << endl; } abcde05"ts?5 abcde

  15. C-string to C++ strings • convert C-string into C++ string type • The C++ string operator = works • but, not vice versa int main () { char s[] = "How are you?"; string str = s; cout << str << endl; } int main () { string str = "How are you?"; char s[] = str; cout << s << endl; } How are you? Compile Error

  16. Functions for C-strings • basically, C-string is an array of characters • can manipulate characters with index number • There are pre-defined functions for C-strings • #include <cstring> required

  17. C-string libraries • strlen(s) • returns the length of s, not counting ‘\0’ int main () { char s[] = "How are you?"; cout << strlen(s) << endl; } 12

  18. C-string libraries • strcpy(t, s) • copies the string s to t • t = s does not work • does not check the array size • make sure there is enough space in t int main () { char s[10] = "Hello"; char t[10]; strcpy(t, s); cout << t << endl; } Hello

  19. C-string libraries • strcat(t, s) • concatenate (append) s to the end of t • does not check the array size • make sure there is enough space in t int main () { char s[10] = "Hello"; char t[20] = "Sungwon,"; strcat(t, s); cout << t << endl; } Sungwon,Hello

  20. C-string libraries • strcmp(t, s) • compare t and s • return 0 if they are equal • return something greater than 0 if t > s • return something less than 0 if t < s int main () { char s[10] = "abcde"; char t[10] = "abcde"; cout << strcmp(t,s) << endl; } int main () { char s[10] = "abcde"; char t[10] = "abced"; cout << strcmp(t,s) << endl; } int main () { char s[10] = "abcde"; char t[10] = "abc"; cout << strcmp(t,s) << endl; } 0 1 -1

  21. array of C-strings • A C string is essentially an array of characters. • an array of C strings is an array of arrays of characters. An array of arrays is simply a 2D array • char s[10][20]; • s[3] : can be used to refer to the string in position 3 • s[3][5] : can be used to refer to the letter in position 5 of the string in position 3. hello apple r int main () { char s[3][6]; strcpy(s[0], "hello"); strcpy(s[1], "apple"); strcpy(s[2], "world"); cout << s[0] << endl; cout << s[1] << endl; cout << s[2][2] << endl; }

More Related