display longest word

Nov 7, 2010 at 11:35am
hi

i am trying to display the longest word in a sentence. first i am trying to display the length of the longest word but it is not displaying the correct length i.e, i have a string s = "This is string" so it displays the length of a longest word as "4" but here it is "6". and secondly i want to display that word as well.

here is my code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <string>

using namespace std;

int main() 
{
	string s = "This is String";
	//int x = 0;
	int counter = 0;
	int max_word = -1;
	int len = s.length();
	string max = " ";
	
	for (int i = 0; i < len; i++)
	{
		if(s[i] != ' ') {
			//max.append(s[i]);
			counter++;
		} 
		else if(s[i] == ' ') {
			if(counter > max_word) {
				max_word = counter;
			}
			counter = 0;
		}
	}
	cout << max_word << " " << len << endl;
	return 0;
}
Nov 7, 2010 at 2:58pm
While iterating through the string:

1. If s[i] is not a space, workingWord += s[i]
2 If s[i] is a space or at the end of the string, and workingWord size > maxWord size, let maxWord=workingWord and reset workingWord.
3. if s[i] is a space and workingWord size < maxWord size, then just reset workingWord.


If you don't check for punctuation as well as space, it will be included as part of the word.
Last edited on Nov 7, 2010 at 3:02pm
Nov 7, 2010 at 3:35pm
i ll also check for punctuation and space
Nov 7, 2010 at 7:27pm
i have changed my code to this but it is displaying this output
 
thisis


here is my code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <string>

using namespace std;

int main() 
{
	string s = "this is string";
	//int x = 0;
	int counter = 0;
	int max_word = -1;
	int len = s.length();
	string workingWord = "";
	string longWord = "";
	int workingLen;
	
	for (int i = 0; i <= len; i++)
	{
		if(s[i] != ' ') {
			//max.append(s[i]);
			workingWord += s[i];
			cout << workingWord << endl;
			workingLen = workingWord.length();
			counter++;
		}
		else if(s[i] == ' ') {
			if(workingLen > max_word) {
				max_word = counter;
				longWord = workingWord;
			}
			counter = 0;
		}
	}
	cout << longWord << endl;
	return 0;
}

help please
Last edited on Nov 7, 2010 at 7:28pm
Nov 8, 2010 at 12:30am
You are not setting workingWord back to "" after you make longWord=workingWord.


Your for loop will go farther than it needs to: the last usable character in your string is s[(s.size-1)].
In other words, your string array starts from 0 not 1--the [i] in s[i] is an address, and s.length() is a count.


For such a small program, you're better off using as few variables as possible. For example workingLen is the same as workingWord.length(), that way you're keeping it simple.

If you weren't almost finished, I'd ask you t o try to use only 4 variables:

string s,
string workingWord
string longWord
unsigned int i

Nov 8, 2010 at 9:05am
i did this
1
2
longWord = workingWord;
workingWord = "";


but it displays only is
 
is
Nov 8, 2010 at 11:14am
workingWord="" should go together with counter=0. If you have detected a space it's time to start a new working word.
And you shouldn't use else if. What if s[i] is not a space but it is the end of the string? Just use if
You should also check if it is the end of the string, if (s[i] =' ' || i=(len-1)) will check for the end of a word and the end of the string (punctuation included in the word).
Nov 8, 2010 at 1:58pm
If you added ' ' after "This is String"
like this:"This is String ",you'll get 6
why?
think about it

here is my code
output
6 String


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <string>

using namespace std;

int main() 
{
	string s = "This is String";
	//int x = 0;
	int counter = 0;
	int max_word = -1;
	int len = s.length();
        string max = " ";
	
	for (int i = 0; i < len; i++)
	{
		if(s[i] != ' ') {
			//max.append(s[i]);
			counter++;
		} 
		if(s[i] == ' ' || i == len - 1) {
			if(counter > max_word) {
				max_word = counter;
                                //here,handle the end of the string
				if(i == len - 1) 
                                      max = s.substr(i + 1 - max_word, max_word);
                                else
				      max = s.substr(i - max_word, max_word);
             }
			
			counter = 0;
		}
}
    cout << max_word << " " << max << endl;
	return 0;
}


haha
enjoy
Last edited on Nov 8, 2010 at 2:58pm
Nov 9, 2010 at 2:05am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>
using namespace std;

string input="This is string";
string workingWord="";  //working string
string maxWord="";  //biggest word
unsigned i=0;          //string iterator

int main()
{
    for (i=0; i < input.size(); i++)
    {
        //If it's not a space, add it to the word, if it is, reset workingWord.
        if (input[i] != ' ')    workingWord += input[i]; else workingWord="";

        //if workingword > maxWord , you have a new maxWord.
        if (workingWord.size() > maxWord.size()) maxWord=workingWord;

    }
    cout << "\n" << maxWord << "\t" << maxWord.size();
    return 0;
}


Forgive me, I'm still a noob.
Last edited on Nov 9, 2010 at 2:56am
Topic archived. No new replies allowed.