How to find the largest and the smallest word in a string with C++

1 Answer

0 votes
#include <iostream>
#include <string>

using namespace std;

int main()
{
	string s("cpp c csharp python java");
	string word, max = "", min = s;

	for (size_t i = 0; i < s.length(); i++)
	{
		if (s[i] != ' ')
		{
			word += s[i];
		}
		else
		{
			if (word.length() < min.length())
				min = word;
			if (word.length() > max.length())
				max = word;
			word = "";
		}
	}
	cout << "The largest word is: " << max << endl;
	cout << "The smallest word is: " << min << endl;

	return 0;
}

/*
run:

The largest word is: csharp
The smallest word is: c

*/

 



answered Mar 21, 2017 by avibootz

Related questions

3 answers 239 views
1 answer 115 views
1 answer 90 views
1 answer 106 views
...