Presentation is loading. Please wait.

Presentation is loading. Please wait.

Friday, January 05, 2007 A few weeks of developing and testing can save a whole afternoon in the library. -Anonymous.

Similar presentations


Presentation on theme: "Friday, January 05, 2007 A few weeks of developing and testing can save a whole afternoon in the library. -Anonymous."— Presentation transcript:

1 Friday, January 05, 2007 A few weeks of developing and testing can save a whole afternoon in the library. -Anonymous

2 Practice Exercise! §Write down the code to multiply two matrices of sizes NxM and MxQ respectively.

3 Strings §One dimensional arrays is used to create character strings §In C++, a string is defined as a character array that is terminated by a null §A null is specified using ‘\0’ and is zero §Because of the null terminator, it is necessary to declare a character to be one character longer than the largest string it will hold.

4 Character Arrays char word[6] = “fruit”;//word has size 6 char list[6] = {‘f’,’r’,’u’,’i’,’t’, ‘\0’}; //list of characters, not a string Not necessary to add null terminator at end of string constants.

5 Character Arrays char word[] = “fruit”; //word has size 6

6 §What is wrong with the following? char name[5]="class"; int MyArray[];

7 §cout<<'3'; §cout<<','; §cout<<'\n';

8 §'a' and "a" are different

9 Null String "" Only contains null terminator.

10 Reading strings from keyboard §The easiest way to read a string entered from the keyboard is to make a character array int main() { char str[80]; cout << "Enter a string: "; cin >> str; // read string from keyboard cout << "Here is your string: "; cout << str <<"\n"; return 0; }

11 Reading strings from keyboard §There is a problem with previous program, if the string has whitespace characters // Using gets() to read a string from the keyboard. #include int main() { char str[80]; cout << "Enter a string: "; cout<<flush; gets(str); // read a string from the keyboard //NOTE: gets takes array_name as parameter cout << "Here is your string: "; cout << str<<“\n”; return 0; }

12 §When you send output to cout, it does not necessarily get printed immediately. Rather, it may wait in a buffer until some event, e.g. buffer full enough, reading from input etc. §Forcing all buffered output to actually be printed is known as "flushing" the stream. A flush can be forced by inserting flush into the stream, or inserting endl. cout << flush; cout << endl;

13 char str[]="This is CS192"; int i=0; while(str[i]){ cout<<str[i]<<endl; i++; }

14 ThisisCS192ThisisCS192

15 What if I insert a zero? char str[]="This is CS092"; int i=0; while(str[i]){ cout<<str[i]<<endl; i++; }

16 char str[80]; do{ gets(str); cout<<str<<endl; }while(str[0]);

17 String Library Functions strcpy() strcat() strlen() strcmp() #include strcpy(to, from) int main() { char str[80]; strcpy(str, "hello"); cout << str; return 0; }

18 String Library Functions strcpy() strcat() strlen() strcmp() #include strcpy(to, from) int main() { char str[80]; strcpy(str, "hello"); cout << str; return 0; } Output is: hello

19 String Library Functions (strcat) int main() { char s1[20], s2[10]; strcpy(s1, "hello"); strcpy(s2, " there"); strcat(s1, s2); cout << s1<< "\n"; return 0; } //output?

20 Output is: hello there

21 String Library Functions (strcmp) § strcmp(s1,s2) compares two strings and returns 0 if they are equal. If s1 is greater than s2 (dictionary order) then a positive number is returned; if it is less than s2 then a negative number is returned. cout<<strcmp("abc", "def")<<endl; Prints -1

22 String Library Functions (strcmp) bool password(); int main() { if(password()) cout << "Logged on.\n"; else cout << "Access denied.\n"; return 0; }

23 String Library Functions (strcmp) // Return true if password accepted; false otherwise. bool password(){ char s[80]; cout << "Enter password:\n"; gets(s); // strings differ if(strcmp(s, "stringfunction")) { cout << "Invalid password.\n"; return false; } // strings compared the same return true; }

24 String Library Functions (strcmp) int main() { char s[80]; for(;;) { cout << "Enter a string: "; cout<<flush; gets(s); if(!strcmp("quit", s)) break; } return 0; }

25 String Library Functions (strlen) strlen(s) returns the length of s (not counting null terminator) int main() { char str[80]; cout << "Enter a string\n"; gets(str); cout << "Length is: " << strlen(str)<<"\n"; return 0; } //what is the output if we enter: this is cs192


Download ppt "Friday, January 05, 2007 A few weeks of developing and testing can save a whole afternoon in the library. -Anonymous."

Similar presentations


Ads by Google