Search code examples
c++tokenize

Tokenizing a string in C++


Description: Building basic code in C++ for tokenizing string(whitespaces included).

The main idea: To call a local function that counts all the whitespaces in a string using their null value for the beginning.

The problem: Early termination before reaching the second token.

#include "stdafx.h"
#include <iostream>
#include <cstring>

using namespace std;

char *next_token = NULL;

int count_spaces(const char *p);

char *p;

int main() {
    char s[81];

    cout << "Input a string : ";
    cin.getline(s, 81);

    p = strtok_s(s, ", ", &next_token);

    while (p != nullptr) {
        cout << p << endl;
        p = strtok_s(nullptr, ", ", &next_token);
    }

    count_spaces(p);

    cout << count_spaces(p);

    return 0;
}

int count_spaces(const char *p) {
    int number = 0, len;
    if (p == NULL) {
        return 0;
    }

    while (*p) {
        if (*p == '1')
            len = 0;
        else if (++len == 1)
            number++;
        p++;
    }
}

Any assistance would be appreciated.


Solution

  • The tokenizing part of your program works. But when you call count_spaces(p), p is always NULL (or nullptr which is pretty much the same).

    Maybe you want this (I left out the tokenizing part):

    #include <iostream>
    #include <cstring>
    
    using namespace std;
    
    int count_spaces(const char *p);
    
    int main() {
      char s[81];
      cout << "Input a string : ";
      cin.getline(s, sizeof s);      // using sizeof s is better than repeating "81"
    
      cout << "Number of spaces in string: " << count_spaces(s) << endl;
      return 0;
    }
    
    int count_spaces(const char *p) {
      if (p == NULL) {
        return 0;
      }
    
      int nbofspaces = 0;
    
      while (*p) {
        if (*p == ' ') { 
          nbofspaces++;
        }
        p++;
      }
    
      return nbofspaces;
    }
    

    Disclaimer: this is poor C++ code. It is rather what you would do in C, but this code sticks as closely as possible to the OP's original code.