working with strings

I have a string that i read from a file with the next code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
 
int main(int argc, char **argv){
 
    ifstream ifs("exercise.txt");
    if (!ifs.bad())
    {
        // u try to see if it reads from the file
        cout << ifs.rdbuf();
 
        ifs.close();
		cin.get();
    }
}

I have to find now the shortest and the longest word from the sentence.
I just don't know how to access the different words of the sentance
You'd have to tokenize the input (basically, split up the string into words and put each of the words into a string array) and then check each word for it's length. After you have the words, finding the shortest or longest string is trivial, tokenizing might be another story. I don't remember there being a standard tokenizer for std::string, so you'd have to write one yourself (or use one of someone else).
you are gonna need to read a line at the time and then loop thru each line and break it up into each separate word and just do length comparison on them.
Topic archived. No new replies allowed.