Presentation is loading. Please wait.

Presentation is loading. Please wait.

PHY 107 – Programming For Science. Today’s Goal  Understand what cStrings are  Relationship to arrays & primitives  What it is and why null character.

Similar presentations


Presentation on theme: "PHY 107 – Programming For Science. Today’s Goal  Understand what cStrings are  Relationship to arrays & primitives  What it is and why null character."— Presentation transcript:

1 PHY 107 – Programming For Science

2 Today’s Goal  Understand what cStrings are  Relationship to arrays & primitives  What it is and why null character important  Discuss built-in cString functions: what they are & do

3 Today’s Goal  Understand what cStrings are  Relationship to arrays & primitives  What it is and why null character important  Discuss built-in cString functions: what they are & do  Unlike “string theory”, cStrings practical & useful  More than 10 people understand cStrings, also

4 Strings

5 What We Want  Human beings generally like using words & text  “Oh, boy! Numbers are cool” rarely, if ever, said nobody  Absolutely nobody prefers using 0 s & 1 s  Happy when using our language and not a computers

6 cStrings  cString  cString is special name for text-based data  Already been using cString literals  Text in quotation marks C/C++ treats as cString literal “Hi, Mom” “Hello world” “Good morning, Prof. Hertz. You are the greatest.” “Yes, the ‘Skins are the best football team.” “Well, at least the Yankees choked & massively failed.” “Wake up, Matthew. You were dreaming again”

7 Strings

8 It Is Like String Theory!

9 Using a cString  Remember: cString simply array of characters  Any legal use of char array okay for cString char bob[4] = “Bob”; char longStr[20] = “This is very long.”; char reallyLong[10000] = “Dummy”; char critics[] = “Statler & Waldorf”; char number[4] = “Four”; char bad[2] = “Will this compile?”; char hello[6] = {‘H’,‘e’,‘l’,‘l’,‘o’,‘\0’}; longStr = “Seventy-five”; bob = “o”; reallyLong[0] = ‘B’; bob[3] = ‘a’;

10 Using a cString  Remember: cString simply array of characters  Any legal use of char array okay for cString char bob[4] = “Bob”; char longStr[20] = “This is very long.”; char reallyLong[10000] = “Dummy”; char critics[] = “Statler & Waldorf”; char number[4] = “Four”; char bad[2] = “Will this compile?”; char hello[6] = {‘H’,‘e’,‘l’,‘l’,‘o’,‘\0’}; longStr = “Seventy-five”; bob = “o”; reallyLong[0] = ‘B’; bob[3] = ‘a’;

11 Using a cString  Remember: cString simply array of characters  Any legal use of char array okay for cString char bob[4] = “Bob”; char longStr[20] = “This is very long.”; char reallyLong[10000] = “Dummy”; char critics[] = “Statler & Waldorf”; char number[4] = “Four”; char bad[2] = “Will this compile?”; char hello[6] = {‘H’,‘e’,‘l’,‘l’,‘o’,‘\0’}; longStr = “Seventy-five”; bob = “o”; reallyLong[0] = ‘B’; bob[3] = ‘a’;

12 Using a cString  Remember: cString simply array of characters  Any legal use of char array okay for cString char bob[4] = “Bob”; char longStr[20] = “This is very long.”; char reallyLong[10000] = “Dummy”; char critics[] = “Statler & Waldorf”; char number[4] = “Four”; char bad[2] = “Will this compile?”; char hello[6] = {‘H’,‘e’,‘l’,‘l’,‘o’,‘\0’}; longStr = “Seventy-five”; bob = “o”; reallyLong[0] = ‘B’; bob[3] = ‘a’;

13 Using a cString  Remember: cString simply array of characters  Any legal use of char array okay for cString char bob[4] = “Bob”; char longStr[20] = “This is very long.”; char reallyLong[10000] = “Dummy”; char critics[] = “Statler & Waldorf”; char number[4] = “Four”; char bad[2] = “Will this compile?”; char hello[6] = {‘H’,‘e’,‘l’,‘l’,‘o’,‘\0’}; longStr = “Seventy-five”; bob = “o”; reallyLong[0] = ‘B’; bob[3] = ‘a’;

14 Using a cString  Remember: cString simply array of characters  Any legal use of char array okay for cString char bob[4] = “Bob”; char longStr[20] = “This is very long.”; char reallyLong[10000] = “Dummy”; char critics[] = “Statler & Waldorf”; char number[4] = “Four”; char bad[2] = “Will this compile?”; char hello[6] = {‘H’,‘e’,‘l’,‘l’,‘o’,‘\0’}; longStr = “Seventy-five”; bob = “o”; reallyLong[0] = ‘B’; bob[3] = ‘a’;

15 Using a cString  Remember: cString simply array of characters  Any legal use of char array okay for cString char bob[4] = “Bob”; char longStr[20] = “This is very long.”; char reallyLong[10000] = “Dummy”; char critics[] = “Statler & Waldorf”; char number[4] = “Four”; char bad[2] = “Will this compile?”; char hello[6] = {‘H’,‘e’,‘l’,‘l’,‘o’,‘\0’}; longStr = “Seventy-five”; bob = “o”; reallyLong[0] = ‘B’; bob[3] = ‘a’;

16 Using a cString  Remember: cString simply array of characters  Any legal use of char array okay for cString char bob[4] = “Bob”; char longStr[20] = “This is very long.”; char reallyLong[10000] = “Dummy”; char critics[] = “Statler & Waldorf”; char number[4] = “Four”; char bad[2] = “Will this compile?”; char hello[6] = {‘H’,‘e’,‘l’,‘l’,‘o’,‘\0’}; longStr = “Seventy-five”; bob = “o”; reallyLong[0] = ‘B’; bob[3] = ‘a’;

17 Using a cString  Remember: cString simply array of characters  Any legal use of char array okay for cString char bob[4] = “Bob”; char longStr[20] = “This is very long.”; char reallyLong[10000] = “Dummy”; char critics[] = “Statler & Waldorf”; char number[4] = “Four”; char bad[2] = “Will this compile?”; char hello[6] = {‘H’,‘e’,‘l’,‘l’,‘o’,‘\0’}; longStr = “Seventy-five”; bob = “o”; reallyLong[0] = ‘B’; bob[3] = ‘a’;

18 Using a cString  Remember: cString simply array of characters  Any legal use of char array okay for cString char bob[4] = “Bob”; char longStr[20] = “This is very long.”; char reallyLong[10000] = “Dummy”; char critics[] = “Statler & Waldorf”; char number[4] = “Four”; char bad[2] = “Will this compile?”; char hello[6] = {‘H’,‘e’,‘l’,‘l’,‘o’,‘\0’}; longStr = “Seventy-five”; bob = “o”; reallyLong[0] = ‘B’; bob[3] = ‘a’;

19 Using a cString  Remember: cString simply array of characters  Any legal use of char array okay for cString char bob[4] = “Bob”; char longStr[20] = “This is very long.”; char reallyLong[10000] = “Dummy”; char critics[] = “Statler & Waldorf”; char number[4] = “Four”; char bad[2] = “Will this compile?”; char hello[6] = {‘H’,‘e’,‘l’,‘l’,‘o’,‘\0’}; longStr = “Seventy-five”; bob = “o”; reallyLong[0] = ‘B’; bob[3] = ‘a’;

20 Using a cString  Remember: cString simply array of characters  Any legal use of char array okay for cString char bob[4] = “Bob”; char longStr[20] = “This is very long.”; char reallyLong[10000] = “Dummy”; char critics[] = “Statler & Waldorf”; char number[4] = “Four”; char bad[2] = “Will this compile?”; char hello[6] = {‘H’,‘e’,‘l’,‘l’,‘o’,‘\0’}; longStr = “Seventy-five”; bob = “o”; reallyLong[0] = ‘B’; bob[3] = ‘a’;

21 cString Input & Output  Already works with printf like int or double  This is not normal & only for array of characters  Prints all characters until null character is hit  Array’s length unknown, only stops at null character  Two possible ways to read in a cString  Can read in all characters until whitespace reached  Read in at most n characters on a single line

22 Reading in “word” with scanf  Can use scanf to read in single “word”  Skips over any whitespace until finds a real character  Continues reading until it hits next whitespace  Result (& end null character) stored in cString  Caveat emptor: no check that array has enough space char lastName[80]; scanf(“%s”, lastName); char middleName[2]; scanf(“%s”, middleName);

23 Reading in Line with gets  Can use fgets to read in up to an entire line  Where last read stopped, this call starts  Continues reading until it hits end of the line  cString, potentially including ‘\n’, stored in argument 80 80 2 2  No worries: at most uses specified number of chars char lastName[80]; fgets(lastName, 80, stdin); char middleInitial[2]; fgets(middleInitial, 2, stdin);

24 Built-in String Functions  To use built-in string function, your program needs: #include #include  Many, many function exist for you to use  All of these listed on webpage from reading  Special feature of cStrings used by these functions  Unlike other arrays, can pass cString literals to functions char str[10] = “c”; str = “b”; strcat(str, “at”); printf(“%d\n”, strlen(“Hi mom.”));

25 Built-in String Functions  To use built-in string function, your program needs: #include #include  Many, many function exist for you to use  All of these listed on webpage from reading  Special feature of cStrings used by these functions  Unlike other arrays, can pass cString literals to functions char str[10] = “c”; str = “b”; strcat(str, “at”); printf(“%d\n”, strlen(“Hi mom.”));

26 Important String Functions  strlen(s)  Counts characters in s before null character  strcpy(s,t)  Copies characters in t into s.  strcat(s,t)  Appends t to end of s.  strcmp(s,t)  Returns negative int if s t, and 0 if s==t  strupr(s)  Converts all characters in s to uppercase  strlwr(s)  Converts all characters in s to lowercase  strncpy(s,t,n)  Copied first n characters from t to s  stricmp(s,t)  Like strcmp, but treats upper & lowercase characters as equals

27 Using String Functions char cat[100] = “ca”; int i; i = strlen(cat); cat[2] = ‘t’; i = strlen(cat); strcpy(cat, “dog”); printf(“%s\n”, cat); strcat(cat, “ged”); printf(“%s\n”, cat); strcpy(cat, “ca”); cat[2] = ‘t’; cat[3] = ‘\0’; printf(“%s\n”, cat);

28 Using String Functions char cat[100] = “cat” char str[100] = “apult”; strcat(cat, str); printf(“%s %s\n”, cat,str); printf(“%d\n”, strcmp(cat, str)); cat[0] = ‘C’; printf(“%d\n”, strcmp(cat, “catapult”)); printf(“%d\n”, stricmp(cat, “catapult”)); cat[1] = ‘\0’; strcat(str, cat); printf(“%s %s\n”, cat,str);

29 Your Turn  Get into your groups and try this assignment

30 For Next Lecture  More practice with arrays (tracing & coding) Wed.  Develop deeper understanding needed for later  Prepare for midterm in 2.5 weeks (Nov. 13 th )  Future weeks rely on expertise with how memory works  D2L also has Weekly Assignment #8 to do  Programming Assignment #2 due next week


Download ppt "PHY 107 – Programming For Science. Today’s Goal  Understand what cStrings are  Relationship to arrays & primitives  What it is and why null character."

Similar presentations


Ads by Google