473,326 Members | 2,438 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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.

Expand|Select|Wrap|Line Numbers
  1. class charconverter
  2. {
  3.     private:
  4.     int count;
  5.     int last;
  6.     public:
  7.     string sentence;
  8.     string letters;
  9.  
  10.     string setUpper()
  11.     {
  12.         count = sentence.length();
  13.         for (int i = 0; i <= count; i++)
  14.         {
  15.             sentence[i] = toupper(sentence[i]);
  16.         }
  17.         return sentence;
  18.     }
  19.     string getFirsts()
  20.     {
  21.         count = sentence.length();
  22.         for (int i = 0; i < count; i++)
  23.         {
  24.             if (i == 0)
  25.             {
  26.                 toupper(sentence[i]);
  27.                 letters = sentence[i];
  28.             }
  29.             else if (isspace(sentence.at(i)))
  30.                 {
  31.                     toupper(sentence[i + 1]);
  32.                     letters += " ";
  33.                     letters += sentence[i + 1];
  34.                 }
  35.         }
  36.         return letters;
  37.     }
  38. };
  39.  
  40.  
So the issue really is just with

Expand|Select|Wrap|Line Numbers
  1.     string getFirsts()
  2.     {
  3.         count = sentence.length();
  4.         for (int i = 0; i < count; i++)
  5.         {
  6.             if (i == 0)
  7.             {
  8.                 toupper(sentence[i]);
  9.                 letters = sentence[i];
  10.             }
  11.             else if (isspace(sentence.at(i)))
  12.                 {
  13.                     toupper(sentence[i + 1]);
  14.                     letters += " ";
  15.                     letters += sentence[i + 1];
  16.                 }
  17.         }
  18.         return letters;
  19.     }
  20.  
  21.  
  22.  
Aug 11 '10 #1
6 5995
weaknessforcats
9,208 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1. toupper(sentence[i + 1]); 
  2.                     letters += " "; 
  3.                     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.
Aug 11 '10 #2
i is the counting mechanism in the for operator
Expand|Select|Wrap|Line Numbers
  1. count = sentence.length();
  2.  
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.
Aug 11 '10 #3
weaknessforcats
9,208 Expert Mod 8TB
No. I am saying that iof i is 10 then toupper(sentence[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:

Expand|Select|Wrap|Line Numbers
  1. i= i+1;
  2. if (i < count)
  3. {
  4.    toupper(sentence[i]);
  5. }
  6. else
  7. }
  8.    //What to do you do if i is not less than count
  9. {
Aug 11 '10 #4
Ok, so fixed for element overflow error.
Expand|Select|Wrap|Line Numbers
  1. string getFirsts()
  2.     {
  3.         count = sentence.length();
  4.         for (int i = 0; i < count; i++)
  5.         {
  6.             if (i == 0)
  7.             {
  8.                 letters = sentence[i];
  9.             }
  10.             else if (isspace(sentence.at(i)))
  11.                 {
  12.                     if ( (i + 1) < count)
  13.                     {
  14.                         letters += " ";
  15.                         letters += sentence[i + 1];
  16.                     }
  17.                 }
  18.         }
  19.         return letters;
  20.     }
  21.  
which now results in it saying I haven't assigned a element at all
iosfwd.cpp:
Expand|Select|Wrap|Line Numbers
  1.     static void __CLRCALL_OR_CDECL assign(_Elem& _Left, const _Elem& _Right)
  2.         {    // assign an element
  3.         _Left = _Right;
  4.         }
  5.  
Aug 11 '10 #5
weaknessforcats
9,208 Expert Mod 8TB
Your code compiles and runs for me. The output even looks correct.

Did you #include <string>?
Aug 12 '10 #6
weaknessforcats
9,208 Expert Mod 8TB
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.
Aug 12 '10 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: dave | last post by:
Hi How to make first letter of word in upper case? i.e. hello should return Hello Is there any inbuilt function?? Thanx dave
1
by: f | last post by:
Suppose I have a xml <class> <Name>Good</Name> <Name>bad</Name> </class> I hava the xsl <xsl:template match="Class">
10
by: Sergio del Amo | last post by:
Hi, I am trying to create a web site to work in Opera 7.2, Explorer 6.0 and Mozilla 1.4. I have the code: var imgs= document.getElementsByTagName("img"); alert(imgs.id); The id of imgs is...
3
by: pilar | last post by:
Hi All, How can I validate the entry in a text field depending on the first letter of the string. I need to have a text box to acept 14 characters if the first letter entered is M and 17...
5
by: DDK | last post by:
What is the best way to capitalize the first letter of a string? Thanks for any help, d.
5
by: JrMc | last post by:
I am new to this .net environement. I'm needing to do a simple task - I need to read the first character of a word and replace it with an uppercase. Example: "horse" --> change to "Horse" ...
2
by: blabla120 | last post by:
In an element of my source xml file, II have an attribute method="getSurfaceNumber". I want to make a XSL transformation to a target file. In the target file, I want to give out the mehod name...
3
by: shapper | last post by:
Hello, How can I make the first letter of a string to be upper case and all the others lower case? Thanks, Miguel
1
thav
by: thav | last post by:
I have a question which may sound stupid. I already searched the forums and I didn't understand it well. But is there a way to just capitalize the first letter of a string just using the string...
8
by: romo14 | last post by:
I'm working on a program that will take an address in on all one line separated by pounds (eg karen smith # p.o. box 123 # new york, new york #) and outputs it in user-friendly format. So far my...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.