CSCI 125 Introduction to Computer Science and Programming II Lecture …€¦ · Introduction to...
Transcript of CSCI 125 Introduction to Computer Science and Programming II Lecture …€¦ · Introduction to...
![Page 1: CSCI 125 Introduction to Computer Science and Programming II Lecture …€¦ · Introduction to Computer Science and Programming II Lecture 5: Char Array and String Jetic Gū 2020](https://reader033.fdocuments.in/reader033/viewer/2022060413/5f11b402cf851b5f442a76e6/html5/thumbnails/1.jpg)
22.06.20 17:36CSCI 125 Introduction to Computer Science and
Programming II Lecture 5: Char Array and String
Jetic Gū2020 Summer Semester (S2)
![Page 2: CSCI 125 Introduction to Computer Science and Programming II Lecture …€¦ · Introduction to Computer Science and Programming II Lecture 5: Char Array and String Jetic Gū 2020](https://reader033.fdocuments.in/reader033/viewer/2022060413/5f11b402cf851b5f442a76e6/html5/thumbnails/2.jpg)
Announcement• Assignment 2 has been posted last night, due date 30 June 2020
• Lab 2 submission has begun (I forgot to turn it on before Sunday)
• Due date is still 28 June 2020
• Practice Problem p18 test cases are still in the making (Tuesday at the latest)
• Turned out a lot harder to design test cases than provide solution
• Due date for all practice problems (p009-p018) is 28 June 2020
![Page 3: CSCI 125 Introduction to Computer Science and Programming II Lecture …€¦ · Introduction to Computer Science and Programming II Lecture 5: Char Array and String Jetic Gū 2020](https://reader033.fdocuments.in/reader033/viewer/2022060413/5f11b402cf851b5f442a76e6/html5/thumbnails/3.jpg)
Overview
• Focus: Basic C/C++ Syntax
• Architecture: Linux/Unix OS
• Core Ideas:
1. Character Arrays
2. Strings
![Page 4: CSCI 125 Introduction to Computer Science and Programming II Lecture …€¦ · Introduction to Computer Science and Programming II Lecture 5: Char Array and String Jetic Gū 2020](https://reader033.fdocuments.in/reader033/viewer/2022060413/5f11b402cf851b5f442a76e6/html5/thumbnails/4.jpg)
Data Types• Primary √
• Integers, Characters, Boolean √
• Floating point √
• Void √
• Derived
• Function√, Array√, Pointer√, Reference√
• User Defined
• Struct, Class, Enumerate, Typedef
Review
P0 Review
char is simple enough. what causes problems is char arrays
string is a class! You won’t find this in C!
![Page 5: CSCI 125 Introduction to Computer Science and Programming II Lecture …€¦ · Introduction to Computer Science and Programming II Lecture 5: Char Array and String Jetic Gū 2020](https://reader033.fdocuments.in/reader033/viewer/2022060413/5f11b402cf851b5f442a76e6/html5/thumbnails/5.jpg)
Character Arrays
Summary
P1 Char Strings
![Page 6: CSCI 125 Introduction to Computer Science and Programming II Lecture …€¦ · Introduction to Computer Science and Programming II Lecture 5: Char Array and String Jetic Gū 2020](https://reader033.fdocuments.in/reader033/viewer/2022060413/5f11b402cf851b5f442a76e6/html5/thumbnails/6.jpg)
Character Arrays
• Syntax
char str[100];
• The standard `string` data type in C
• You create an array of char, and you will have a string
• Fixed length: cannot exceed
Concep
t
P1 Char Strings
![Page 7: CSCI 125 Introduction to Computer Science and Programming II Lecture …€¦ · Introduction to Computer Science and Programming II Lecture 5: Char Array and String Jetic Gū 2020](https://reader033.fdocuments.in/reader033/viewer/2022060413/5f11b402cf851b5f442a76e6/html5/thumbnails/7.jpg)
Character Arrays
• Storage: like standard array
• Must include a termination element: signals the end of string
• Otherwise when you print it, your program will keep accessing memory regions until it finds a 0 value, which will likely cause segmentation fault
• In this example, it will be printed as "check"
Tech
nical
P1 Char Strings
str[0] str[1] str[2] str[3] str[4] str[5] str[6]
'c' 'h' 'e' 'c' 'k' 0 e'
![Page 8: CSCI 125 Introduction to Computer Science and Programming II Lecture …€¦ · Introduction to Computer Science and Programming II Lecture 5: Char Array and String Jetic Gū 2020](https://reader033.fdocuments.in/reader033/viewer/2022060413/5f11b402cf851b5f442a76e6/html5/thumbnails/8.jpg)
Input and Output using cstdio
• Input Output: cstdio library (or in C, stdio.h)
char str[100];
scanf("%s", str); // no address-of op since its array
printf("%s", str);
• Single char symbol in formatting: "%c"
• Char array (string) symbol in formatting: "%s"
• Although scanf works in most cases, it is NOT recommended
Concep
t
P1 Char Strings
![Page 9: CSCI 125 Introduction to Computer Science and Programming II Lecture …€¦ · Introduction to Computer Science and Programming II Lecture 5: Char Array and String Jetic Gū 2020](https://reader033.fdocuments.in/reader033/viewer/2022060413/5f11b402cf851b5f442a76e6/html5/thumbnails/9.jpg)
Input and Output using cstdio
• Read in a single string, separated by whitespace
scanf("%s", str); // input "Hello World"
printf("%s", str); // outputs "Hello"
• %s operates similarly to how scanf handles int input: it stops reading upon seeing a whitespace
• Demo 1 here
Tech
nical
P0 XXXXXXX
P1 Char Strings
![Page 10: CSCI 125 Introduction to Computer Science and Programming II Lecture …€¦ · Introduction to Computer Science and Programming II Lecture 5: Char Array and String Jetic Gū 2020](https://reader033.fdocuments.in/reader033/viewer/2022060413/5f11b402cf851b5f442a76e6/html5/thumbnails/10.jpg)
Input and Output using cstdio
• When the storage space is not enough for the string
char str[3];
scanf("%s", str); // input "cheeseburger"
printf("%s", str); // outputs "cheeseburger"
• scanf doesn’t check of memory space! This way unallocated memory will be used!
• Dangerous! Not recommended to use scanf for char arrays, we have alternatives.
Tech
nical
P0 XXXXXXX
P1 Char Strings
![Page 11: CSCI 125 Introduction to Computer Science and Programming II Lecture …€¦ · Introduction to Computer Science and Programming II Lecture 5: Char Array and String Jetic Gū 2020](https://reader033.fdocuments.in/reader033/viewer/2022060413/5f11b402cf851b5f442a76e6/html5/thumbnails/11.jpg)
Input and Output using cstdio
• Read in a single character
char str[3];
scanf("%s", str); // input "cheeseburger"
printf("%s", str); // outputs "cheeseburger"
• scanf doesn’t check of memory space! This way unallocated memory will be used!
• Dangerous!
Tech
nical
P0 XXXXXXX
P1 Char Strings
![Page 12: CSCI 125 Introduction to Computer Science and Programming II Lecture …€¦ · Introduction to Computer Science and Programming II Lecture 5: Char Array and String Jetic Gū 2020](https://reader033.fdocuments.in/reader033/viewer/2022060413/5f11b402cf851b5f442a76e6/html5/thumbnails/12.jpg)
Input and Output using cstdio• Correct way of reading in Char Array
fgets(str, sizeof(str), stdin);
• stdin: standard input stream, by default it is Keyboard input
• sizeof: returns the declared size of the array
• returns true if reading was successful
• This will keep reading until newline '\n' is detected (reads whitespace as well!) or storage is used up
• Safe: ensures only reading in characters within declared storage space
• Demo 3 here
Tech
nical
P0 XXXXXXX
P1 Char Strings
![Page 13: CSCI 125 Introduction to Computer Science and Programming II Lecture …€¦ · Introduction to Computer Science and Programming II Lecture 5: Char Array and String Jetic Gū 2020](https://reader033.fdocuments.in/reader033/viewer/2022060413/5f11b402cf851b5f442a76e6/html5/thumbnails/13.jpg)
Input and Output using cstdio
• Correct way of reading in Char Array
fgets(str, sizeof(str), stdin);
• stdin: standard input stream, by default it is Keyboard input
• sizeof: returns the declared size of the array
• returns true if reading was successful
Tech
nical
P0 XXXXXXX
P1 Char Strings
#include <cstdio>
using namespace std;
int main() {
char str[10000];
if (fgets(str, sizeof(str), stdin))
printf("\n%s\n", str);
else {
// handle error
}
return 0;
}
![Page 14: CSCI 125 Introduction to Computer Science and Programming II Lecture …€¦ · Introduction to Computer Science and Programming II Lecture 5: Char Array and String Jetic Gū 2020](https://reader033.fdocuments.in/reader033/viewer/2022060413/5f11b402cf851b5f442a76e6/html5/thumbnails/14.jpg)
• Reading in characters one at a time
char ch;
ch = getchar();
• Common usage
while ((ch = getchar()) != '\n' && ch != EOF) {... do stuff}
• EOF: end of file symbol, signals end of input (control-d if stdin, or an actual end of file)
• This can be very useful as well.
Input and Output using cstdio
Tech
nical
P0 XXXXXXX
P1 Char Strings
![Page 15: CSCI 125 Introduction to Computer Science and Programming II Lecture …€¦ · Introduction to Computer Science and Programming II Lecture 5: Char Array and String Jetic Gū 2020](https://reader033.fdocuments.in/reader033/viewer/2022060413/5f11b402cf851b5f442a76e6/html5/thumbnails/15.jpg)
Utilities• Some useful utilities for Character strings in cstring (In C the header is string.h)
• strcpy: Copy stringe.g.: strcpy (tgtStr, srcStr); // source str can be const
• strcat: Concatenate stringse.g.: strcat (tgtStr, "Hello "); // "Hello " + tgtStr
• strcmp: Compare of two strings, outputs 0 if equale.g.: if (strcmp(str1, str2) == 0) printf("Equal!\n");
Tech
nical
P0 XXXXXXX
1. http://www.cplusplus.com/reference/cstring/
P1 Char Strings
![Page 16: CSCI 125 Introduction to Computer Science and Programming II Lecture …€¦ · Introduction to Computer Science and Programming II Lecture 5: Char Array and String Jetic Gū 2020](https://reader033.fdocuments.in/reader033/viewer/2022060413/5f11b402cf851b5f442a76e6/html5/thumbnails/16.jpg)
More
• For more information on Char Arrays (especially dos and don’ts), here’s a useful reference online from CMU’s Software Engineering Institute
• From CMU: https://wiki.sei.cmu.edu/confluence/display/c/STR31-C.+Guarantee+that+storage+for+strings+has+sufficient+space+for+character+data+and+the+null+terminator
Tech
nical
P0 XXXXXXX
1. http://www.cplusplus.com/reference/cstring/
P1 Char Strings
![Page 17: CSCI 125 Introduction to Computer Science and Programming II Lecture …€¦ · Introduction to Computer Science and Programming II Lecture 5: Char Array and String Jetic Gū 2020](https://reader033.fdocuments.in/reader033/viewer/2022060413/5f11b402cf851b5f442a76e6/html5/thumbnails/17.jpg)
First taste of C++ class: string
Summary
P2 String
![Page 18: CSCI 125 Introduction to Computer Science and Programming II Lecture …€¦ · Introduction to Computer Science and Programming II Lecture 5: Char Array and String Jetic Gū 2020](https://reader033.fdocuments.in/reader033/viewer/2022060413/5f11b402cf851b5f442a76e6/html5/thumbnails/18.jpg)
String Class
• It is a class, so not supported in C (C++ Only)
• Internal Storage: uses Char Arrays
• If you look at the memory regions’ representation, it is almost identical to C char arrays
• Easier management, the class methods handles everything for you
Concep
t
P2 String
![Page 19: CSCI 125 Introduction to Computer Science and Programming II Lecture …€¦ · Introduction to Computer Science and Programming II Lecture 5: Char Array and String Jetic Gū 2020](https://reader033.fdocuments.in/reader033/viewer/2022060413/5f11b402cf851b5f442a76e6/html5/thumbnails/19.jpg)
String Class
• Header: must include string
#include <string>
• Syntax
std::string str;
// uses std namespace
• Maximum length: 4,294,967,295 (232-1)
Concep
t
P2 String
![Page 20: CSCI 125 Introduction to Computer Science and Programming II Lecture …€¦ · Introduction to Computer Science and Programming II Lecture 5: Char Array and String Jetic Gū 2020](https://reader033.fdocuments.in/reader033/viewer/2022060413/5f11b402cf851b5f442a76e6/html5/thumbnails/20.jpg)
String Class
• The statement: string name = "William Jacob";
declares name to be a string variable and also initialises name to "William Jacob"
• The first character, 'W', is in position 0
• The second character, 'i', is in position 1
Tech
nical
P0 XXXXXXX
P2 String
![Page 21: CSCI 125 Introduction to Computer Science and Programming II Lecture …€¦ · Introduction to Computer Science and Programming II Lecture 5: Char Array and String Jetic Gū 2020](https://reader033.fdocuments.in/reader033/viewer/2022060413/5f11b402cf851b5f442a76e6/html5/thumbnails/21.jpg)
String Class• Binary operator + and the array subscript operator [], have been defined for
the data type string
• + performs the string concatenation operation
• Example:
str1 = "Sunny";
str2 = str1 + " Day";
• stores "Sunny Day" into str2
Tech
nical
P0 XXXXXXX
P2 String
![Page 22: CSCI 125 Introduction to Computer Science and Programming II Lecture …€¦ · Introduction to Computer Science and Programming II Lecture 5: Char Array and String Jetic Gū 2020](https://reader033.fdocuments.in/reader033/viewer/2022060413/5f11b402cf851b5f442a76e6/html5/thumbnails/22.jpg)
Input and Output
• Assuming you are using std namespace
cin >> str; // reads until whitespace
cout << str;
• endl is also a string instance, which for us can be considered equivalent to "\n"
Concep
t
P2 String
![Page 23: CSCI 125 Introduction to Computer Science and Programming II Lecture …€¦ · Introduction to Computer Science and Programming II Lecture 5: Char Array and String Jetic Gū 2020](https://reader033.fdocuments.in/reader033/viewer/2022060413/5f11b402cf851b5f442a76e6/html5/thumbnails/23.jpg)
Input and Output
• Extraction operator
• Skips any leading whitespace characters and reading stops at a whitespace character
• The function getline
getline(cin, str);
• Reads until end of the current line
Tech
nical
P0 XXXXXXX
P2 String
![Page 24: CSCI 125 Introduction to Computer Science and Programming II Lecture …€¦ · Introduction to Computer Science and Programming II Lecture 5: Char Array and String Jetic Gū 2020](https://reader033.fdocuments.in/reader033/viewer/2022060413/5f11b402cf851b5f442a76e6/html5/thumbnails/24.jpg)
String Memory Structure• Building blocks
• Char Array, stored as Char pointerRemember, Arrays are Pointers! (Demo 4, 5)
• Size information: the memory allocated for this instance
• Iterator: provides the [ ] array access mechanism
• Core functions
• The class manages memory for you!
Tech
nical
P0 XXXXXXX
P2 String
![Page 25: CSCI 125 Introduction to Computer Science and Programming II Lecture …€¦ · Introduction to Computer Science and Programming II Lecture 5: Char Array and String Jetic Gū 2020](https://reader033.fdocuments.in/reader033/viewer/2022060413/5f11b402cf851b5f442a76e6/html5/thumbnails/25.jpg)
String Memory Structure
• Creation: storage space for short strings
• Empty string: same space
• Extra long strings: dynamically allocate space for you
• You wouldn’t notice this though, it’s designed so neatly!
Tech
nical
P0 XXXXXXX
P2 String
![Page 26: CSCI 125 Introduction to Computer Science and Programming II Lecture …€¦ · Introduction to Computer Science and Programming II Lecture 5: Char Array and String Jetic Gū 2020](https://reader033.fdocuments.in/reader033/viewer/2022060413/5f11b402cf851b5f442a76e6/html5/thumbnails/26.jpg)
Additional string Operations
• length
• substr
• swap
• There’s tons more, for more reference consult the C++ official doc
• http://www.cplusplus.com/reference/string/string/
Concep
t
P2 String
![Page 27: CSCI 125 Introduction to Computer Science and Programming II Lecture …€¦ · Introduction to Computer Science and Programming II Lecture 5: Char Array and String Jetic Gū 2020](https://reader033.fdocuments.in/reader033/viewer/2022060413/5f11b402cf851b5f442a76e6/html5/thumbnails/27.jpg)
length Function
• Returns the number of characters currently in the string
• Syntax:
str.length();
• length returns an unsigned int
• The value returned can be stored in an integer variable
Concep
t
P2 String
![Page 28: CSCI 125 Introduction to Computer Science and Programming II Lecture …€¦ · Introduction to Computer Science and Programming II Lecture 5: Char Array and String Jetic Gū 2020](https://reader033.fdocuments.in/reader033/viewer/2022060413/5f11b402cf851b5f442a76e6/html5/thumbnails/28.jpg)
substr Function
• Returns a particular substring of a string
• Syntax:
str.substr(expr1, expr2);
• expr1 and expr2 are expressions evaluating to unsigned integers
• expr1 specifies a position within the string (starting position of the substring)
• expr2 specifies the length of the substring to be returned
Concep
t
P2 String
![Page 29: CSCI 125 Introduction to Computer Science and Programming II Lecture …€¦ · Introduction to Computer Science and Programming II Lecture 5: Char Array and String Jetic Gū 2020](https://reader033.fdocuments.in/reader033/viewer/2022060413/5f11b402cf851b5f442a76e6/html5/thumbnails/29.jpg)
swap Function• Interchanges contents of two string variables
• Syntax: str1.swap(str2);
• Suppose you have the following statements:
string str1 = "Warm";
string str2 = "Cold";
• After str1.swap(str2); executes, the value of str1 is "Cold" and the value of str2 is "Warm"
Concep
t
P2 String