Download presentation
Presentation is loading. Please wait.
1
Strings
2
Declaring Strings 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; 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’;
3
String 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 cin.peek() returns the next character without removing cin.get() returns the next character cin.unget() puts it back
4
String Assignment and 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
5
Comparing Strings 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”;
6
String Functions String Characteristics
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
7
Accessing Elements of Strings
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
8
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’);
9
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); }
10
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 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”
11
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 s.erase(1,2); cout << s; // outputs ”Hlo” other useful functions s.clear() empties the string s.resize(50) changes size to specified, either truncating or extending with null characters
12
Passing Strings as Parameters Returning Strings
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
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.