1 / 17

CSE 20232 Lecture 33 – Strings

CSE 20232 Lecture 33 – Strings. What is a “string”? C++ string class C strings (char arrays) Next Homework Coder Encodes and decodes text using strings. What is a C++ string. Defined in the header <string> Has dynamically sized, indexed storage of characters Knows its own size

kaiya
Download Presentation

CSE 20232 Lecture 33 – Strings

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSE 20232Lecture 33 – Strings • What is a “string”? • C++ string class • C strings (char arrays) • Next Homework • Coder • Encodes and decodes text using strings

  2. What is a C++ string • Defined in the header <string> • Has dynamically sized, indexed storage of characters • Knows its own size • Has overloaded operators for … • Assignment: = • Comparison: <, >, <=, >=, ==, != • Concatenation: +, += • Other functions: • size(), find(), rfind(), substr(), c_str(), …

  3. What is a C-style string • A statically allocated char array • End of string is marked with ASCII code 0 (char ‘\0’) • We can find its size • String operations are defined in headers <string.h> and <cstring> • Assignment: strcpy(), strncpy() • Comparison: strcmp(), strncmp() • Concatenation: strcat(), strncat() • Find: index(), rindex(), strchr() • Others: strlen(), …

  4. Strings side-by-sideC++ String vs. C-style string • Declaring #include <string> #include <string.h> using namespace std; // no namspace here string line, other; char line[256], other[256]; • Assigning line = “Hello there!”; strcpy(line,”Hello there!”); other = line; strcpy(other,line);

  5. Strings side-by-sideC++ String vs. C-style string • Assigning substrings // copy first 5 chars // copy first 5 chars other = line.substr(0,5); strncpy(other,line,5); // copy line[3]..line[5] // copy line[3]..line[5] other = line.substr(3,3); strncpy(other,line+3,3);

  6. Strings side-by-sideC++ String vs. C-style string • Concatenating strings // since line is “Hello there!” // these will set other to “hello there! Ok?” // C++ concatenation // C requires two steps other = line + “ Ok?”; strcpy(other,line); strcat(other,“ Ok?”); // creating “Hello Hello ” // only copy or cat 5 chars other = line.substr(0,5); strncpy(other,line,5); other = other + other; strncat(other,line,5);

  7. Strings side-by-sideC++ String vs. C-style string • Using length or size of strings and indexing individual characters // C++ : get size // C : get length int i, num = line.size(); int i, num = strlen(line); // output in order // output in order for (i = 0; i < num; i++) for (i = 0; i < num; i++) cout << line[i]; cout << line[i]; // output in reverse //output in reverse for (i=num-1; i>=num; i--) for (i=num-1; i>=num; i--) cout << line[i]; cout << line[i];

  8. Strings side-by-sideC++ String vs. C-style string • Comparing whole strings // output the string that is lexicographically first // C++ strings // C-style strings if (line < other) if (strcmp(line,other) < 0) cout << line; cout << line; else if (line > other) else if (strcmp(line,other) > 0) cout << other; cout << other; else else // strcmp(line,other) == 0 cout << “Same string”; cout << “Same string”;

  9. Strings side-by-sideC++ String vs. C-style string • Comparing substrings // output the string that is lexicographically first // based only on the first 5 characters // C++ strings // C-style strings subL = line.substr(0,5) ; subO = other.substr(0,5) ; if (subL < subO) if (strncmp(line,other,5) < 0) cout << line; cout << line; else if (subL > subO) else if (strncmp(line,other,5) > 0) cout << other; cout << other; else else // strncmp(line,other,5) == 0 cout << “Same first 5”; cout << “Same first 5”;

  10. Strings side-by-sideC++ String vs. C-style string • Finding occurrences of a char in the string // find first ‘e’ in “Hello there!” // C++ strings // C-style strings // get index of the char // get a pointer to the char int pos =line.find(‘e’); char *p = strchr(line,‘e’); // output rest of string // output rest of string cout << line.substr(pos); cout << p; // get index of next ‘e’ // get a pointer to next ‘e’ int pos =line.find(‘e’,pos+1); p = strchr(p+1,‘e’); // output rest of string // output rest of string cout << line.substr(pos); cout << p;

  11. Example using strchr() // c-style string test #include <iostream> #include <cstring> using namespace std; int main() { char line[256], other[256]; strcpy(line,"Hello there!"); // get a pointer to first 'e' char *p = strchr(line,'e'); cout << p << endl; // OUTPUT: ello there! // get a pointer to next 'e' p = strchr(p+1,'e'); cout << p << endl; // OUTPUT: ere! return 0; }

  12. C-style strings in memory Note: the actual size of array must be one position longer than the length of the largest string that will be stored there If this is missing, there is no way to know where string ends // declare and set value of str char str[10]; strcpy(str, “the chars”);

  13. C++ string search functions • find(v,i) and rfind(v,i) • Find first or last occurrence of value • Value may be char or string or c-string • Optional index indicates where search starts • find_first_of(v,i), find_last_of(v,i) • Find first or last occurrence of ANY char in value • Value may be char or string or c-string • Optional index indicates where search starts • find_first_not_of(v,i), find_last_not_of(v,i) • Find first or last occurrence NOT ANY char in value • Value may be char or string or c-string • Optional index indicates where search starts

  14. Next homework • A simple encoder/decoder program • Uses string manipulation • You may use either C++ strings or C-style strings • You will probably need to use some of each • C-style will be required for handling the command line argument (argv) • The program will use a variation of a shift cipher along with randomly embedded distracters

  15. Coder • Usage • coder -c for encoding plain text • coder -d for decoding previously coded text • The program will be used with the Linux pipe (|) and I/O redirection (> < ) symbols so that there is no user interaction. See sample usage below. • coder –c < plain.txt • Encodes text from file “plain.txt” and outputs to screen • coder –d < coded.txt • Decodes text from file “coded.txt” and outputs to screen • cat plain.txt | coder –c • Output from cat is piped into coder where it is encoded and sent to screen • echo test it on this | coder –c > test.txt • “test it on this” is piped into coder, encoded, and sent to file “text.txt” • echo hello | coder –c | coder –d • “hello” is encoded and then decoded and then sent to the screen

  16. Coder • Format of code is • [s][b]xx[block1]xx[block2]xx[block3]……. • [s] is a shift value 1..25 • It is used to shift all letters up by that amount • The shift wraps around in the alphabet • Upper case and lower case maintain their case in the code • [b] is the block size • Each block is a group of characters from the original plain text • xx stands for two random characters with ASCII values 33..126 that are • Inserted between blocks when encoding • Skipped over during decoding

  17. Coder • Format of code is • [s][b]xx[block1]xx[block2]xx[block3]……. • Example: • This is a test. • Gets encoded differently based on shift and block size • Shift 22 and clock size 2 • w2/PPd*)eo6h eKfo iQw tTpa)=opiV. • Shift 24 and block size 3 • y3TtRfg.pq gibq yxZ rc>,qr.

More Related