By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,199 Members | 1,551 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,199 IT Pros & Developers. It's quick & easy.

stringTokenizer

Ganon11
Expert 2.5K+
P: 3,652
Hey,

Last year I was working in Java, and one of the predefined classes we used was the stringTokenizer.java class. I wanted to use this in C++, but couldn't find a predefined class. So I made my own class - here's the code:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. class stringTokenizer {
  6.       public:
  7.              void setMessage(string newMessage);
  8.              string next();
  9.              bool hasNext();
  10.              stringTokenizer(string myMes, char myCh, bool retDel);
  11.              stringTokenizer(string myMes, char myCh);
  12.              stringTokenizer(string myMes);
  13.  
  14.       private:
  15.               void processBlanks();
  16.               string message;
  17.               char ch;
  18.               bool delRet;
  19. };
  20.  
  21. stringTokenizer::stringTokenizer(string myMes, char myCh, bool retDel) {
  22.     message = myMes;
  23.     ch = myCh;
  24.     delRet = retDel;
  25. }
  26.  
  27. stringTokenizer::stringTokenizer(string myMes, char myCh) {
  28.     message = myMes;
  29.     ch = myCh;
  30.     delRet = false;
  31. }
  32.  
  33. stringTokenizer::stringTokenizer(string myMes) {
  34.     message = myMes;
  35.     ch = ' ';
  36.     delRet = false;
  37. }
  38.  
  39. void stringTokenizer::setMessage(string newMessage) {
  40.      message = newMessage;
  41. }
  42.  
  43. string stringTokenizer::next() {
  44.     string word;
  45.     if (!delRet) {
  46.         processBlanks();
  47.         int pos = message.find(ch);
  48.         if (pos != string::npos) {
  49.             word = message.substr(0, pos);
  50.             message = message.substr(pos);
  51.         } else {
  52.             word = message;
  53.             message = "";
  54.         }
  55.     } else {
  56.         if (message[0] == ch) {
  57.             word = message.substr(0, 1);
  58.             message = message.substr(1);
  59.         } else {
  60.             int pos = message.find(ch);
  61.             if (pos != string::npos) {
  62.                 word = message.substr(0, pos);
  63.                 message = message.substr(pos);
  64.             } else {
  65.                 word = message;
  66.                 message = "";
  67.             }
  68.         }
  69.     }
  70.     return word;
  71. }
  72.  
  73. bool stringTokenizer::hasNext() {
  74.      return message.length() > 0;
  75. }
  76.  
  77. void stringTokenizer::processBlanks() {
  78.      while (message.length() > 0 && message[0] == ch)
  79.            message = message.substr(1);
  80.      return;
  81. }
My question is, for any Java/C++ users out there, is there anything I can improve to make this as identical as possible in implementation as the Java class? I have looked at the Java summary of the stringTokenizer class at http://java.sun.com/j2se/1.4.2/docs/api/java/util/StringTokenizer.html

Thanks for any help.
Nov 29 '06 #1
Share this Question
Share on Google+
1 Reply


P: 16
u can check this website which gives information about string tokenizers in c++

http://www.codeproject.com/cpp/string_tokenizer_class_in_c__.asp

http://www.partow.net/programming/stringtokenizer/index.html

hope its of some use,

thank you,

smitha
Nov 29 '06 #2

Post your reply

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