How to count punctuation marks in an array

I have seen many ways to REMOVE punctuation marks in an array, but how would one go about it when implementing it in a function? This is what I have so far

1
2
3
4
5
6
7
8
9
10
11
12
  int countAllPunctuation(const string array[], int n)
{
	int punctuation = 0;
for (int i = 1; i < n; i++)
{
punctuation = i;
}

	

	return punctuation;
}
use the count function
http://www.cplusplus.com/reference/algorithm/count/

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;

int main()
{
    char arr[] = {'H', 'e', 'l', 'l', 'o','!', ' ', 'w', 'o', 'r', 'l', 'd', '!', '\0'};

    std::cout << "there are " << std::count(arr, arr + strlen(arr), '!') << " !'s in this array" << std::endl;
    return 0;
}
But how would it be implemented with an array of strings?

Such as

string array[4] = { "howard.", "lol.", "howard", "hi." };
If it doesn't have to be an array:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
using std::cout;

#include <locale> 
using std::ispunct;

#include <string>
using std::string;

int main()
{
    int numPunct = 0;

    string sentence = "Howard, lol! You ate - MY DUCK!";
    
     for (auto it = sentence.begin(); it!=sentence.end(); ++it)
     {
       if (ispunct(*it)) 
       {
           ++numPunct;
       }
     }
    
    cout << numPunct << " punctuation marks found";
    return 0;
}


Here would be an example using the same with an array:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
using std::cout;

#include <locale> 
using std::ispunct;

#include <string>
using std::string;

int main()
{
    const int NUMELS = 4;
    int numPunct = 0;
    
    string puncts[NUMELS] = { "howard", "lol.", "howard", "hi." }; 
    
    for (int i = 0; i < NUMELS; i++)
    {
     for (auto it = puncts[i].begin(); it!=puncts[i].end(); ++it)
     {
       if (std::ispunct(*it)) 
       {
           ++numPunct;
       }
      }
    }
    
    cout << numPunct << " punctuation marks found";
   return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <cctype>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

int numPunct( const vector<string> &A )
{
   int n = 0;
   for ( const string &s : A ) n += count_if( s.begin(), s.end(), ::ispunct );
   return n;
}

int main()
{
   vector<string> A = { "howard", "lol.", "howard", "hi." };
   cout << "Punctuation count: " << numPunct( A ) << '\n';
}

Topic archived. No new replies allowed.