Download presentation
Presentation is loading. Please wait.
Published byMartha Tyler Modified over 8 years ago
1
CSC 107 – Programming For Science
2
Today’s Goal Understand what cStrings are Relationship to arrays & primitives What the null character is; why we rely upon it Know how several built-in string functions work
3
Today’s Goal Understand what cStrings are Relationship to arrays & primitives What the null character is; why we rely upon it Know how several built-in string functions work Unlike “string theory”, cStrings practical & useful Also understood by more than 10 people
4
Strings
5
What We Want Human beings generally like using words & test “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++ treats as cString literal “Hi, Mom” “Hello world” “Good morning, Prof. Hertz. You are the greatest.” “Yes, the ‘Skins are the best football team.” “Yes! The Red Sox came back & beat the Yankees” “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 cout just 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 using cin Can read in all characters until whitespace reached Read in at most n characters on a single line
22
Reading in “word” with cin Can use cin 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]; cin >> lastName; char middleName[2]; cin >> middleName;
23
Reading in Line with cin Can use cin to read in up to an entire line Where last cin statement stopped, this call will start Continues reading until it hits end of the line Result (& end null character) stored in cString No worries: includes maximum characters to read 80 80 2 2 char lastName[80]; cin.getline(lastName, 80); char middleInitial[2]; cin.getline(middleName, 2);
24
Built-in String Functions To use built-in string function, your program needs: #include #include Many, many function exist for you to use Several of these listed on page 311 of book 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”); cout << 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 Several of these listed on page 311 of book 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”); cout << 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”); cout << cat << endl; strcat(cat, “ged”); cout << cat << endl; strcpy(cat, “ca”); cat[2] = ‘t’; cat[3] = ‘\0’; cout << cat << endl;
28
Using String Functions char cat[100] = “cat” char str[100] = “apult”; strcat(cat, str); cout << cat << “ ” << str << endl; cout << strcmp(cat, str) << endl; cat[0] = ‘C’; cout << strcmp(cat, “catapult”) << endl; cout << stricmp(cat, “catapult”) << endl; cat[1] = ‘\0’; strcat(str, cat); cout << cat << “ ” << str << endl;
29
Your Turn Get into your groups and try this assignment
30
For Next Lecture Multidimensional arrays in Section 10.10 – 10.11 What do these arrays look like? How can we create pictures & bridges in C++? How much cooler can arrays get? Angel also has Weekly Assignment #9 due today Programming Assignment #2 due on Friday
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.