#include #include #include using namespace std; void ClassifyLetter(char letter); void IsInString(string word, string pattern); int main() { string word = "Hello, World 123!"; size_t i; cout << "The string \"" << word << "\" is " << word.size() << " characters long." << endl; cout << "The string '" << word << "' is " << word.size() << " characters long." << endl; cout << endl << endl; cout << "The leters Gare:" << endl; for(i=0; i < word.size(); i++) { cout << setw(4) << i << " '" << word[i] << "' "; ClassifyLetter(word[i]); cout << endl; } // lhs = rhs // the thing on the left hand side gets a copy of the thing on the rhs // // x = 3 + 2 // // cout << word[i] cout << endl << endl; IsInString(word, "World"); IsInString(word, ", W"); IsInString(word, "l"); IsInString(word, "nope"); cout << endl << endl; for(i = 0; i < word.size(); i++) { cout << "The substring starting at " << i << " is \"" << word.substr(i) << "\"." << endl; } cout << endl << endl; for(i = 0; i < word.size(); i++) { cout << "The substring starting at 2 for " << i << " letters is \"" << word.substr(2,i) << "\"." << endl; } cout << endl << endl; for(i = 0; i < word.size(); i++) { //word[i] = char(toupper(word[i])); word[i] = static_cast (toupper(word[i])); } cout << "After changing to uppercase the word is \"" << word << "\"." << endl; string phrase = "danger danger danger"; size_t pos; pos = phrase.find("dan"); while (pos != string::npos) { cout << "dan is at position " << pos << endl; pos = phrase.find("dan", pos+1); //pos = phrase.find("dan", ++pos); } /* i++ ( rv = i; i = i + 1 return rv ) ++i ( i = i + 1 return i) */ return 0; } void ClassifyLetter(char letter) { if (isalpha(letter)) { cout << "a letter"; if (isupper(letter)) { cout <<" (upper case)"; } else { cout <<" (lower case)"; } } else if (isdigit(letter)) { cout << "a digit"; } else if (isspace (letter)) { cout << "a whitespace character"; } else if (ispunct(letter)) { cout << "punctuation"; } else { cout << "unknown"; } return; } void IsInString(string word, string pattern){ size_t pos; cout << "Searching for \"" << pattern << "\" in \"" << word << '"' << endl; pos = word.find(pattern); if (pos == string::npos) { cout << "\t\"" << pattern << "\" is not in the string." << endl; } else { cout << "\t\"" << pattern << "\" is in the string at position " << pos << "." << endl; } return; }