The CStyle Character String char greeting6 H e

  • Slides: 22
Download presentation
The C-Style Character String: char greeting[6] = {'H', 'e', 'l', 'o', '�'}; char greeting[]

The C-Style Character String: char greeting[6] = {'H', 'e', 'l', 'o', ''}; char greeting[] = "Hello";

S. Function & Purpose N. 1 strcpy(s 1, s 2); Copies string s 2

S. Function & Purpose N. 1 strcpy(s 1, s 2); Copies string s 2 into string s 1. 2 strcat(s 1, s 2); Concatenates string s 2 onto the end of string s 1. 3 strlen(s 1); Returns the length of string s 1. strcmp(s 1, s 2); 4 Returns 0 if s 1 and s 2 are the same; less than 0 if s 1<s 2; greater than 0 if s 1>s 2. strchr(s 1, ch); 5 Returns a pointer to the first occurrence of character ch in string s 1. strstr(s 1, s 2); 6 Returns a pointer to the first occurrence of string s 2 in string s 1.

include <iostream> # #include <cstring> using namespace std ; int main (){ char str

include <iostream> # #include <cstring> using namespace std ; int main (){ char str 1[10] = "Hello"; char str 2[10] = "World"; char str 3[10]; int len ; // copy str 1 into str 3 strcpy( str 3, str 1); cout << "strcpy( str 3, str 1) : " << str 3 << endl; // concatenates str 1 and str 2 strcat( str 1, str 2); cout << "strcat( str 1, str 2): " << str 1 << endl; // total lenghth of str 1 after concatenation len = strlen(str 1); cout << "strlen(str 1) : " << len << endl; return 0; }

The String Class in C++: string str 1 = "Hello"; string str 2 =

The String Class in C++: string str 1 = "Hello"; string str 2 = "World"; string str 3; int len ; // copy str 1 into str 3 = str 1; cout << "str 3 : " << str 3 << endl; // concatenates str 1 and str 2 str 3 = str 1 + str 2; cout << "str 1 + str 2 : " << str 3 << endl; // total lenghth of str 3 after concatenation len = str 3. size(); cout << "str 3. size() : " << len << endl;

Implicit And Explicit Initialiation string s 1; // Default constructor - creates an empty

Implicit And Explicit Initialiation string s 1; // Default constructor - creates an empty or null C++ string of length 0, equal to "" string s 2("hello"); // Explicit constructor call to initialize new object with C string s 3 = "hello"; // Implicit constructor call to initialize new object with C string s 4(s 2); // Explicit constructor call to initialize new object with C++ string s 5 = s 2; // Implicit constructor call to initialize new object with C++ string

Creating String Objects #include <string> //string initialization string s; string s 1( "Hello" );

Creating String Objects #include <string> //string initialization string s; string s 1( "Hello" ); string s 2 = “HELLO”; //s contains 0 characters //s 1 contains 5 characters //s 2 contains 5 characters //implicitly calls the constructor string s 3( 8, 'x' ); string s 4 = s 3; //s 3 contains 8 'x' characters //s 4 contains 8 'x' characters string s 5(s 2, 3, 2); //s 5 copies a substring of s 2; it contains ”LO”

string s = “ABCDEFG”; char c = s[2]; //assigns ‘C’ to c S[4] =

string s = “ABCDEFG”; char c = s[2]; //assigns ‘C’ to c S[4] = ‘*’; //changes s to “ABCD*FG” string s = “ABCD*FG”; string s 2 = “Robot”; string s 5 = “Soccer”; string s 6 = s + “HIJK”; //changes s 6 to “ABCD*FGHIJK s 2 += s 5; //changes s 2 to “Robot. Soccer”

Substring function: substr() s 6 = “ABCD*FGHIJK”; s 4 = s 6. substr(5, 3);

Substring function: substr() s 6 = “ABCD*FGHIJK”; s 4 = s 6. substr(5, 3); //changes s 4 to “FGH” erase() and replace() functions: s 6 = “ABCD*FGHIJK”; s 6. erase(4, 2); //changes s 6 to “ABCDGHIJK”; s 6. replace(5, 2, “xyz”); //changes s 6 to “ABCDGxyz. JK”;

 • s 1. erase( start ) – Erase from index start to end

• s 1. erase( start ) – Erase from index start to end of string, including start • Replace – s 1. replace( begin, N, s 2) • begin: index in s 1 to start replacing • N: number of characters to replace • s 2: replacement string – s 1. replace( begin, N, s 2, index, num ) • index: element in s 2 where replacement comes from • num: number of elements to use when replacing – Replace can overwrite characters

s 1. replace( begin, N, s 2, index, num ) • • • begin:

s 1. replace( begin, N, s 2, index, num ) • • • begin: index in s 1 to start replacing N: number of characters to replace s 2: replacement string index: element in s 2 where replacement comes from num: number of elements to use when replacing str = "this is an example string. "; string str 3="sample phrase"; str. replace(19, 6, str 3, 7, 6); // "this is an example phrase. "

find() function Returns the index of the first occurrence of a given substring: string

find() function Returns the index of the first occurrence of a given substring: string s 7 = “Mississippi River basin”; //23 characters cout << s 7. find(“si”) << endl; //prints 3 If the find() function fails, it returns the length of the string it was searching. cout << s 7. find(“so”) << endl; //prints 23, the length of the string

Find functions – s 1. find( s 2 ) – s 1. rfind( s

Find functions – s 1. find( s 2 ) – s 1. rfind( s 2 ) • Searches right-to-left – s 1. find_first_of( s 2 ) • Returns first occurrence of any character in s 2 Example: s 1. find_first_of( "abcd" ) – Returns index of first 'a', 'b', 'c' or 'd'

Find functions s 1. find_last_of( s 2 ) Finds last occurrence of any character

Find functions s 1. find_last_of( s 2 ) Finds last occurrence of any character in s 2 s 1. find_first_not_of( s 2 ) Finds first character NOT in s 2 s 1. find_last_not_of( s 2 ) Finds last character NOT in s 2

Assignment – s 2 = s 1; • Makes a separate copy – s

Assignment – s 2 = s 1; • Makes a separate copy – s 2. assign(s 1); • Same as s 2 = s 1; – my. String. assign(s, start, N); • Copies N characters from s, beginning at index start – Individual character assignment • s 2[0] = s 3[2];

Insertion • s 1. insert( index, s 2 ) – Inserts s 2 before

Insertion • s 1. insert( index, s 2 ) – Inserts s 2 before position index • s 1. insert( index, s 2, index 2, N ); – Inserts substring of s 2 before position index – Substring is N characters, starting at index 2

Concatenation s 3. append( "pet" ); s 3 += "pet"; Both add "pet" to

Concatenation s 3. append( "pet" ); s 3 += "pet"; Both add "pet" to end of s 3. append( s 1, start, N ); Appends N characters from s 1, s 1 beginning at index start Swap • s 1. swap(s 2); – Switch contents of two strings

Comparison s 1. compare(s 2) returns positive if s 1 is lexicographically greater compares

Comparison s 1. compare(s 2) returns positive if s 1 is lexicographically greater compares letter by letter 'B' lexicographically greater than 'A‘ ‘a’ lexicographically greater than ‘Z‘ returns negative if less; zero if equal Sample order: ‘A’, ”Apple”, “Banana”, “Zest”, ‘a’, “apricot”, “leon” s 1. compare(start, length, s 2, start, length) Compare portions of s 1 and s 2 s 1. compare(start, length, s 2) Compare portion of s 1 with all of s 2

Member functions – s 1. size() and s 1. length() • Number of characters

Member functions – s 1. size() and s 1. length() • Number of characters in a string – s 1. capacity() • Number of elements that can be stored without reallocation – s 1. max_size() • Maximum possible string size – s 1. empty() • Returns true if empty – s 1. resize(newlength) • Resizes string to newlength

Conversion to C-Style char* – Strings are not necessarily null-terminated – s 1. copy(

Conversion to C-Style char* – Strings are not necessarily null-terminated – s 1. copy( ptr, N, index ) • Copies N characters into the array ptr • Starts at location index • Need to null terminate char str[8]; Output: str = thode s 2 = cathode string s 2 = "cathode"; s 2. copy(str, 5, 2); //copy 5 characters into str //starting at index 2 str[5] = ''; //this is required cout << "str = " << str << endl; cout << "s 2 = " << s 2 << endl; 19

Method Use append(char *pt); append(char *pt, size_t count); append(string &str, size_t offset, size_t count);

Method Use append(char *pt); append(char *pt, size_t count); append(string &str, size_t offset, size_t count); append(string &str); append(size_t count, char ch); append(Input. Iterator Start, Input. Iterator End); Appends characters to a string from C-style strings, char's or other string objects. at(size_t offset); Returns a reference to the character at the specified position. Differs from the subscript operator, [], in that bounds are checked. begin(); Returns an iterator to the start of the string. *c_str(); Returns a pointer to C-style string version of the contents of the string. clear(); Erases the entire string. copy(char *cstring, size_t count, size_t offset); Copies "count" characters into a C-style string starting at offset. empty(); Test whether a string is empty. end(); Returns an iterator to one past the end of the string. erase(iterator first, iterator last); erase(iterator it); erase(size_t pos, size_t count); Erases characters from the specified positions. 20

Method Use find(char ch, size_t offset = 0); find(char *pt, size_t offset = 0);

Method Use find(char ch, size_t offset = 0); find(char *pt, size_t offset = 0); find(string &str, size_t offset = 0); Returns the index of the first character of the substring when found. Otherwise, the special value "npos" is returned. find_first_not_of(); Same sets of arguments as find. Finds the index of the first character that is not in the search string. find_first_of(); Same sets of arguments as find. Finds the index of the first character that is in the search string. find_last_not_of(); Same sets of arguments as find. Finds the index of the last character that is not in the search string. find_last_of(); Same sets of arguments as find. Finds the index of the last character that is in the search string. insert(size_t pos, char *ptr); insert(size_t pos, string &str); insert(size_t pos, size_t count, char ch); insert(iterator it, Input. Iterator start, Input. Iterator end); Inserts characters at the specified position. push_back(char ch); Inserts a character at the end of the string. replace(size_t pos, size_t count, char *pt); replace(size_t pos, size_t count, string &str); replace(iterator first, iterator last, char *pt); replace(iterator first, iterator last, string &str); Replaces elements in a string with the specified characters. The range can be specified by a start position and a number of elements to replace, or by using iterators. size(); Returns the number of elements in a string. swap(string &str); Swaps two strings. 21