#include <iostream>
#include <string>
std:: size_t countSpaces( const std:: string & s)
{
std:: size_t spaces = 0 ;
for ( auto letter : s)
if ( letter == ' ' )
++ spaces;
return spaces;
}
std:: string withoutSpaces( const std:: string & s)
{
std:: string result;
for ( auto letter : s)
if ( letter ! = ' ' )
result + = letter;
return result;
}
int main( )
{
std:: string line;
// get the string!
std:: cout << "Enter a string\n " ;
std:: getline ( std:: cin , line) ;
std:: cout << '"' << line << "\" contains " << countSpaces( line) << " spaces.\n " ;
std:: cout << "Without spaces: \" " << withoutSpaces( line) << "\" \n " ;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8c3RyaW5nPgoKc3RkOjpzaXplX3QgY291bnRTcGFjZXMoY29uc3Qgc3RkOjpzdHJpbmcmIHMpCnsKICAgIHN0ZDo6c2l6ZV90IHNwYWNlcyA9IDA7CgogICAgZm9yIChhdXRvIGxldHRlciA6IHMpCiAgICAgICAgaWYgKGxldHRlciA9PSAnICcpCiAgICAgICAgICAgICsrc3BhY2VzOwoKICAgIHJldHVybiBzcGFjZXM7Cn0KCnN0ZDo6c3RyaW5nIHdpdGhvdXRTcGFjZXMoY29uc3Qgc3RkOjpzdHJpbmcmIHMpCnsKICAgIHN0ZDo6c3RyaW5nIHJlc3VsdDsKCiAgICBmb3IgKGF1dG8gbGV0dGVyIDogcykKICAgICAgICBpZiAobGV0dGVyICE9ICcgJykKICAgICAgICAgICAgcmVzdWx0ICs9IGxldHRlcjsKCiAgICByZXR1cm4gcmVzdWx0Owp9CgppbnQgbWFpbigpCnsKICAgIHN0ZDo6c3RyaW5nIGxpbmU7CgogICAgLy8gZ2V0IHRoZSBzdHJpbmchCiAgICBzdGQ6OmNvdXQgPDwgIkVudGVyIGEgc3RyaW5nXG4iOwogICAgc3RkOjpnZXRsaW5lKHN0ZDo6Y2luLCBsaW5lKTsKCiAgICBzdGQ6OmNvdXQgPDwgJyInIDw8IGxpbmUgPDwgIlwiIGNvbnRhaW5zICIgPDwgY291bnRTcGFjZXMobGluZSkgPDwgIiBzcGFjZXMuXG4iOwogICAgc3RkOjpjb3V0IDw8ICJXaXRob3V0IHNwYWNlczogXCIiIDw8IHdpdGhvdXRTcGFjZXMobGluZSkgPDwgIlwiXG4iOwp9
stdin
V2VsbCwgaXQgaXMgbm8gc2VjcmV0IHRoYXQgdGhlIGJlc3QgdGhpbmcgYWJvdXQgc2VjcmV0cyBpcyBzZWNyZXRseSB0ZWxsaW5nIHNvbWVvbmUgeW91ciBzZWNyZXQsIHRoZXJlYnkgYWRkaW5nIGFub3RoZXIgc2VjcmV0IHRvIHlvdXIgY29sbGVjdGlvbiBvZiBzZWNyZXRzLCBzZWNyZXRseS4=
Well, it is no secret that the best thing about secrets is secretly telling someone your secret, thereby adding another secret to your collection of secrets, secretly.
stdout
Enter a string
"Well, it is no secret that the best thing about secrets is secretly telling someone your secret, thereby adding another secret to your collection of secrets, secretly." contains 26 spaces.
Without spaces: "Well,itisnosecretthatthebestthingaboutsecretsissecretlytellingsomeoneyoursecret,therebyaddinganothersecrettoyourcollectionofsecrets,secretly."