CSC 270 Survey of Programming Languages C Lecture

  • Slides: 15
Download presentation
CSC 270 – Survey of Programming Languages C++ Lecture 2 – Strings Credited to

CSC 270 – Survey of Programming Languages C++ Lecture 2 – Strings Credited to Dr. Robert Siegfried

Goals • • • Standard string vs cstring functions std: : string member methods

Goals • • • Standard string vs cstring functions std: : string member methods Functions that use std: : string cin / cout / character manipulation getline vs cin

Standard String and C Style String • http: //www. tutorialspoint. com/cplus/cpp_strings. htm • Declare

Standard String and C Style String • http: //www. tutorialspoint. com/cplus/cpp_strings. htm • Declare and initialize: – char cstring[100]; – char greeting[6] = {'H', 'e', 'l', 'o', ''}; – char a[] = "text"; • #include <cstring> • #include <cctype>

Predefined Functions in <cstring> Function Description Caution strcpy(s, t) Copies t into s No

Predefined Functions in <cstring> Function Description Caution strcpy(s, t) Copies t into s No bounds checking strncpy(s, t, n) Copies t into s but no more than n characters are copies Not implemented in all versions of c++ strcat(s, t) Concatenates t to the end of s No bounds checking strncat(s, t, n) Concatenates t to the end of s but no more than n characters Not implemented in all versions of c++ strlen(s) Returns the length of s (not counting ‘’) strcmp(s, t) Returns 0 if s == t < 0 if s < t > 0 if s > t No bounds checking strncmp(s, t, n) Same as strcmp but compares no more than n characters Not implemented in all versions of c++

Functions in <cctype> Function Description Example toupper(c) Returns the upper case version of the

Functions in <cctype> Function Description Example toupper(c) Returns the upper case version of the character c = toupper(‘a’); tolower(c) Returns the lower case version of the character c = tolower(‘A’); isupper(c) Returns true if c is an upper case letter if (isupper(c)) cout << ‘upper case’; islower(c) Returns true if c is an lower case letter if (islower(c)) cout << ‘lower case’; isalpha(c) Returns true if c is a letter if (isalpha(c)) isdigit(c) Returns true if c is a digit (0 through 9) cout << “it’s a letter”; if (isalpha(c)) cout << “it’s a number”;

Functions in <cctype> (continued) Function Description Example isalnum(c) Returns true if c is alphanumeric

Functions in <cctype> (continued) Function Description Example isalnum(c) Returns true if c is alphanumeric if (isalnum(‘ 3’)) cout << “alphanumeric”; isspace(c) Returns true if c is a white space character while (isspace(c)) cin. get(c); ispunct(c) Returns true if c is a printable if (ispunct(c)) cout << “punctuation”; character other than number, letter or white space isprint(c) Returns true if c is a printable character isgraph(c) Returns true if c is a printable character other an white space isctrl(c) Returns true if c is a control character

Standard String • std: : string • include <string> or using namespace std •

Standard String • std: : string • include <string> or using namespace std • http: //en. cppreference. com/w/cpp/header – = assigns and + concatenates and == tests – 2 ways to create • string str = "a string" • string str("a string") – str. length – returns length as a property – str[#] – lets you get straight to character index as though it were an array (no protection on out of bounds) – str. at(#) – returns character in str at I (does have bounds checking) – str. substr(position, length); - returns substring – str. insert(position, str 2); put str 2 into str starting at position – str. replace(position, length to be replaced, replacement string) – str. find(str 1, position) –returns index of str 1 in str (position optional) – str. c_str() – returns a cstring – Helpful: getline(cin, str) : std: : getline (std: : cin, name); - input up to, not including null from cin to str

Palindrome wget http: //home. adelphi. edu/~pe 16132/csc 270/note/S ample. Code/cplus/Palindrome. cpp • logic –

Palindrome wget http: //home. adelphi. edu/~pe 16132/csc 270/note/S ample. Code/cplus/Palindrome. cpp • logic – ask user for string, test palindrome (is. Pal), report – test palindrome: make entry lowercase, remove punctuation, and then reverse it. Is reversal = original? – Send const string & as parms • pass by ref (no copy) • allow no change • See string functions in use

Member Functions of the string class Example Remarks Constructors string str Default constructor –

Member Functions of the string class Example Remarks Constructors string str Default constructor – creates empty string object string str("string"); Creates a string object with data "string" string str(a. String); Creates a string object that is a copy of a. String, (which is a string object) Element Access str[i] Returns read/write reference to character in str at index i str. at(i) Returns read/write reference to character in str at index i str. substr(position, length) Return the substring of the calling object starting at position and having length characters

Member Functions of the string class Example Remarks Assignment/Modifiers string str 1 = str

Member Functions of the string class Example Remarks Assignment/Modifiers string str 1 = str 2; Allocates space and initializes it to str 1’s data, releases memory allocated to str 1 and sets str 1's size to that of str 2. str 1 += str 2; Character data of str 2 is concatenated to the end of str 1; the size is set appropriately str. empty(); Returns true if str is an empty string; returns false otherwise str 1 + str 2 Returns a string that has str 2’s data concatenated to the end of str 1’s data. The size is set appropriately str. insert(pos, str 2) Inserts str 2 into str beginning at position pos str. remove(pos, length) Removes a substring of size length beginning at position pos

Member Functions of the string class Example Remarks Comparisons str 1 == str 2

Member Functions of the string class Example Remarks Comparisons str 1 == str 2 str 1 != str 2; str 1 < str 2 str 1 >= str 2 Compare for equality or inequality; returns a Boolean value. str 1 > str 2 str 1 <= str 2; Four comparisons. All are lexicographical comparisons str. find(str 1) Returns index of the first occurrence of str 1 in str. find(str 1, pos) Returns index of the first occurrence of str 1 in str; the search starts at position pos. str. find_first_of(str 1, pos) Returns index of the first instance of any character in str 1; the search starts at position pos. str. find_first_not_of(pos, length) Returns index of the first instance of any character not in str 1; the search starts at position pos

Converting string objects and C-Strings • Sample String and cstring: – char a. CString[]

Converting string objects and C-Strings • Sample String and cstring: – char a. CString[] = “This is my C-string. ”; – std: : string. Variable; • CString to String – string. Variable = a. CString; – std: : string. Variable 2 (a. CString); – NOT: strcpy(string. Variable, a. CString); • String to CString – strcpy(a. CString, string. Variable. c_str()); – NOT: a. CString = string. Variable; – NOT: strcpy(ACString, string. Variable);

C-String: Input and Output • In addition to cin >> and cout << ,

C-String: Input and Output • In addition to cin >> and cout << , there are other input and output methods available when working with strings: – getline()-- up to, not incl new line one char: – get()-- get one char in – put() -- put one char out – putback () -- put one char back to input – peek() -- look one char ahead many char: – ignore() -- ignore # of char until match

more Versions of getline • getline(cin, line); will read until the newline character. •

more Versions of getline • getline(cin, line); will read until the newline character. • getline(cin, line, '? '); will read until the '? '. • getline(cin, s 1) >> s 2; will read a line of characters into s 1 and then store the next string (up to the next whitespace) in s 2.

Summary • • • Standard string vs cstring functions std: : string member methods

Summary • • • Standard string vs cstring functions std: : string member methods Functions that use std: : string cin / cout / character manipulation getline vs cin