capitalization trick

This is a discussion on capitalization trick within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> using namespace std; int main() { char string[81]; cout << "Enter a string" << endl; cin.getline(string, 81); ...

  1. #1
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357

    capitalization trick

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	char string[81];
    
    	cout << "Enter a string" << endl;
    
    	cin.getline(string, 81);
    
    	cout << endl << string << endl;
    
    	return 0;
    }
    Is there a way to get a string to print out as:

    Hi, How Are You?

    if I enter:

    hi, how are you?

    I'm guessing to capitalize the first word of a sentence, you would need to detect when the space key is entered and use the toupper function somewhere.

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    you'll need to parse the string, and find the first letter of each word and use toupper().

  3. #3
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    Code:
             cout << "Please enter your name: ";
    	getline(cin,name);
    
    	if((name[0] > 97) && (name[0] < 122))
    	 {
    		name[0] -= 32;
    	 }

  4. #4
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357
    Originally posted by alpha
    you'll need to parse the string, and find the first letter of each word and use toupper().
    Parse the string?

  5. #5
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    the code i posted will capitalize the first letter in "name";

  6. #6
    The Pantless Man CheesyMoo's Avatar
    Join Date
    Jan 2003
    Posts
    262
    What you should do is something like

    Code:
    first letter is capatilized
    
    start a loop
    while(blabla[i] != 0)
    {
    check for a space
    if there is a space
    blablabla[i +1] = toupper(blablabla[i + 1]);
    
    i++;
    }
    Of course that was just some messed up pseudo code, but it's got the main idea.
    If you ever need a hug, just ask.

  7. #7
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357
    Originally posted by RoD
    Code:
             cout << "Please enter your name: ";
    	getline(cin,name);
    
    	if((name[0] > 97) && (name[0] < 122))
    	 {
    		name[0] -= 32;
    	 }
    Can't you make this a little easier for me by using my code example?

    I'm not even familiar with getline(cin,name);

    I use cin.getline();

    Also, isn't it easier with the toupper method? Besides, I remember someone telling me that the - 32 trick isn't portable to other platforms.

  8. #8
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
    char string[81];
    cout << "Enter a string" << endl;
    cin.getline(string, 81);
    
    if((string[0] > 97) && (string[0] < 122))
     {
    string[0] -= 32;
     }
    
    
    cout << endl << string << endl;
    
    return 0;
    }

  9. #9
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357
    Thanks RoD, but...

    "Also, isn't it easier with the toupper method? Besides, I remember someone telling me that the - 32 trick isn't portable to other platforms."

  10. #10
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    hmm try this

    Code:
    #include <iostream>
    #include <ctype.h>
    
    using namespace std;
    int main()
    {
    char string[81];
    cout << "Enter a string" << endl;
    cin.getline(string, 81);
    
    string[0] = toupper(string[0]);
    	
    cout << endl << string << endl;
    return 0;
    }

  11. #11
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357
    Originally posted by RoD
    hmm try this

    Code:
    #include <iostream>
    #include <ctype.h>
    
    using namespace std;
    int main()
    {
    char string[81];
    cout << "Enter a string" << endl;
    cin.getline(string, 81);
    
    string[0] = toupper(string[0]);
    	
    cout << endl << string << endl;
    return 0;
    }
    Thanks again, RoD. But why did you include <ctype.h>? As far as I know, that code will work perfectly without it...I think...

    Oh, I forgot about that space checking thing. I wanted the output of "hi, how are you?" to look like "Hi, How Are You?" not "Hi, how are you?

    Would a for loop be better than a while loop for this process?

  12. #12
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Code:
    #include <iostream>
    #include <string>
    #include <cctype>
    
    int main(int argc, char** argv)
    {
       using namespace std;
       string in;
       getline(cin, in);
    
       in[0] = toupper(in[0]);
       for (i = 0; i < in.length() - 1; ++i)
       {
           if (isspace(in[i]))
              in[i+1] = toupper(in[i+1]);
       }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trick Question
    By @nthony in forum C Programming
    Replies: 16
    Last Post: 04-16-2009, 06:22 PM
  2. Replies: 6
    Last Post: 07-24-2008, 01:53 PM
  3. Trick with strings help/questions
    By NoobieGecko in forum C Programming
    Replies: 2
    Last Post: 05-17-2008, 04:15 PM
  4. No! Another way to trick us men!!
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 10-18-2004, 09:47 PM
  5. The Meaning of Life: A Trick Question?
    By chix/w/guns in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 07-12-2004, 07:53 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21