Here is an exmaple to help you.
#include<iostream>
using namespace std;
int main() {
char str[] = "one two three four five";
bool wasSpace = 1;
for (int i = 0; str[i]; i++) {
if (wasSpace)
str[i] &= str[i] >= 'a' && str[i] <= 'z' ? 223 : 0;
wasSpace = str[i] == ' ';
}
cout << str;
cin.ignore();
return 0;
}
William Hemsworth
Posting Virtuoso
1,591 posts since Mar 2008
Reputation Points: 1,429
Solved Threads: 129
>Is there a toProper() in C++?
No, you have to write it yourself. However, it's not terribly difficult to do:
#include <cctype>
#include <iostream>
#include <string>
bool isWordChar ( char c )
{
const std::string word_chars =
"abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
return word_chars.find ( c ) != std::string::npos;
}
std::string toProper ( const std::string& src )
{
std::string dst = src;
std::string::size_type i = 0;
while ( i < dst.size() ) {
// Skip non-word characters
while ( i < dst.size() && !isWordChar ( dst[i] ) )
++i;
if ( i < dst.size() )
dst[i++] = std::toupper ( (unsigned int)dst[i] );
// Find the next word boundary
while ( i < dst.size() && isWordChar ( dst[i] ) )
++i;
}
return dst;
}
int main()
{
std::cout<< toProper ( "this is a test" ) <<'\n';
}
With this approach you can even specify what you want a "word" to mean.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401