Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
cs-142:more-with-strings [2017/03/18 01:40]
kseppi
cs-142:more-with-strings [2017/03/18 02:27] (current)
kseppi
Line 1: Line 1:
 +I made a few improvements...
 <code cpp> <code cpp>
- 
 /* /*
    
Line 101: Line 101:
     cout << "​Suppose we wanted to scramble your sentence?"​ << endl;     cout << "​Suppose we wanted to scramble your sentence?"​ << endl;
     cout << endl;     cout << endl;
-    ​index = aLine.find(importantString);​+    ​anotherIndex ​= aLine.find(importantString);​
     if (string::​npos == anotherIndex) {     if (string::​npos == anotherIndex) {
         cout << "​\""​ << importantString << "​\"​ not found."​ << endl;         cout << "​\""​ << importantString << "​\"​ not found."​ << endl;
Line 107: Line 107:
     else {     else {
         // remember that substr wants the starting point and (optionally) the length to extract         // remember that substr wants the starting point and (optionally) the length to extract
-        firstPart = aLine.substr(0,​index);+        firstPart = aLine.substr(0,​anotherIndex);
         // skip over the important string because we know what that is!         // skip over the important string because we know what that is!
-        lastPart = aLine.substr(index+importantString.length());​ // no length to extract because we want everything to the end+        lastPart = aLine.substr(anotherIndex+importantString.length());​ // no length to extract because we want everything to the end
         scrambled = lastPart + firstPart + importantString;​         scrambled = lastPart + firstPart + importantString;​
         cout << "​Scrambled String: \""​ << scrambled << "​\"​."​ << endl;         cout << "​Scrambled String: \""​ << scrambled << "​\"​."​ << endl;
Line 120: Line 120:
     cout << "​\""​ << betterImportantString << "​\""​ << endl;     cout << "​\""​ << betterImportantString << "​\""​ << endl;
     cout << endl;     cout << endl;
-    ​index = aLine.find(betterImportantString);​ +    ​anotherIndex ​= aLine.find(betterImportantString);​ 
-    if (string::​npos == index) {+    if (string::​npos == anotherIndex) {
         cout << "​\""​ << betterImportantString << "​\"​ not found."​ << endl;         cout << "​\""​ << betterImportantString << "​\"​ not found."​ << endl;
     }     }
     else {     else {
         // remember that substr wants the starting point and (optionally) the length to extract         // remember that substr wants the starting point and (optionally) the length to extract
-        firstPart = aLine.substr(0,​index);+        firstPart = aLine.substr(0,​anotherIndex);
         // skip over the important string because we know what that is!         // skip over the important string because we know what that is!
-        lastPart = aLine.substr(index+betterImportantString.length());​ // no length to extract because we want everything to the end+        lastPart = aLine.substr(anotherIndex+betterImportantString.length());​ // no length to extract because we want everything to the end
         scrambled = lastPart + firstPart + betterImportantString;​         scrambled = lastPart + firstPart + betterImportantString;​
         cout << "​Slightly better scrambled String: \""​ << scrambled << "​\"​."​ << endl;         cout << "​Slightly better scrambled String: \""​ << scrambled << "​\"​."​ << endl;
cs-142/more-with-strings.txt · Last modified: 2017/03/18 02: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