I'm in an entry level c++ class. My professor wants us to write a function that counts the number of words in a string. The stipulation to this is, A WORD IS DEFINED TO BE ANT CONTIGUOUS GROUP OF ASCII CHARS THAT START WITH THE LETTERS A THROUGH Z AND MUST BE UPPER CASE. For example, the sentence: "the Dog At5674 654 Mypaper", there would be 3 words considered in this sentence. The 3 words would be Dog, At5674, and Mypaper. Right now I'm really stumped, My c++ code isn't working :sad: I can't figure what I am missing or doing wrong to pring the result out.
Here is my function:
void wordcount()
{
string text = "the Dog At5674 654 Mypaper";
const int x=3;;
int index[x];
int count=0,i,j=0,k;
for(i=0; i<text.length(); i++)
{
if(isupper(text[i]))
{
count++; //counts number of times a upper case letter is found.
index[i]=i; //stores the position of that capitol letter.
}
}
while(sizeof(index))
{
for(k=index[j]; !isspace(text[k]) || text[k] != '\n'; k++)
{
cout<<text[k];
}
j++;
}
}
I've found other codes for word count, but you have to keep in mind that this is entry level c++ and we have not touched on vectors, class, istreams, etc.
Thank for the help.