
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Find total number of distinct years from a string in C++
In this tutorial, we will be discussing a program to find total number of distinct years from a string.
For this we will be provided with a string containing dates in format ‘DD-MM-YYYY’ format. Our task is to find the count of distinct years mentioned in the given string.
Example
#include <bits/stdc++.h> using namespace std; //calculating the distinct years mentioned int calculateDifferentYears(string str) { unordered_set<string> differentYears; string str2 = ""; for (int i = 0; i < str.length(); i++) { if (isdigit(str[i])) { str2.push_back(str[i]); } if (str[i] == '-') { str2.clear(); } if (str2.length() == 4) { differentYears.insert(str2); str2.clear(); } } return differentYears.size(); } int main() { string sentence = "I was born on 22-12-1955." "My sister was born on 34-06-2003 and my mother on 23-03-1940."; cout << calculateDifferentYears(sentence); return 0; }
Output
3
- Related Articles
- Find total number of distinct years from a string in C++ Program
- Count number of Distinct Substring in a String in C++
- Find distinct characters in distinct substrings of a string
- Program to find out number of distinct substrings in a given string in python
- How to find and extract a number from a string in C#?
- Program to find number of distinct island shapes from a given matrix in Python
- Number of Distinct Islands in C++
- Averaging a total from a Score column in MySQL with the count of distinct ids?
- Program to find total sum of all substrings of a number given as string in Python
- Write a program in Python to count the total number of leap years in a given DataFrame
- Count total number of digits from 1 to N in C++
- Number of Distinct Islands II in C++
- Print all distinct characters of a string in order in C++
- C++ code to find total number of digits in special numbers
- Find all distinct palindromic sub-strings of a given String in Python

Advertisements