find the smallest word in the string.

Pages: 12
hi everyone, i need help with a very simple task. i have to enter a sentence and simply find the smallest word in the sentence. i tried using strtok, and stuff, but ultimately, nothing got figured out. any help would be appreciated :)
If you are using the strings library:
http://cplusplus.com/reference/string/

Then just use .size():
http://cplusplus.com/reference/string/string/size/

Example:
1
2
3
4
5
6
7
std::string FindSmallestString(std::string word1, std::string word2)
{
    if ( word1.size() < word2.size() )
        return word1;
    else
        return word2;
}
Last edited on
no but this is not two words, the sentence thats entered, its size is arbitrary.
do you need to isolate each word?
do

for(int i = 0; !isspace(c); i++){//ctype.h library
word[i] = string[i];
}
how do i run the comparison through all the words typed in?
It is simply to do with the standard class std::istringstream. But I am afraid that you do not use it yet.

For example

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
#include	<iostream>
#include	<iterator>
#include	<sstream>
#include	<string>

int main()
{
	const std::string s( "This is a test" );

	std::istringstream is( s );

	std::string minimum;
	bool first = true;

	for ( auto it = std::istream_iterator<std::string>( is );
		  it != std::istream_iterator<std::string>();
		  ++it )
	{
		if ( first ) first = false, minimum = *it;
		else if ( ( *it ).size() < minimum.size() ) minimum = *it;
	}

	std::cout << "minimum = \"" << minimum << "\"" << std::endl;

	return 0;
}


Another way is to use std::string functions as substr, find and so on.
Last edited on
find seems like a good way to go. Go through your string looking for spaces, and then compare the space you find with the next space you find. The smallest amount is your shortest word. Then use those positions to display the word.
hi i tested this code yesterday and it didnt seem to work...i failed this assignment. i would just like to know why.
What code did you test?
i tried yours. except i replaced std:: with using std (it caused errors for some reason) it still did not work. i also tried this:
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <string>
#include <vector>
#include <conio.h>
#include "stdafx.h"
 
using std::cout;
using std::cin;
using std::endl;
using std::vector;
using std::string;
 
 
int main(){

    cout << "Please give me a list of words:" << endl;
     
    string x;
    vector<string> words;
     
    //adds the input to the vector
    while (cin >> x){
          words.push_back(x);
          }
     
 
    if (words.size() == 0){
       cout << "I didn't receive any input." << endl;
        
       cout << "Press any key to continue...";
       _getch();
       return 1;
       }
 
    string longest = words[0];
    string shortest = words[0];
     

    for(int i = 1; i != words.size(); i++){
            if(words[i].length() < shortest.length()){
                                 shortest = words[i];
            }
            if(words[i].length() > longest.length()){
                                 longest = words[i];
            }
             
    }
     
    
    cout << "Shortest: " << shortest << endl;
    cout << "Longest: " << longest << endl;
    
    cout << "Press enter to continue...";
    _getch();
    return 0;
}


did not work, i enter the words and spaces, and the program just doesnt respond...
Last edited on
cocopuff


i tried yours. except i replaced std:: with using std (it caused errors for some reason)


My code does not conains errors. You even could not simply copy and paste it!

If you are saying that there were errors then show it!
how do i probably parse the string using getline??

std::cin.getline(s) returns an errors and so does std::getline(cin,s);

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
37
38
39
40
41
42
43
44
45
46
47
48
49

#include <iostream>
#include <string>
#include <vector>
#include <conio.h>
#include <iterator>
#include <sstream>
#include "stdafx.h"
 
using std::cout;
using std::cin;
using std::endl;
using std::vector;
using std::string;
 
 

using namespace std;


int main()
{

	

	const std::string s;


	std::cout<<"Please enter your sentence"<<std::endl;

	std::getline(cin,s);
	std::istringstream is( s );

	std::string minimum;
	bool first = true;

	for ( auto it = std::istream_iterator<std::string>( is );
		  it != std::istream_iterator<std::string>();
		  ++it )
	{
		if ( first ) first = false, minimum = *it;
		else if ( ( *it ).size() < minimum.size() ) minimum = *it;
	}

	std::cout << "minimum = \"" << minimum << "\"" << std::endl;

	getch();
	return 0;
}
Last edited on
Where do you find getline() in my code?!

It is your code that is invalid!

And if you got en error in getline then you should show that code.
i didnt! i just added it into it to parse any entered string...i am asking how to make it be parsed ..(the string)

UPDATE: nevermind, found it out. but now i have to re-do all of this without any external functions....:/
Last edited on
hi, yes. so i need to re-code this into something that doesnt require any external libraries...can anyone help?
Hey,
why dont you try it with .substr()? find the whitespaces, and separate the sentence into words? Than you can count each word with .size()
because i need external header files for those functions...
Can anyone help me????
string, vector, getline etc. is standard C++ and only standard headers is needed to use them.

You need to include standard headers to use strtok too. What headers exactly are you allowed to use?
Last edited on
i have to use standard c, not c++. ctype is allowed!
Last edited on
Pages: 12