Hello all,
Could somebody help me please with my code? I am having trouble with the NumWords() function. I need to be able to count how many words are in my character array. The catch is that counting the number of spaces will not be enough since you can have a consecutive spaces between "words". This is what I have so far.
Code:
// Program Description: This is a header file for the Sentence class
#ifndef SENTENCE_H
#define SENTENCE_H
#include <iostream>
#include <cstring>
// sets number of elements string array can have to constant for easy modification
const int SIZE = 200;
class Sentence
{
public:
Sentence();
Sentence(char *s);
void Input();
char *GetSentence() const;
void Append(char *s);
int Size();
void Clear();
int NumWords();
void Insert(char *s, int index);
void Delete(int index, int num_chars);
private:
char string[SIZE];
int track; // tracking variable
};
#endif
Code:
int Sentence::NumWords()
// returns the number of words in the stored string
{
int word_count;
char *s;
s = string;
while(*s != '\0'){
if ((*s == ' ')||(*s == '\t'))
word_count++;
*s++;
}
return word_count;
}
Thank you in advance.