Solved

Cleaning up words inside a map template

Posted on 2004-09-21
6
221 Views
Last Modified: 2010-04-01
Hello everybody,

I am reading words into a map template and while reading them in I have to check to make sure they are all correct without any punctuation marks in them.  If a word comes in that looks like this:     --hello      , then I need to get rid of the -- and just have the hello remain.  I am new to c++ so I was looking for suggestions......My get_words function is below and it's working!!!  Thanks jkr!


unsigned get_words(map<string,unsigned>& rMap)
{

      string word;
      int count = 0;

      while (cin >> word)
      {
      
          rMap[word]++;
          count++;
      }
   
      return (count);
}


Any suggestions would be extremely helpful!
0
Comment
Question by:nothing8171
  • 3
  • 3
6 Comments
 
LVL 11

Expert Comment

by:avizit
ID: 12119130
maybe you can use  

isalpha()  

 on each character of the word to see if its alphabetic , if not you just discard that character

0
 
LVL 1

Author Comment

by:nothing8171
ID: 12119150
Yeah, I came across that method and figured it would be the most appropriate, but it only takes one character as an argument.  How do I break down the word to send each individual character into it?
0
 
LVL 1

Author Comment

by:nothing8171
ID: 12119183
I also found that I could use an index variable and set it to the first character in the word, but I don't understand how this works?  I do with arrays, but not individual words.
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 11

Accepted Solution

by:
avizit earned 500 total points
ID: 12119198
strings can be subscripted like arrays .

so you can use that  ..
see the folllowing example program to get a hint on how to break up a string into individual characters

#include <iostream>

#include <string>
using namespace std;

int main(){

  string str = "experts-exchange";

  for (unsigned int i = 0; i < str.length(); i++){
    cout<<str[i]<<endl;
  }
  return 0;
}


++++++++++++++
you can also use iterators etc to iterate through the characters .. that i will leave it for you to read and try on your own

0
 
LVL 11

Expert Comment

by:avizit
ID: 12119202
0
 
LVL 1

Author Comment

by:nothing8171
ID: 12119259
Thanks avizit!!!  I didn't realize strings act as one-dimensional arrays!!!  Awesome!
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

805 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question