DOC PREVIEW
USF CS 682 - Introduction to Programming II

This preview shows page 1-2-24-25 out of 25 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 25 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 25 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 25 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 25 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 25 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

{small lecturenumber - heblocknumber :} Strings in Caddtocounter {blocknumber}{1}{small lecturenumber - heblocknumber :} Strings as arraysaddtocounter {blocknumber}{1}{small lecturenumber - heblocknumber :} String libraryaddtocounter {blocknumber}{1}{small lecturenumber - heblocknumber :} strlenaddtocounter {blocknumber}{1}{small lecturenumber - heblocknumber :} Exerciseaddtocounter {blocknumber}{1}{small lecturenumber - heblocknumber :} strcpyaddtocounter {blocknumber}{1}{small lecturenumber - heblocknumber :} Exerciseaddtocounter {blocknumber}{1}{small lecturenumber - heblocknumber :} 'n' functionsaddtocounter {blocknumber}{1}{small lecturenumber - heblocknumber :} strcmpaddtocounter {blocknumber}{1}{small lecturenumber - heblocknumber :} Exampleaddtocounter {blocknumber}{1}{small lecturenumber - heblocknumber :} Exerciseaddtocounter {blocknumber}{1}{small lecturenumber - heblocknumber :} strcataddtocounter {blocknumber}{1}{small lecturenumber - heblocknumber :} Exampleaddtocounter {blocknumber}{1}{small lecturenumber - heblocknumber :} Exerciseaddtocounter {blocknumber}{1}{small lecturenumber - heblocknumber :} strdupaddtocounter {blocknumber}{1}{small lecturenumber - heblocknumber :} Exampleaddtocounter {blocknumber}{1}{small lecturenumber - heblocknumber :} Hints for stringsaddtocounter {blocknumber}{1}{small lecturenumber - heblocknumber :} Bitwise operationsaddtocounter {blocknumber}{1}{small lecturenumber - heblocknumber :} Bitwise operationsaddtocounter {blocknumber}{1}{small lecturenumber - heblocknumber :} Bitwise operationsaddtocounter {blocknumber}{1}{small lecturenumber - heblocknumber :} Bitwise ORaddtocounter {blocknumber}{1}{small lecturenumber - heblocknumber :} Bitwise ANDaddtocounter {blocknumber}{1}{small lecturenumber - heblocknumber :} Shiftsaddtocounter {blocknumber}{1}{small lecturenumber - heblocknumber :} Exampleaddtocounter {blocknumber}{1}Intro to Programming IIStrings in CChris BrooksDepartment of Computer ScienceUniversity of San FranciscoDepartment of Computer Science — University of San Francisco – p.1/??27-0: Strings in CRemember that C does not have a String data type.Instead, a string is just an array of characters.This means that we can access characters in a string justlike we do elements of an array.Department of Computer Science — University of San Francisco – p.2/??27-1: Strings as arraysSo to iterate through a string and convert all thecharacters to upper case, we could do:#include <ctype.h>int main(void) {char str[80];int i;scanf(‘‘%s’’, str);for (i = 0; i < 80; i++){str[i] = toupper(str[i]);}printf(‘‘%s’’,str);}Department of Computer Science — University of San Francisco – p.3/??27-2: String libraryC has a number of useful functions built into the string.hlibrary.strlenstrcpy/strncpystrcat/strncatstrdupstrcmp/strncmpDepartment of Computer Science — University of San Francisco – p.4/??27-3: strlenstrlen takes a string as input and returns an intrepresenting the length of the string.int countXs(char *instr) {int i, count = 0;for (i = 0; i < strlen(instr); i++) {if ( instr[i] == ’x’ || instr[i] == ’X’) {count++;}}}Department of Computer Science — University of San Francisco – p.5/??27-4: ExerciseWhat if strlen didn’t exist? How would we write it?Department of Computer Science — University of San Francisco – p.6/??27-5: strcpystrcpy takes two arguments: dest and src.strcpy makes a copy of src in dest.char in[80] = ‘‘hello world’’;char out[80];strcpy(out,in);printf(‘‘%s’’, out);Department of Computer Science — University of San Francisco – p.7/??27-6: ExerciseWhat if strcpy didn’t exist? How would we write it?Department of Computer Science — University of San Francisco – p.8/??27-7: ’n’ functionsMost string functions also have an ’n’ version.strncpyThis takes a third argument which represents the numberof characters to copystrcpy(out,in,20) will copy the first 20 characters of in intoout.Department of Computer Science — University of San Francisco – p.9/??27-8: strcmpstrcmp(s1,s2) compares two strings, s1 and s2.Semantics are the same as compareTo.s1 < s2: return < 0.s1 == s2: return 0s1 > s2: return > 0.Department of Computer Science — University of San Francisco – p.10/??27-9: Examplechar *name1, *name2;printf(‘‘enter name 1:’’);scanf(‘‘%s’’,name1);printf(‘‘enter name 2:’’);scanf(‘‘%s’’,name2);if (strcmp(name1,name2) < 0){printf(‘‘name 1 comes first.’’);} else if (strcmp(name1,name2) > 0) {printf(‘‘name 2 comes first.’’);} else {printf(‘‘equal.’’);}Department of Computer Science — University of San Francisco – p.11/??27-10: ExerciseWhat if strcmp didn’t exist? How would we write it?Department of Computer Science — University of San Francisco – p.12/??27-11: strcatstrcat(dest, src);strcat appends a copy of the characters in src to the stringdest.dest must have enough room for all of the characters insrc.Department of Computer Science — University of San Francisco – p.13/??27-12: Examplechar s1[80] = ‘‘world ‘‘;char s2[80] = ‘‘hello ’’;strcat(s2,s1);printf(’%s’’,s2);Department of Computer Science — University of San Francisco – p.14/??27-13: ExerciseWhat if strcat didn’t exist? How would we write it?Department of Computer Science — University of San Francisco – p.15/??27-14: strdupstrdup returns a pointer to a copy of the input string.These pointers refer to different memory locations.Department of Computer Science — University of San Francisco – p.16/??27-15: Examplechar *s1 = ‘‘Hello world’’;char *s2 = strdup(s1);s2[5] = ’Q’;printf(‘‘%s %s’’, s1, s2);Department of Computer Science — University of San Francisco – p.17/??27-16: Hints for stringsWhen processing a string character by character, becareful not to run off the end.When creating your own strings by hand, be sure to add anull character at the end.Use string.h when you can.Department of Computer Science — University of San Francisco – p.18/??27-17: Bitwise operationsLet’s say we need to keep track of whether 16 computersare on or off.How should we represent this?Department of Computer Science — University of San Francisco – p.19/??27-18: Bitwise operationsLet’s say we need to keep track of whether 16 computersare on or off.How should we represent this?Sixteen separate variables? ick.An array of 16 ints? Better, but wasteful.Let’s do it with a single


View Full Document

USF CS 682 - Introduction to Programming II

Documents in this Course
Load more
Download Introduction to Programming II
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Introduction to Programming II and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Introduction to Programming II 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?