Hello I need to modify this code to have only the first character of whatever word inputted be capitalized. I have this one that shows all the characters capitalized, but I need to modify it using isspace().

#include <stdio.h>
#include <iostream>
using namespace std;

int main ()
{
char c, lastc= ' ' ;
c = cin.get() ;
do
{
cout.put(toupper(c));
c=cin.get();
}
while ( c!= EOF) ;
}

Recommended Answers

All 4 Replies

Please wrap your code with appropiate code tags: your code here tags so we can see keyword highlighting, line numbers and indentation.

What you want to do is look for the first non-whitespace character after a whitespace, and capitalize that. Tod do it, you need code like

/* ... */
prior = c;
c = cin.get();
if(! isspace(c) && isspace(prior)) /* is this the best order for the test? */{
  /*do something useful here*/
}
/* ... */
commented: ok thanks and sorry i didnt do the [CODE] thing it was my first thread +0

whats with the c-style syntax? Use proper C++ syntax here, or else jump towards the C
forum, where your kind are welcomed lol.

string input;
while(cin >> input){
 input[0] = toupper(input[0]);
 cout << input << endl;
}
commented: thanks and sorry for not using Proper C++ syntax im new to this cite and to both c and c++, thanks a lot +0

>> input[0] = toupper(input[0]);

And what will happen if the first character is white space?

#include <iostream>
#include <string>

int main()
{
    std::string line;
    while( std::getline(std::cin, line) )
    {
        std::string::iterator it = line.begin();
        for(; it != line.end() && isspace(*it); it++)
            ;
        *it = toupper(*it);
       std::cout << "\"" << line << "\"" << '\n';

    }
}

>>And what will happen if the first character is white space
Then it just skips it because I used cin and not getline. Here is a test.

#include <iostream>
#include <vector>
#include <string>
#include <cctype>
#include <sstream>

using namespace std;


int main(){
	string input = " hello this is a test";
	std::stringbuf inStream = std::stringbuf(input);
	cin.rdbuf(&inStream);
	string in;
	while(cin >> in){
		in[0] = toupper(in[0]);
		cout << in << endl;
	}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.