I thought about using a struct and a vector to read character by character and when I reach a space a store it as a value let's say 0.
So what about this approach?

Based on what arkoenig has told you, you could read in the words one by one into a vector, as the extraction operator(>>) is delimited on space (probably would have to get rid of punctuation first). That way you wouldn't have to keep track of spaces at all. You'd just end up with a vector full of words and you could perform whatever manipulations you needed to process them.

Not sure what'd you'd need a struct for in that mix.

I needed the struct for the white spaces but i just don't know how to read different word from the sentence

Well, you'll need a while loop and the >> operator.

std::string tempstring;
while(ifs >> tempstring)
   Add tempstring to vector

Play around with that with different text in your file, like

"Does it tellthewords apart what happens      if there's a big space, or punctuation!"

I came up with a solution.
I put the whole sentence in an array and i read char by char with strlen.
And then I count and I find the longest word.
the problem is that I just don't know yet how to find the shortest one.

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
	char mystring[2000];
  //ofstream myfile ("exercise.txt");
  ifstream infile("exercise.txt");
  // You should create the file in the directory with the executable before hand.
  if ( infile.is_open() )
  {
   //ifstream infile("exercise.txt"); // Don't open inside a block. Will dissapear at itsend.
	  //for(int i=0; i<mystring.size(); i++)
		  infile.getline(mystring, 2000); 
   cout << mystring; // cout uses <<, cin uses >>
  }
  else cout << "Unable to open file";
int count =0;
  int longest_word = -1;
  int shortest_word = 1;
  int posi=0;
 
  int length = (strlen(mystring));
mystring[length]= ' ';
 
  cout<<length<<endl;
 
  for(int i=0; i<=length; i++)
  {
      if(mystring[i] !=' ')
      {
          count++;
      } 
      else if(mystring[i]==' ')
      {
          if(count > longest_word)
          {
              longest_word = count;
              posi=i;
          }
          count = 0;
 
      }
 
 
  }  
  cout <<"Longest word:";
   for(int k=posi-longest_word; k<posi; k++)
  {cout<<mystring[k];}
  //myfile.close();
  infile.close();
  cin.get();
  return 0;
}

By doing something similar to what you did for the longest word?

if(count < shortest_word)
{    
    shortest_word = count;
    short_posi  = i;
}

Start longest and shortest initially as the length of the first word. If you have shortest_count = 1 then it's going to be tough to find something shorter than that.

I made this and it's compiling fine but i have some problems with the initialization of the variables.
int longest_word = 2;
int shortest_word = 1;
when I do this some strange characters appear on my screen.

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
	char mystring[2000];
  //ofstream myfile ("exercise.txt");
  ifstream infile("exercise.txt");
  // You should create the file in the directory with the executable before hand.
  if ( infile.is_open() )
  {
   //ifstream infile("exercise.txt"); // Don't open inside a block. Will dissapear at itsend.
	  //for(int i=0; i<mystring.size(); i++)
		  infile.getline(mystring, 2000); 
   cout << mystring; // cout uses <<, cin uses >>
  }
  else cout << "Unable to open file";
int count =0;
  int longest_word = 2;
  int shortest_word = 1;
  int posi=0;
 
  int length = (strlen(mystring));
mystring[length]= ' ';
 
  cout<<length<<endl;
 
  for(int i=0; i<=length; i++)
  {
      if(mystring[i] !=' ')
      {
          count++;
      } 
      else if(mystring[i]==' ')
      {
          if(count > longest_word)
          {
              longest_word = count;
              posi=i;
          }
          count = 0;
 
      }
 for(int i=0; i<=length; i++)
  {
      if(mystring[i] !=' ')
      {
          count++;
      } 
      else if(mystring[i]==' ')
      {
          if(count < shortest_word)
          {
			  shortest_word = count;
              posi=i;
          }
          count = 0;
 
	  }
 }
  }  
  cout <<"Longest word:";
   for(int k=posi-longest_word; k<posi; k++)
  {cout<<mystring[k];}
   cout <<"Shortest word:";
   for(int k=posi-shortest_word; k<posi; k++)
  {cout<<mystring[k];}
  //myfile.close();
  infile.close();
  cin.get();
  return 0;
}

Think about it, you can find the shortest and the longest all in one pass. Name your variable that marks the position of the shortest word as something different so it doesn't overwrite the one for the longest word.

Sorry but I do not understand

Line 56, rename posi to posi_short or something, as it's overwriting the value of posi that you found for the longest word in the previous for loop.

I was just saying that you don't need a second for loop to test for the shortest value:

for( )
{
   if(count is longer than longest)
   {
        longest = count;
        posi = i;
   }

   if(count is less than shortest)
   { 
        shortest = count;
        posi_short = i;
   }

}

Save yourself from making two passes over the data.

I did this now is taking me the shortest word ot of the sentence and displaying me the rest of the sentence.

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
	char mystring[2000];
  //ofstream myfile ("exercise.txt");
  ifstream infile("exercise.txt");
  // You should create the file in the directory with the executable before hand.
  if ( infile.is_open() )
  {
   //ifstream infile("exercise.txt"); // Don't open inside a block. Will dissapear at itsend.
	  //for(int i=0; i<mystring.size(); i++)
		  infile.getline(mystring, 2000); 
   cout << mystring; // cout uses <<, cin uses >>
  }
  else cout << "Unable to open file";
int count =0;
  int longest_word = 3;
  int shortest_word = 2;
  int posi=0;
 int posi_short=0;
  int length = (strlen(mystring));
mystring[length]= ' ';
 
  cout<<length<<endl;
 
  for(int i=0; i<=length; i++)
  {
      if(mystring[i] !=' ')
      {
          count++;
      } 
      else if(mystring[i]==' ')
      {
          if(count > longest_word)
          {
              longest_word = count;
              posi=i;
          }
          count = 0;
 
      }
	  if(count < shortest_word)
          {
			  shortest_word = count;
              posi_short=i;
          }

  }  
  cout <<"Longest word:";
   for(int k=posi-longest_word; k<posi; k++)
  {cout<<mystring[k];}
   cout <<"Shortest word:";
   for(int k=posi_short-shortest_word; k<posi; k++)
  {cout<<mystring[k];}
  //myfile.close();
  infile.close();
  cin.get();
  return 0;
}

and also I would like to know how do I initialize the variables with the shortest and the longest word

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.