Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added solution for dimik-count-numbers #494

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions dimik-count-numbers/en.md
Original file line number Diff line number Diff line change
@@ -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 **0<i<n** and **S<sub>i-1</sub>** is whitespace and **S<sub>i</sub>** 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<bits/stdc++.h>

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
}
}
```