Sunday, December 3, 2017

Count Words of Minimum Length L

Given a string S, which contains several words, print the count C of the words whose length is atleast L. (You can include punctuation marks like comma, full stop also as part of the word length. Space alone must be ignored)

Input Format:
The first line contains S. The second line contains L.

Output Format:
The first line contains C

Boundary Conditions: 2 <= Length of S <= 1000

Example Input/Output 1:
Input:
During and after Kenyattas inauguration police elsewhere in the capital, Nairobi, tried to stop the opposition from holding peaceful demonstrations. 5
Output:
13
Explanation:
The words of minimum length 5 are
During after Kenyattas inauguration police elsewhere capital, Nairobi, tried opposition holding peaceful demonstrations.

Solution:
#include <iostream>
#include <vector>
#include <cstdlib>
using namespace std;
int main(){
 string s;
 int i,n,c=0;
 vector<string> v;
 while(cin>>s)
    v.push_back(s);
 n=atoi(v.back().c_str());
 for(i=0;i<v.size()-1;i++)
   if(v[i].size()>=n)
    c++;
 cout<<c;
}
Share: