C Strings
C Strings
characters c string c++ string class Examples: hello, high school, H2O. Typical desirable operations on strings are: Concatenation: high+school=highschool Comparisons: high<school // alphabetical Finding/retrieving/modifying/deleting/inserting substrings in a given string
Strings in C
In C, a string can be a specially terminated char
a char array, such as char str[ ]=high; a char pointer, such as char *p = high;
equal to \0, signaling the end For example, the above str[] is really of length 5: str[0]=h str[1]=i str[2]=g str[3]=h str[4]=\0 The same array couldve been declared as:
char str[5] = {h,i, g,h,\0};
array of chars but not a string. In char *p=high; the system allocates memory of 5 characters long, stores high in the first 4, and \0 in the 5th.
implemented
It is very convenient and makes string
Declaration of strings
The following instructions are all equivalent.
They declare x to be an object of type string, and assign the string high school to it:
std::string x;
string x= high; string y= school; string z; z=x+y; cout<<z=<<z<<endl; z =z+ was fun; cout<<z=<<z<<endl;
Output: z=highschool
z= highschool was fun
x += y; is equivalent to x=x+y; where y can be a string object, a C-style string variable, a char variable, a double-quoted string, or a single-quoted char.
+= Examples
the following operators: ==, !=, <, <=, >, >= The comparison is alphabetical The outcome of each comparison is: true or false The comparison works as long as at least x or y is a string object. The other string can be a string object, a C-style string variable, or a double-quoted string.
characters If x is a string object, and you wish to obtain the value of the k-th character in the string, you write: x[k];
string x= high; char c=x[0]; // c is h c=x[1]; // c is i c=x[2]; // c is g
is, and assigns it to str. istream& operator>>( istream& is, string& str); This << function writes the value of str to the output stream os ostream& operator<<( ostream& os, const string& str); getline inputs a string value. Input stops at end of line by default
in it):
bool x.empty();
characters in x For example, rod is a substring of product substr returns a substring without modifying the string to which the function was applied. Define the substring as index position up to length The default value for position is 0 The default value of the length is x.length(
substr Examples
string strA = "123456789"; strB = strA.substr(2,3); // value is "345" strC = strA.substr(2); // value is "3456789"
Replace Example
// value is "1BBB45"
want to delete/erase the characters in the range [pos,pos+len) in x. To do so, write: x.erase(pos,len); The default value of len is the x.length( )
x.erase(pos); // erases x[pos..end-1]
The default value for pos is 0 To erase the whole string of x, do:
x.clear( );
Examples of erase
want to search for a string y in x. To do so, write: int startLoc = x.find(y); This method returns the starting index of the leftmost occurrence of y in x, if any occurrence exists; otherwise, the method returns the length of x (string::npos). To search starting from a position pos, do
int startLoc = x.find(y, pos);
do
argument y can be a string object, a C-style string variable, double-quoted string, a char variable, or a single-quoted char.
An Example
string x=FROM:ayoussef@gwu.edu; int colonPos=x.find(:); string prefix=x.substr(0,colonPos); //=FROM string suffix = x. substr(colonPos+1); cout<<-This message is from <<suffix<<endl;
several string processing functions Some of the more commonly used functions in that library are presented in the next two slides In those functions, most of the arguments are of type char *str. That can be replaced by char str[];
References
www.seas.gwu.edu/~ayoussef/cs103/lecture5.ppt nepsweb.co.uk/pgtcpp/string.htm