0

How can i read in an array and then count the number of words that end in punctuation? Thanks for any insight.

4
Contributors
3
Replies
4
Views
9 Years
Discussion Span
Last Post by atish00
0

U can use standard container routine as "vector".

steps:
1. read char
2. check
3. push_back

then "size" of vector will be the length of sentence.

regards

Teddy

0

well, if you just need to count the number of words that end in punctuation, you do not need to store it in an array or a vector first. assuming that words are whitespace separated, read word by word into a std::string till there is nothing more.

std::string word ;
while( std::cin >> word )
{
  // process it
}

to get the last character of the word

if( !word.empty() )
{
   char last_char = word[ word.size()-1 ] ;
   // check if it a punctuation, if yes increment a count
}

to check if a char is a punctuation in the input locale, use std::ispunct (#include <cctype>)

const std::locale& input_locale = std::cin.getloc() ; 
if( std::ispunct( last_char, input_locale ) ) ++count ;

if you also need to store the words read for access later, add each word to a std::vector<string>

0

strlen for counting nuber of words :

for reading and counting I use.


for( i=0 ; a != '\0' ; i++)
{
cout<<a;
length++;
}

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.