count number of sentences in a string

Hi, I am writing a program that counts the number of sentences in a string.
I count the number of '.' '?' '!'. However, there are Mr. Mrs. PhD. Dr. ..... situations. Any help please?

1
2
3
4
5
6
7
 int number_of_sentences = 0;
  for(unsigned int i=0; i <= text.length()-1; i++){
    if(text[i] == '.' || text[i] == '?' ||text[i] == '!'){
      ++number_of_sentences;
    }
  }
  return number_of_sentences;
Last edited on
You can first remove all those cases you do not want, and then use your existing code to count punctuation.

By the way, there is a standard function for detecting if a character is punctuation:
http://www.cplusplus.com/reference/cctype/ispunct/
http://en.cppreference.com/w/cpp/string/byte/ispunct
It includes commas, however.
Last edited on
if I use ispunct, the program will count all the punctuation. The text I have to read is more than 3000 characters. I cannot eliminate all other punctuations that I dont need to count.
3000 characters is tiny, even a bad implementation of std::string will whizz through it. Don't worry about efficiency until after the program works.
Topic archived. No new replies allowed.