Word count - ignoring/subtracting double spaces

General Tech Learning Aids/Tools 3 years ago

2.57K 2 0 0 0

User submissions are the sole responsibility of contributors, with TuteeHUB disclaiming liability for accuracy, copyrights, or consequences of use; content is for informational purposes only and not professional advice.

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 3 years ago

 

I'm trying to learn some coding to broaden my scope of knowledge, and I've seemed to run into a bit of a conundrum.

I'm trying to create a program to output the number of characters, digits, punctuation, spaces, words and lines that are being read in from a file.

Here is the text file I am reading in.

  See Jack run. Jack can run fast. Jack runs after the cat. The cat's fur is black. See Jack catch the cat.
  Jack says, "I caught the cat."
  The cat says, "Meow!"
  Jack has caught 1 meowing cat. Jack wants 5 cats, but can't find any more.

Here is my code

  #include 
  #include 

  using namespace std;

  int main()
  {
  ifstream lab3;
  string word;
  lab3.open("lab3.txt");
  int countletters=0,countnum=0,countpunc=0,countspace=0,words=0,line=0;
  char character;
  if(!lab3)
  {
  cout << "Could not open file" << endl;
  return 1;
  }
  while(lab3.get(character) && !lab3.eof())
  {
  if(isalpha(character))
  {
  countletters++;
  }
  if (isdigit(character))
  {
  countnum++;
  }
  if (ispunct(character))
  {
  countpunc++;
  }
  if (isspace(character))
  {
  countspace++;
  }
  if (isalpha(character) && (isspace(character++) || ispunct(character++)))
  {
  words++;
  }
  if(character=='\n')
  {
  line++;
  }
  }
  cout << "There are " << countletters << " letters." << endl;
  cout << "There are " << countnum << " numbers." << endl;
  cout << "There are " << countpunc << " punctuations." << endl;
  cout 
                                                
0 views
0 shares

profilepic.png
manpreet 3 years ago

Because your method of getting the next character is by lab3.get(character), incrementing the character in your word count check is not going to get the next character, only alter the value of the character you have.

Rather than trying to 'look ahead', consider preserving the last character read and checking against it to detect the end of the word on the following iteration.

if (ispunct(character)) { countpunc++; if (isaalpha(prevchar)) { words++; }
} if (isspace(character)) { countspace++; if (isaalpha(prevchar)) { words++; }
}

with prevchar initialized to zero before the loop starts, and then set equal to character inside the loop after all of your checks.

Also note that your numbers check is really capturing digits, so a value of 10 in your data will count as 2 numbers in your output rather than one.


0 views   0 shares

No matter what stage you're at in your education or career, TuteeHUB will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.

Similar Forum