// Ask user to input a string and tell them what they put down
string str1;
cout << "Please enter your text : ";
getline(cin, str1);
cout << "Your input was : " << str1;
//Checking for white spaces inside the string
for (int i = 0; i < str1.length(); i++){
if (isspace(str1.at(i))) {
}
}
// Prints out the amount of spaces inside the string.
cout << "\nThe number of 'spaces(s)' : " << str1;
return 0;
}
#include <iostream>
#include <string>
#include <locale>
int main()
{
// Ask user to input a string and tell them what they put down
std::string str1;
std::cout << "Please enter your text : ";
std::getline( std::cin, str1 );
std::cout << "Your input was : '" << str1 << "'\n'" ;
std::size_t num_whitespaces = 0 ; // count of white space characters
//Checking for white spaces inside the string
// http://www.stroustrup.com/C++11FAQ.html#forfor( char c : str1 ) // for each character in the string
{
// http://en.cppreference.com/w/cpp/locale/isspace
// use the locale used by stdin
if( std::isspace( c, std::cin.getloc() ) ) ++num_whitespaces ;
// or use current C locale: #include <cctype> and
// if( std::isspace(c) ) ++num_whitespaces ;
}
// Prints out the amount of spaces inside the string.
std::cout << "\nThe number of 'space(s)' : " << num_whitespaces << '\n' ;
}
#include <iostream>
#include <string>
#include <locale>
usingnamespace std;
int main()
{
// Ask user to input a string and tell them what they put down
string str1;
cout << "Please enter your text : ";
getline(cin, str1);
cout << "Your input was : " << str1;
//Checking for white spaces inside the string
int count = 0;
for (int i = 0; i < str1.length(); i++)
{
if (isspace(str1.at(i)))
{
count++;
}
}
// Prints out the amount of spaces inside the string.
cout << "\nThe number of 'spaces(s)' : " << count << "\n\n;
system("PAUSE");
return 0;
}