This should get the first letter of every word in a string?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • TimSama
    New Member
    • Aug 2010
    • 3

    #1

    This should get the first letter of every word in a string?

    The below code is to make a sentence upper case, and also get every first letter of every word in it, and put it in a different string to be called by the main function.
    When I try and call the getFirsts() function I get a breakpoint in a file in the compiler.

    Code:
    class charconverter
    {
    	private:
    	int count;
    	int last;
    	public:
    	string sentence;
    	string letters;
    	
    	string setUpper()
    	{
    		count = sentence.length();
    		for (int i = 0; i <= count; i++)
    		{
    			sentence[i] = toupper(sentence[i]);
    		}
    		return sentence;
    	}
    	string getFirsts()
    	{
    		count = sentence.length();
    		for (int i = 0; i < count; i++)
    		{
    			if (i == 0)
    			{
    				toupper(sentence[i]);
    				letters = sentence[i];
    			}
    			else if (isspace(sentence.at(i)))
    				{
    					toupper(sentence[i + 1]);
    					letters += " ";
    					letters += sentence[i + 1];
    				}
    		}
    		return letters;
    	}
    };
    So the issue really is just with

    Code:
    	string getFirsts()
    	{
    		count = sentence.length();
    		for (int i = 0; i < count; i++)
    		{
    			if (i == 0)
    			{
    				toupper(sentence[i]);
    				letters = sentence[i];
    			}
    			else if (isspace(sentence.at(i)))
    				{
    					toupper(sentence[i + 1]);
    					letters += " ";
    					letters += sentence[i + 1];
    				}
    		}
    		return letters;
    	}
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Code:
    toupper(sentence[i + 1]); 
                        letters += " "; 
                        letters += sentence[i + 1];
    If i is length(), then i+1 is out of bounds and down you come.

    I suggest you write two functions, one to convert the string to upper case and one to get the first letter of every word.

    Functions should do only one thing. The golden rule in function design is less is more. The less your function does, the more places you can use it.

    Comment

    • TimSama
      New Member
      • Aug 2010
      • 3

      #3
      i is the counting mechanism in the for operator
      Code:
      count = sentence.length();
      What the
      letters += sentence[i + 1];
      is trying to do is get the letter after the space and add it to the letters string

      or are you saying it needs to be
      letters += ++sentence[i]; ?
      also yeah, good point with the upper as a different function.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        No. I am saying that iof i is 10 then toupper(sentenc e[i + 1]) uses element 11, which does not exist.

        You cannot increment i and use it without first checking that i+1 is still < count.

        Like this:

        Code:
        i= i+1;
        if (i < count)
        {
           toupper(sentence[i]);
        }
        else
        }
           //What to do you do if i is not less than count
        {

        Comment

        • TimSama
          New Member
          • Aug 2010
          • 3

          #5
          Ok, so fixed for element overflow error.
          Code:
          string getFirsts()
          	{
          		count = sentence.length();
          		for (int i = 0; i < count; i++)
          		{
          			if (i == 0)
          			{
          				letters = sentence[i];
          			}
          			else if (isspace(sentence.at(i)))
          				{
          					if ( (i + 1) < count)
          					{
          						letters += " ";
          						letters += sentence[i + 1];
          					}
          				}
          		}
          		return letters;
          	}
          which now results in it saying I haven't assigned a element at all
          iosfwd.cpp:
          Code:
          	static void __CLRCALL_OR_CDECL assign(_Elem& _Left, const _Elem& _Right)
          		{	// assign an element
          		_Left = _Right;
          		}

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            Your code compiles and runs for me. The output even looks correct.

            Did you #include <string>?

            Comment

            • weaknessforcats
              Recognized Expert Expert
              • Mar 2007
              • 9214

              #7
              BTW: This is a very minor point but all of the STL containers have a size() method that tells you how many items are in the container.

              Whoever wrote the string template didn't see a string with a size() method for the number of characters in the string. Instead, a length() method was written.

              string does have a size() method. You are to prefer
              size() instead of length(). The length() method is deprecated.

              Comment

              Working...