See the original problem on HackerRank.
Solutions
The problem is very simple: we just count how many capital letters the string has and we sum 1.
A C++ Solution:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
#include <string>
#include <iostream>
#include <algorithm>
#include <iterator>
using namespace std;
int main()
{
cout << (count_if(istream_iterator<char>(cin), istream_iterator<char>(), [](char c){
return isupper(c);
})+1);
}
|
Here is a Python solution by Yuri Valentini:
1
2
|
ss = raw_input().strip()
print map(operator.eq, ss, ss.lower()).count(False) +1
|
Haskell solution by Alessandro Pezzato
1
|
main = interact $ show . (+ 1) . length . filter (\x -> 'A' <= x && x <= 'Z')
|
Rust solution by Alessandro Pezzato
1
2
3
4
5
6
7
|
use std::io;
fn main() {
let mut input = String::new();
io::stdin().read_line(&mut input).unwrap();
println!("{}", 1 + input.chars().filter(|&x| x.is_uppercase()).count());
}
|
An alternative method, to count uppercase letters, is to treat characters
as bytes and match against bitmasks. In ASCII, lowercase letters have
the sixth bit up (1), while uppercase letter have it low.
Rust, with for
:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
use std::io;
use std::io::Read;
fn main() {
let mut count: u32 = 1;
for b in io::stdin().bytes() {
let b = b.unwrap();
count += ((!b & 0x20) >> 5) as u32;
}
println!("{}", count);
}
|
Rust, with fold
:
1
2
3
4
5
6
7
8
9
10
11
|
use std::io;
use std::io::Read;
fn main() {
println!(
"{}",
io::stdin()
.bytes()
.fold(1, |acc, b| acc + ((!b.unwrap() & 0x20) >> 5) as u32)
);
}
|
Bash, with regular expression and word count. This is the complete program.
1
|
let n=1+$(grep -e '[A-Z]' -o | wc -l); echo $n;
|