// Demo of C++ string class usage #include #include using namespace std; const int SIZE = 100; // prototypes void splitLineByCharacter(string myLine, char delimiter, string substrings[], int & substringCount); int main() { int substringCount, index; string myLine; string substrings[SIZE]; cout << "Enter your separated data: "; getline(cin, myLine); // reads a whole line of text including whitespace characters cout << "You entered address " << myLine << endl; splitLineByCharacter(myLine, '\t', substrings, substringCount); cout << "There are " << substringCount << " delimited strings. The strings are:" << endl; for (index = 0; index < substringCount; index++) { cout << substrings[index] << endl; } // end for } // end main void splitLineByCharacter(string myLine, char delimiter, string substrings[], int & substringCount) { int startOfSubstring, endOfSubstring; substringCount = 0; startOfSubstring = 0; while (true) { if (substringCount == SIZE) { cout << "Array full: string entered was not all processed!" << endl; return; } else { endOfSubstring = myLine.find(delimiter, startOfSubstring); if (endOfSubstring >=0) { substrings[substringCount] = myLine.substr(startOfSubstring, (endOfSubstring - startOfSubstring)); substringCount++; startOfSubstring = endOfSubstring + 1; } else { endOfSubstring = myLine.length(); substrings[substringCount] = myLine.substr(startOfSubstring, (endOfSubstring - startOfSubstring)); substringCount++; return; } // end if } // end if } // end while } // end splitLineByCharacter