Apr 3, 2012 at 7:58pm UTC
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 :)
Apr 3, 2012 at 8:35pm UTC
Last edited on Apr 3, 2012 at 8:37pm UTC
Apr 3, 2012 at 8:54pm UTC
no but this is not two words, the sentence thats entered, its size is arbitrary.
Apr 3, 2012 at 9:02pm UTC
do you need to isolate each word?
do
for(int i = 0; !isspace(c); i++){//ctype.h library
word[i] = string[i];
}
Apr 3, 2012 at 9:15pm UTC
how do i run the comparison through all the words typed in?
Apr 3, 2012 at 9:29pm UTC
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 Apr 3, 2012 at 9:32pm UTC
Apr 3, 2012 at 10:26pm UTC
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.
Apr 4, 2012 at 2:57pm UTC
hi i tested this code yesterday and it didnt seem to work...i failed this assignment. i would just like to know why.
Apr 4, 2012 at 5:45pm UTC
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 Apr 4, 2012 at 5:46pm UTC
Apr 4, 2012 at 5:58pm UTC
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 Apr 4, 2012 at 6:03pm UTC
Apr 4, 2012 at 6:05pm UTC
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.
Apr 4, 2012 at 6:08pm UTC
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 Apr 4, 2012 at 6:47pm UTC
Apr 4, 2012 at 7:08pm UTC
hi, yes. so i need to re-code this into something that doesnt require any external libraries...can anyone help?
Apr 4, 2012 at 7:44pm UTC
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()
Apr 4, 2012 at 9:05pm UTC
because i need external header files for those functions...
Apr 5, 2012 at 11:28am UTC
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 Apr 5, 2012 at 11:28am UTC
Apr 5, 2012 at 1:21pm UTC
i have to use standard c, not c++. ctype
is allowed!
Last edited on Apr 5, 2012 at 1:39pm UTC