-4

i need a little help with this problem,

How do you break a char array like this "char* text" into individual words based on specific delimiters and save them in the form "char* text[]" without using the strtok function or any libraries besides "iostream".

In a normal situation i would use strings instead of char arrays and the strtok function, but in this situation, i am simply not allowed to.

Thanks,

Update: i have included what i have attempted

#include <iostream>
#include <fstream>
//#define MAX_CHARS_PER_LINE = 512;
//#define MAX_TOKENS_PER_LINE = 5;
using namespace std;
char stringToken(char* input_string);
int main(int argc, char* argv[])
{
    char input_string[512];
    ifstream infile;
    infile.open(argv[1]);
    while(!infile.eof())
    {
        infile.getline(input_string, 512);
        cout << "Main line: " << input_string << endl;
        stringToken(input_string);
    }
    infile.close();
    return 0;
}
char stringToken(char* input_string)
{
    //char* word;
    //cout << "String token function: " << input_string << endl;
    /*while(input_string >> word)
    {
        cout << word << endl;
    }*/

    char *tempone;
    char *temptwo[5];

    int ii=0,
        jj=0;
    while(input_string[ii] != '\0' && jj<5)
    {
        if((int)input_string[ii]!= 32 && (int)input_string[ii]!= 9 && (int)input_string[ii] != 44)
        {
            tempone[ii]=input_string[ii];
            //cout << "\n\nindiv char" << input_string[ii] << "\t\t" << (int)input_string[ii] << "\n\n";
        }
        else
        {
            temptwo[jj]=tempone;
            jj++;
            //testing
            cout << temptwo << endl;
        }
        ii++;

    }



    return 0;
}
Victor L
  • 3
  • 2
  • It's a little bit difficult to obtain something in C++ without doing it. In other words, use `strtok`, for example. – nbro Nov 19 '16 at 22:23
  • I know it's hard but you could do a [search](http://stackoverflow.com/questions/236129/split-a-string-in-c). Or show what you try. You come and just say "do my work thx !". – Stargateur Nov 19 '16 at 22:26
  • @Stargateur i understand your criticism, but i have been searching for a while and pretty much everything i found requires the use of the string or vector library. – Victor L Nov 19 '16 at 22:30
  • @VictorL You can read source of [strtok](https://fossies.org/dox/glibc-2.24/strtok_8c_source.html). To split a c-string you have many option. If you need to code it yourself, you need a couple of hours if you are beginner in C. Why code in C++ if you write C code... It's useless. – Stargateur Nov 19 '16 at 22:45
  • Please elaborate why you are not allowed to use any library. "I'm not allowed to use X or Y" sounds very much like "please do my homework for me". – chtz Nov 19 '16 at 22:46
  • @chtz Yes, this is part of a school assignment, originally i had used strtok and the string library. I later realized that we were not allowed to use these functions as it would fail the test case for the word "string", as such i am trying to salvage the remainder of my code. I do not expect anyone to simply drop in a piece of code for me to copy and paste, i am simply asking for a suggestion on how to proceed. Thank you for your time, – Victor L Nov 19 '16 at 23:01

1 Answers1

0

Here a pseudo code

words split(line, delims)
{
    nb_words = cound_words(line);
    words = allocate_words(nb_words + 1); // words is a array of pointer
    i = 0
    j = 0
    while true
    {
        while line[i] in delims // we transform every delims into a end string
        {
            line[i] = end_string
            i++
        }
        if line[i] not end_string
        {
            words[j] = line + i // we stock the address of line[i]
            j++
            while line[i] not in delims and line[i] not end_string
            {
                i++
            }
        }
        else
        {
            words[j] = NULL // we end the array by NULL pointer
            return words
        }
    }
}

count_word use a similar loop. I let you find it. The purpose of this algorithm is to transform the line into multiple word. So line must life as long that you use words.

Stargateur
  • 24,473
  • 8
  • 65
  • 91
  • Thank you! What is the type of "nb_words"? – Victor L Nov 19 '16 at 23:30
  • @VictorL nb_words is here to **count** the number of words in line. And than it's use to allocate the array. So his type is the same that the argument of [new operator](http://www.cplusplus.com/reference/new/operator%20new/). – Stargateur Nov 19 '16 at 23:42