forked from portfoliocourses/cplusplus-example-code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcount_char_occurrences.cpp
68 lines (57 loc) · 2.18 KB
/
count_char_occurrences.cpp
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*******************************************************************************
*
* Program: Count The Occurrences Of A Character In A String
*
* Description: Program to count the occurrences of a character in a string
* using C++.
*
* YouTube Lesson: https://www.youtube.com/watch?v=vdZRW7WkD9Y
*
* Author: Kevin Browne @ https://portfoliocourses.com
*
*******************************************************************************/
#include <iostream>
#include <algorithm>
using namespace std;
// returns the number of occurrences of the character 'character' in the string
// 'text'
long count_occurrences(string text, char character)
{
// use the count() function of the algorithm library to count the occurrences
// of the character in the range text.begin() to text.end(), i.e. the
// beginning to the end of the string
return count(text.begin(), text.end(), character);
}
int main()
{
// Declare variables to store the string and the character
string text;
char character;
// Prompt the user to enter the string, store it into text
cout << "String: ";
getline(cin, text);
// Prompt the user to enter the character, store it into character
cout << "Character: ";
cin >> character;
// We could use a for-loop with counter variable i that goes from 0 to the
// length of the string, and check the character in the string at each
// index in the string text[i] to see if it matches the character,
// incrementing a count of occurrences each time it does.
//
// int occurrences = 0;
// for (int i = 0; i < text.length(); i++)
// if (text[i] == character) occurrences++;
// We could also use a range-based for loop to count the characters.
//
// int occurrences = 0;
// for (char c : text)
// if (c == character) occurrences++;
// We could also create and call a function to count the occurrences of a
// character in a string. You could comment out this code below and uncomment
// the above approaches to test them out instead.
long occurrences;
occurrences = count_occurrences(text, character);
// Output the number of occurrences of the character in the string
cout << occurrences << " occurrences" << endl;
return 0;
}