capatilize first word using space

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dave han
    New Member
    • Feb 2007
    • 2

    capatilize first word using space

    Code:
    #include <iostream>
    #include <cctype>
    
    using namespace std;
    
    int main(int argc, char *argv[], char **env) 
    {
            char c, lastc = ' ' ;
    		c= cin.get();
            do {
    			if(isalpha(c)) //determines if it is alphabetic
    				cout << (char)toupper(c); //capitalizes and outputs
    			else
    				cout << c; // if not alphabetic, outputs with no change
    			lastc = c;
    		} while(cin.get(c));
    }
    hello this is my first post and i need help on an assignment for my c++ class. above is code to capatilze all alphabetic letters entered. this was the first part of the assignement. the 2nd is to then use the code to just capatilize the first letter of each word. i think one would have to do something with spaces using lastc = ' ' but have no idea how to implement that into the code i have written so far. any suggestions or help would be great.
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    you have the correct idea, try replacing
    Code:
    	if(isalpha(c))   //determines if it is alphabetic
    		cout << (char)toupper(c); //capitalizes and outputs
    with
    Code:
    	if(isalpha(c) && lastc==' ')
    	     cout << (char)toupper(c); //capitalizes and outputs
    i.e. if the character is alphabetic and the last character was space make character upper case

    Comment

    • dave han
      New Member
      • Feb 2007
      • 2

      #3
      wow thank you sooooo much. that did it.

      Comment

      Working...