C++ code to count volume of given text

Suppose we have a string S with n characters. S is a single-space separated words, consisting of small and capital English letters. Volume of the word is number of capital letters in the given word. And volume of the text is maximum volume of all words in the text. We have to find the volume of the given text.

So, if the input is like S = "Paper MILL", then the output will be 4, because volume of first word is 1 and second word is 4, so maximum is 4.


To solve this, we will follow these steps −

ans := 0
a := 0
n := size of S
for initialize i := 0, when i <= n, update (increase i by 1), do:
   s := S[i]
   if s >= 'A' and s <= 'Z', then:
      (increase a by 1)
   if s is same as blank space, then:
      ans := maximum of ans and a
      a := 0
ans := maximum of ans and a
return ans


Let us see the following implementation to get better understanding −

#include <bits/stdc++.h>
using namespace std;
int solve(string S){
   int ans = 0, a = 0;
   int n = S.size();
   for (int i = 0; i <= n; i++){
      char s = S[i];
      if ((s >= 'A') && (s <= 'Z'))
      if (s == ' '){
         ans = max(ans, a);
         a = 0;
   ans = max(ans, a);
   return ans;
int main(){
   string S = "Paper MILL";
   cout << solve(S) << endl;


"Paper MILL"



Updated on: 30-Mar-2022


Kickstart Your Career

Get certified by completing the course

Get Started