From a0fb7825fcd695d568439a5f1f297da09a245f79 Mon Sep 17 00:00:00 2001 From: adnangif Date: Sat, 19 Aug 2023 22:48:22 +0600 Subject: [PATCH] added solution for dimik-count-numbers --- dimik-count-numbers/en.md | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 dimik-count-numbers/en.md diff --git a/dimik-count-numbers/en.md b/dimik-count-numbers/en.md new file mode 100644 index 00000000..79fea772 --- /dev/null +++ b/dimik-count-numbers/en.md @@ -0,0 +1,37 @@ +# Counting Numbers + +**Tutorial** +As the question states, there are multiple numbers given in each **Testcase**. We have to count how many numbers are there. + +A simple approach to solving this can be taking each line as a string **S** and counting the number of whitespaces that has a non-whitespace character **before or after** it. + +Suppose length of **S** is **n**. We count number of index **i** such that **0i-1** is whitespace and **Si** is not a whitespace. +Lastly, if the **S** starts with a non-whitespace character, we increment the count by 1. +We can repeat this process for each line. + +**Solution in C++** +``` +#include + +using namespace std; + +int main(){ + int t; + cin >> t; // Input the number of testcases + + while(t--){ + cin >> ws; // To clear The buffer + string s; + getline(cin,s); // Input each line as a string + + int counter = 0; // initially we set the counter to zero + + for(int i = 1;i < s.size();i++){ + if(s[i-1] == ' ' && s[i] != ' ') counter++; + } + if(s[0] != ' ') counter++; + + cout << counter << endl; // output + } +} +``` \ No newline at end of file