Solved

Cleaning up words inside a map template

Posted on 2004-09-21
6
215 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 11

Accepted Solution

by:
avizit earned 500 total points
Comment Utility
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
Comment Utility
0
 
LVL 1

Author Comment

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

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

763 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now