#include <iostream>
#include <string>
#include <fstream>
usingnamespace std;
int main()
{
fstream indiaFile("india.txt");
if(!indiaFile.is_open()) //File not open, not much we can do.
return -1;
string fileContents="";
int wordsWithS=0;
//Reads file.
string inLine=""; //Will be changed by reading the file.
while( getline(indiaFile, inLine))
{
indiaFile >> inLine;
fileContents+=inLine;
}
//Parses file contents.
int pos1=0; //Position data for substr().
for(int i=0; i != fileContents.length(); i++)
{
if(fileContents[i] == ' ' || fileContents[i] == '\n' || i == fileContents.length()-1) //Found a Separator Character.
{
string word="";
if(pos1 == 0)
word=fileContents.substr(pos1, i);
if(pos1 > 0)
{
word=fileContents.substr(pos1, i).erase(0,1); //Removes the beginning space/Char.
if((fileContents.length()-1)-i > 0) //If not last word (Found by positioning of i and the length of FileContents), erase last Chars
word=word.erase((i-pos1)-1, word.length()); //Erases last Chars to 'capture' only the needed word and not any other bits.
}
cout <<"Found word: "<<word<<"\n";
if(word[word.length()-1] == 's') //Found a word, increase the counter.
wordsWithS++;
pos1=i; //Updates position in For loop.
}
}
cout <<"Words ending with S: "<<wordsWithS; //Display results.
}