130 likes | 150 Views
Strings. character manipulation. Declaration. strings are specific constructs that are designed for processing sequences of characters before using, include string header: #include <string> don’t use string.h as well as using std::string; string declaration: string mystr;
E N D
Strings character manipulation
Declaration • strings are specific constructs that are designed for processing sequences of characters • before using, include string header: #include <string>don’t use string.h • as well as usingstd::string; • string declaration: string mystr; • value assignment at declaration: string mystr(”Hello”); string mystr2=”Hello”; • assignments from other types are not permitted: string error1 = ’c’; // error string error2 = 22; // error • can assign a character with assignment operator: mystr=’n’;
I/O • string can be output as any other type: string s=”hello world”; cout << s << endl; • two ways to input strings: • using extraction operator - strips white space and assigns the first token (word) to the string: cin >> s; hello world\n input assigns only hello to s • using getline function - assigns all characters to string up to newline (not included, \n is discarded): getline(cin, s); hello world\n input assigns hello world to s • to successfully mix getline and extraction, may need cin.peek() – returns next character without removing cin.get() – returns a single next character cin.unget() – puts previously returned character back into cin
Assignment, Concatenation • use assignment operator as with other types: string s1, s2, s3; s1=”C++”; s2=”fun”; • plus “+” is used for string concatenation: s3=s1 + ” is ” + s2; • at least one operand has to be string variable! • compound concatenation allowed: s1 += ” language”; • characters can be concatenated with strings: s2 = s1 + ’o’; s2+=’o’; • no other types can be assigned to strings or concatenated with strings: s2= 42.54; // error s2=”Catch” + 22; // error
Comparing • comparison operators (>, <, >=, <=, ==, !=) are applicable to strings • strings are compared lexicographically: string s1=”accept”, s2=”access”, s3=”acceptance”; s1 is less than s2 and s1 is less than s3 • the following rules hold: • letters in the alphabet are in the increasing order • longer word (with the same characters) is greater than shorter word • comparison to literal string constants and named constants is also legal: const string myname=”John Doe”; string hername=”Jane Doe”; if ((myname==hername)||(myname==”Jake Doe”)) cout << ”found him\n”;
Functions, Size • a number of standard functions are defined for strings. Usual syntax: string_name.function_name(arguments) • useful functions return string paremeters: • size() - current string size (number of characters currently stored in string • length()- same as size() • max_size() - maximum number of characters in string allowed on this computer • empty() - true if string is empty • example: string s=”Hello”; cout << s.size(); outputs 5
Accessing Elements • similar to arrays a character in a string can be accessed and assigned to using its index (start from 0) cout << str[3]; • it is an error to access an element beyond the size of the string: string s=”Hello”; // size is 5 cout << s[6]; //error • the type of the element of the string is character, assigning integers, strings and other types are not allowed s[3] = ”hi”; //error s[3] = 22; //error
Substrings, Searching • substr - function that returns a substring of a string: substr(start, numb)start - index of the first character, numb - number of characters string s=”Hello”; // size is 5 cout << s.substr(3,2); // outputs ”lo” • find family of functions return position of substring found, if not found return global constant string::npos defined in string header • find(substring) - returns the position of the first character of substring in the string • rfind(substring) - same as find, search in reverse • find_first_of(substring) - find first occurrence of any character of substring in the string • find_last_of(substring) - find last occurrence of any character of substr in the string • all functions work with individual characters as well: cout << s.find(’l’);
Searching for All • all search functions work with a particular starting position find(substring, startingPosition) • example: reporting all occurrences of character ’a’ string s= ”baa baa black sheep”; int pos=s.find(’a’); while (pos != string::npos){ cout << ”found a at ” << pos << endl; pos=s.find(’a’, pos+1); }
Inserting, Replacing • insert(start, substring)- inserts substring starting from position start string s=”place”; cout << s.insert(1, ”a”); // produces ”palace” • variant insert(start, number, character) – inserts number of character starting from position start string s=”place”; cout << s.insert(4, 2, ’X’); // produces ”placXXe” note it is a character not a string • replace (start, number, substring)- replaces number of characters starting from start with substring the number of characters replaced need not be the same string s=”Hello”; s.replace(1,4, ”i, there”); // produces ”Hi, there”
Appending, Erasing • append(string2)- appends string2 to the end of the string string s=”Hello”; cout << s.append(”, World!”); cout << s; // outputs ”Hello, World!” • erase(start, number)- removes number of characters starting from start string s=”Hello”; s.erase(1,2); cout << s; // outputs ”Hlo”
Passing as Parameters, Returning • strings can be passed as parameters: • by value: void myfunc(string s); • by reference: void myfunc(string &s); if string is not modified by function use const type modifier: void myfunc(const string &s); • strings (unlike arrays) can be returned: • string myfunc(int, int); • note, that passing strings by value and returning strings is less efficient than passing them by reference: careful when they are large
Strings Reveiw • which include-file needs to be used for string manipulation? • what using statement needs to be included fro string manipulation? • how is a string assigned a value? initialized? • how is getline() different from extraction operator? • what does this line do? string s1=s2 + ’ ’ + ”h1”; • how are strings compared? Which one is greater? hell bell hello • how can string size be determined? what is the size of this string? string s2=”hello”; • is there a problem with this statement? cout << s2[5]; • assuming string s3=”C++ is fun”; what do the following functions do? s3.substr(4,2); s3.find(’+’); s3.rfind(’+’); s3.find_first_of(”is”); s3.find_last_of(”is”); s3.insert(3,2,’ ’); s3.replace(1,3,”--”); s3.append(”!!”); s3.erase(0,3); • how do you find every character 'a' in a string? • can string be passed by value? reference? returned?