Get next token - C++ Data Type

C++ examples for Data Type:char array

Description

Get next token

Demo Code

#include <iostream>
#include <cstring>
#include <cctype>
using namespace std;
const char *gettoken(const char *str);
int main() {/*from   ww w. j  a va  2s  . c  o m*/
   char sampleA[] = "this is a test max=12+3/89; count27 = 19*(min+floor);";
   char sampleB[] = "while(i < max) test test i = counter * 2;";
   const char *tok;

   // Tokenize the first string.
   tok = gettoken(sampleA);
   cout << "Here are the tokens in: " << sampleA << endl;

   while(tok) {
      cout << tok << endl;
      tok  = gettoken(NULL);
   }
   cout << "\n\n";
   // Restart gettoken() by passing the second string.
   tok = gettoken(sampleB);
   cout << "Here are the tokens in: " << sampleB << endl;

   while(tok) {
      cout << tok << endl;
      tok  = gettoken(NULL);
   }
   return 0;
}
#define MAX_TOK_SIZE 128
const char *gettoken(const char *str) {
   static char token[MAX_TOK_SIZE+1];
   static const char *ptr;
   int count; // holds the current character count
   char *tokptr;
   if(str) {
      ptr = str;
   }
   tokptr = token;
   count = 0;

   while(isspace(*ptr)) ptr++;

   if(isalpha(*ptr)) {
      while(isalpha(*ptr) || isdigit(*ptr)) {
         *tokptr++ = *ptr++;
         ++count;
         if(count == MAX_TOK_SIZE) break;
      }
   } else if(isdigit(*ptr)) {
      while(isdigit(*ptr)) {
         *tokptr++ = *ptr++;
         ++count;
         if(count == MAX_TOK_SIZE) break;
      }
   } else if(ispunct(*ptr)) {
      *tokptr++ = *ptr++;
   } else 
      return NULL;
   // Null-terminate the token.
   *tokptr = '\0';
   return token;
}

Result


Related Tutorials