Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find total number of distinct years from a string in C++ Program
In this article, we will write a C++ program to find the total number of distinct years from a string. Here, we have a string that contains the words and the dates. Our task is to find the number of distinct years mentioned.
Let's see the following example scenario to understand the problem better:
Scenario 1
Input: str = "The Berlin Wall fell on 09/11/1989. The first website was launched on 06/08/1991." Output: Total number of distinct years: 2 Explanation: Two distinct years are referenced: 1989, and 1991
Scenario 2
Input: str = "TutorialsPoint India was founded on 20/08/2006. The Java tutorial section was launched on 10/03/2007. Major UI revamp was done on 15/01/2021." Output: Total number of distinct years: 3 Explanation: Three distinct years are referenced: 2006, 2007, and 2021.
Total Number of Distinct Years Using Regex
The following is the algorithm to find the total number of distinct years from a string:
- Initialize the text string that contains dates in the format
DD/MM/YYYY. - Write a regular expression to match dates in the
DD/MM/YYYYFormat and extract the year part. - Initialize an empty unordered set to store unique years.
- Use
regexto search all date matches in the text and extract the year from each match. - Add each extracted year to the unordered set.
- Print the size of the unordered set.
Example: Total Number of Distinct Years
In the following C++ example, we use regex to find the total number of distinct years from a string:
#include<iostream>
#include<regex>
#include<unordered_set>
using namespace std;
int uniqueYearsCount(string text) {
// Step 1: Regex to match DD/MM/YYYY and capture year
regex date_regex("\\b\\d{2}/\\d{2}/(\\d{4})\\b");
smatch match;
unordered_set<string> years;
while (regex_search(text, match, date_regex)) {
years.insert(match[1]);
text = match.suffix();
}
return years.size();
}
int main() {
string text = "TutorialsPoint India was founded on 20/08/2006. The Java tutorial section was launched on 10/03/2007. Major UI revamp was done on 15/01/2021";
cout << "Total number of distinct years: " << uniqueYearsCount(text) << endl;
return 0;
}
Following is the output:
Total number of distinct years: 3
What If the Date Format is Different?
Assuming that the date will be in 'DD-MM-YYYY' format and the string will end with a full stop.
Following is the algorithm:
- Traverse the string
- Check if the current character is a digit. Store it in another string.
- Check if the current character is '-', then remove the character stored in another string.
- Check if the length of another string is 4, then it means that is a year.
- Store that year in an unordered_set.
- Return the size of the unordered_set.
Example: Total Number of Distinct Years
Following is another C++ example, to find the number of distinct years if the format is 'DD-MM-YYYY':
#include <bits/stdc++.h>
using namespace std;
int distinctYear(string str)
{
string anotherStr = "";
unordered_set<string> uniqueDates;
for (int i = 0; i < str.length(); i++) {
if (isdigit(str[i])) {
anotherStr.push_back(str[i]);
}
else if (str[i] == '-') {
anotherStr.clear();
}
else if (anotherStr.length() == 4) {
uniqueDates.insert(anotherStr);
anotherStr.clear();
}
else{
anotherStr.clear();
}
}
return uniqueDates.size();
}
int main()
{
string str = "The Berlin Wall fell on 09/11/1989. The first website was launched on 06/08/1991.";
cout<<"Total number of distinct years: "<<distinctYear(str);
return 0;
}
Following is the output:
Total number of distinct years: 2