Rule violation prevention
Jul 11, 2013 at 5:11am UTC
I was trying to write a program that prevents others to type every letter in CAPITAL SIZE since it's rude to do so in a chat. The program should output the number of phrases that are CAPITALIZED. However, it doesn't work. The output is always 1. Please help me with it. Thanks a lot! :)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
int main(){
char a[257];
int len,c_count=0,c,total=0;
scanf("%d" ,&c);
for (int i=1;i<=c;i++){
scanf("%s" ,a);
len=strlen(a);
for (int i=0;i<len;i++){
if (a[i]>='A' && a[i]<='Z' ) c_count++;
}
if (c_count==len) total++;
}
printf("%d\n" ,total);
return 0;
}
Last edited on Jul 11, 2013 at 5:11am UTC
Jul 11, 2013 at 6:22am UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <iostream>
#include <string>
#include <cctype>
#include <numeric>
#include <iomanip>
int main()
{
std::string line ;
while ( std::getline(std::cin, line) && line.length() > 0 )
{
unsigned nCaps = std::accumulate(std::begin(line), std::end(line), 0u,
[](unsigned val, char ch){return val+=std::isupper(ch)?1:0;}) ;
std::cout << std::fixed << std::setprecision(0)
<< (static_cast <double >(nCaps) / line.length())*100.0 << "% CAPS\n" ;
}
}
http://ideone.com/djW2jz
Topic archived. No new replies allowed.