473,762 Members | 8,115 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why i can't erase the punctuation?

254 Contributor
I have done the following code, i just couldn't figure out why it can erase the punctuation in the end of each word(strr variable) ?

i got this when i run it:



here is my function code:
Expand|Select|Wrap|Line Numbers
  1. void testFunction(string str)
  2. {
  3.     map<string, int> freq;
  4.     string strr;
  5.     int i;
  6.  
  7.     for(i=0; i<str.length(); i++)
  8.     {
  9.             if(isspace(str[i])){
  10.                 strr = str.substr(0, i);
  11.  
  12.                 if(ispunct(strr[i-1])){     // is this statement can't check punctuation?
  13.                     strr.erase(i,1);
  14.                 }
  15.                 str.erase(0, i+1);
  16.                 freq[strr]++;
  17.                 i=0;
  18.  
  19.             }
  20.     }
  21.  
  22.     map<string, int>::const_iterator it;
  23.             for (it=freq.begin(); it != freq.end(); ++it) {
  24.             cout << it->second << " " << it->first << endl;
  25.     }
  26. }
  27.  
Nov 22 '06 #1
1 1510
Banfa
9,065 Recognized Expert Moderator Expert
Should

strr.erase(i,1) ;

be

strr.erase(i-1,1);
Nov 23 '06 #2

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

Similar topics

5
2759
by: Angus Leeming | last post by:
Dinkumware's online STL reference http://tinyurl.com/3es52 declares std::map's overloaded erase member functions to have the interface: map::erase iterator erase(iterator where); iterator erase(iterator first, iterator last); size_type erase(const Key& keyval); Ie, the first two functions above have the same interface as
26
5401
by: Pieter Thysebaert | last post by:
Hello, I've got a question conerning erasing key-value pairs from a std::map while iterating over it. According to the STL docs, erasing an element in a map invalidates all iterators pointing to that element so
3
5175
by: jose luis fernandez diaz | last post by:
Hi, Erase elements while iterating on a map don't invalidate the iterator except the erased one, so the program below: (1) #include <map> int main()
7
3442
by: Lachlan Hunt | last post by:
Hi, I have recently downloaded and experemented with IBM HPR 3.0, and Opera 8 with text-to-speech, and have come to realise some fairly annoying issues regarding punctuation marks. I've found, that when a punctuation mark occurs directly after an element, both HPR and Opera 8 will read the punctuation mark. For example, the following: <p><abbr title="...">HTML</abbr> is an application of
8
6170
by: olanglois | last post by:
Hi, I was asking myself to following question. What is better to erase an element from a STL map: calling (option #1) size_type erase(const key_type& k) or calling (option #2)
11
5540
by: moleskyca1 | last post by:
Hi, I know if you call erase when you iterate through map you will crash. Ex: map<int,doublem; // insert something for ( map<int, double>::iterator i = m.begin(); i != m.end(); i++ ) if ( i->second < 0 ) m.erase(i);
6
3706
by: Tashfeen Bhimdi | last post by:
I'm trying to remove punctuation from a string with the following code: ---------------------------- #include <string> #include <algorithm> #include <cctype> .. using namespace std ..
6
1710
by: alon | last post by:
I got the following situation: I have a few lists of integers, and I have an iterator pointing to one of the elements. I don't have a pointer to the list itself ! All I want is to remove the element the iterator points to... Basically, a list element has pointers back and forth, so I shouldn't need the list itself, but I don't see any function that does the work. Is there a real problem to do this? Is it just a coincidence that no body...
3
1881
by: subramanian100in | last post by:
Consider vector<stringv; If we call, v.erase(v.end()) this invokes undefined behaviour. But, if we call
0
9554
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10137
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9927
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6640
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5268
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5405
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3914
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3510
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2788
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.