#include <iostream> #include <string> using namespace std; using std::string; string firstWord(string str); int main() { string str; cout << "Enter the string: " << endl; getline(cin, str); cout << firstWord(str) << endl; return 0; } string firstWord(string str) { string result = ""; for (int i = 0; !isspace(str[i]) && str.size(); i++) { result += str[i]; } return result; }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter