How do I capitalize first letter of all words in a string?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dizzylizzyd514
    New Member
    • Jul 2008
    • 5

    How do I capitalize first letter of all words in a string?

    I need to make this:

    introductoRy speEch ==> Introductory Speech
    pRecalCulus 1 and 2 ==> Precalculus 1 And 2


    I know how to capitalize the first letter of a word, but not multiple words in a string like the examples shown.

    Any ideas????
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Capitalize the first character of the string as well as all non white space characters
    that follow a white space character. Every other character should be changed to
    its lowercase form whereever applicable,

    kind regards,

    Jos

    Comment

    • dizzylizzyd514
      New Member
      • Jul 2008
      • 5

      #3
      void reformatcourse (string& course)
      //Given the course name (string data type), reformatcourse will capitalize the
      //first letter of the course name and make the remaining letters of the course
      //name lowercase. The updated coursename is then passed back.
      {
      int length;
      length = course.length() ;
      course [0] = toupper(course [0]);
      for (int i=1; i<length; i++)
      { if (course[i] != ' ')
      {course[i] = tolower(course[i]);
      }
      if (course[i] = ' ')
      {course[i+1] = toupper(course[i+1]);
      i++;
      }

      }

      }


      This is what i have and it's not working. Do you notice any errors I could fix. I've been working on this since last night and I just can't figure it out.

      Comment

      • gpraghuram
        Recognized Expert Top Contributor
        • Mar 2007
        • 1275

        #4
        Hi,
        The problem is in the line
        [code=cpp]
        //if (course[i] = ' ')
        //You have used assignment operator instead of comparison operator.
        //change that to
        if (course[i] == ' ')
        [/code]

        And everything is fine

        Raghu

        Comment

        • whodgson
          Contributor
          • Jan 2007
          • 542

          #5
          .......somethin g like this will do it to.
          Code:
          int l= s.length();
              s[0]=toupper(s[0]);              //capitalizes 
              cout<<s[0];                        //and prints first letter
                         for(int i=1;i<l;i++)     //now starting with s[1]
                         {
                         if(s[i]!=' ')                  //prints next char providing not a space
                         {cout<<s[i];
                         continue;}                //repeat for all following lower case
                            if( s[i]=' ')              //if a space print it
                            {cout<<' ';
                            i++;                       //and increment
                            s[i]=toupper(s[i]);   //capitalize letter after space
                            }
                            cout<<s[i];           //and print it
              
                        }

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            Originally posted by whodgson
            .......somethin g like this will do it to.
            Code:
            int l= s.length();
            <snipped>                  cout<<s[i];           //and print it
                
                          }
            No it wont, it doesn't lower case letters that are upper case and in the middle of a word.

            Comment

            • whodgson
              Contributor
              • Jan 2007
              • 542

              #7
              Quite so Banfa, I wrongly assumed they were all in lower case to start with despite the example.
              So add a for loop which does that first.
              e.g.
              Code:
              for(int i=0;i<l;i++)
                s[i]=tolower(s[i]);

              Comment

              • hdanw
                New Member
                • Feb 2008
                • 61

                #8
                Use the Shift Key.

                ;)

                Comment

                Working...