I made a few improvements…

/*
 
 More with strings
 */
 
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
 
using namespace std;
 
int main() {
    string aLine;
 
    // used later:
    string firstPart;
    string lastPart;
    string scrambled;
    string emphasis;
 
    // More on strings
    //
    cout << "First let's read in some text. Use getline so that we get a whole line and not just the first word: ";
    getline(cin,aLine);
    cout << endl;
    cout << "You typed \"" << aLine << "\"" << endl;
    cout << endl;
 
    cout << "If you typed exactly \"C++ is cool\", you MIGHT get an A in the class. Let's check for that:" << endl;
    cout << endl;
 
    if ("C++ is cool" == aLine) {
        cout << "You MIGHT get an A" << endl;
    }
    cout << endl;
 
    cout << "Even if you merely INCLUDED the text \"C++ is cool\", you MIGHT get an A in the class. Let's check for that:" << endl;
    cout << endl;
 
    // string::npos is returned by find if the string is NOT found, so if the npos is NOT returned by find, it must be there some where!
    if (string::npos != aLine.find("C++ is cool")) {
        cout << "You MIGHT get an A" << endl;
    }
    cout << endl;
 
    cout << "Suppose we wanted to know where you said it?" << endl;
    cout << endl;
 
    // We should have defined a const string for this very important string at the start of this function, but let's do it here:
    const string importantString = "C++ is cool";
 
    // IMPORTANT, it turns out that c++ does not like:
    // int index = aLine.find(importantString);
    // because it knows that the position returned can never be negative! It will give a warning about this.
    // so we need to put the value in an unsigned variable, you can use "unsigned long" or "size_t"
    size_t index = aLine.find(importantString);
    if (string::npos == index) {
        cout << "\"" << importantString << "\" not found." << endl;
    }
    else {
        cout << "\"" << importantString << "\" found at index " << index << "." << endl;
    }
    cout << endl;
 
    cout << "The same but using unsigned long:" << endl;
    cout << endl;
 
    unsigned long anotherIndex = aLine.find(importantString);
    if (string::npos == anotherIndex) {
        cout << "\"" << importantString << "\" not found." << endl;
    }
    else {
        cout << "\"" << importantString << "\" found at index " << anotherIndex << "." << endl;
    }
    cout << endl;
 
 
    cout << "Suppose we wanted to count how many time you said it?" << endl;
    cout << endl;
    unsigned long startingAt = 0;
    int count = 0;
    index = 0;
    // there are many ways to structure this loop here is one:
    while (true) {
        index = aLine.find(importantString,startingAt);
        if (string::npos != index) {
            cout << "found it" << endl;
            ++count;
            startingAt = index + importantString.length();
        }
        else {
            break;
        }
    }
    cout << "You said \"" << importantString << "\" " << count << " times." << endl;
    cout << endl;
 
    cout << "Suppose we wanted to scramble your sentence?" << endl;
    cout << endl;
    anotherIndex = aLine.find(importantString);
    if (string::npos == anotherIndex) {
        cout << "\"" << importantString << "\" not found." << endl;
    }
    else {
        // remember that substr wants the starting point and (optionally) the length to extract
        firstPart = aLine.substr(0,anotherIndex);
        // skip over the important string because we know what that is!
        lastPart = aLine.substr(anotherIndex+importantString.length()); // no length to extract because we want everything to the end
        scrambled = lastPart + firstPart + importantString;
        cout << "Scrambled String: \"" << scrambled << "\"." << endl;
    }
    cout << endl;
 
    cout << "If you type \"first part C++ is cool last part\", it is a bit awkward with the spaces" << endl;
    cout << "There are many ways to improve on this, but lets just include a space in the important string, like:" << endl;
    const string betterImportantString = "C++ is cool "; // I have to make a new const, I can't change the other one. Note the space after "cool"
    cout << "\"" << betterImportantString << "\"" << endl;
    cout << endl;
    anotherIndex = aLine.find(betterImportantString);
    if (string::npos == anotherIndex) {
        cout << "\"" << betterImportantString << "\" not found." << endl;
    }
    else {
        // remember that substr wants the starting point and (optionally) the length to extract
        firstPart = aLine.substr(0,anotherIndex);
        // skip over the important string because we know what that is!
        lastPart = aLine.substr(anotherIndex+betterImportantString.length()); // no length to extract because we want everything to the end
        scrambled = lastPart + firstPart + betterImportantString;
        cout << "Slightly better scrambled String: \"" << scrambled << "\"." << endl;
        cout << "We could do more to try to clean it up, but this might not be a good idea depending on what the user typed" << endl;
        scrambled = lastPart + string(" ") + firstPart + betterImportantString.substr(0,betterImportantString.length()-1);
        cout << "POSSIBLY better scrambled String: \"" << scrambled << "\"." << endl;
        cout << "Maybe even better, let's let's force the first character to be uppercase and add a period." << endl;
        // if we wanted it to be lower we would use tolower()
        if (0 < lastPart.length()) {
            lastPart[0] = toupper(lastPart.at(0));
        }
        scrambled = lastPart + string(" ") + firstPart + betterImportantString.substr(0,betterImportantString.length()-1) + string(".");
        cout << "I sort of like this one: \"" << scrambled << "\"." << endl;
    }
    cout << endl;
 
    //system("pause");
    return 0;
}
cs-142/more-with-strings.txt · Last modified: 2017/03/17 20:27 by kseppi
Back to top
CC Attribution-Share Alike 4.0 International
chimeric.de = chi`s home Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0