I'm trying to extract the value of the coeffecients and the exponent from a polynomial. I have already succeeded in extracting the coeffients using strtok. I applied the same concept to find the exponent, but I don't know how to use strtok to extract the string AFTER the delimiters or skip the first character, and strtok is the only extracting tool I know. The program is developed in C++ but strtok uses c codes so I just post it here

This is main()

#include <iostream>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <string>
using namespace std;

void extractCoeff (char *str, char *copy);
void extractExp (char *str, char *copy);
int main()
{
    const int SIZE = 150; // size for string input

    char *string;
    string = new char[SIZE];
    cout << "Enter the polynomial\n";
    cin.ignore();
    cin.getline(string, SIZE);  

    char *strCopy;
    strCopy = new char[SIZE];
    strcpy(strCopy, string);

    char *copy1;
    copy1 = new char[SIZE];
    strcpy(copy1, string);  
    extractCoeff(string, copy1);

    cout << endl << endl;
    char *copy2;
    copy2 = new char[SIZE];
    strcpy(copy2, strCopy); 
    extractExp(strCopy, copy2);

    return 0;
}

extracting coeff

void extractCoeff (char *str, char *copy)
{   
    char *p = strtok(str, " +"); // extract the first time
    char *search;
    int counter = 0;
    while (p) 
    {
        search = strstr(p, "x^");
        cout << "Token: " << p << endl;
        cout << "Search " << search << endl;
        p = strtok(NULL, " +");
        counter++;
    }

    cout << copy << endl;

    // find coeff
    int *coefficient;
    coefficient = new int[counter];

    p = strtok(copy, " +"); // extract the second time to find coeff
    int a = 0;
    while (p)
    {
        cout << "p: " << p << endl;
        long coeff;
        if (*p == 'x')
        {
           coeff = 1;
        }
        else if (*p == NULL)
        {
            coeff = 0;
        }
        else
        {
            char *endptr;
            coeff = strtol(p, &endptr, 10);
        }
        coefficient[a] = coeff;
        p = strtok(NULL, " +");
        a++;
    }

    for (int i = 0; i < counter; i++)
        cout << coefficient[i] << endl;
}

extracting exponent (it prints also the + characters, i just want the exponents)

void extractCoeff (char *str, char *copy)
{   
    char *p = strtok(str, " +"); // extract the first time
    char *search;
    int counter = 0;
    while (p) 
    {
        search = strstr(p, "x^");
        cout << "Token: " << p << endl;
        cout << "Search " << search << endl;
        p = strtok(NULL, " +");
        counter++;
    }

    cout << copy << endl;

    // find coeff
    int *coefficient;
    coefficient = new int[counter];

    p = strtok(copy, " +"); // extract the second time to find coeff
    int a = 0;
    while (p)
    {
        cout << "p: " << p << endl;
        long coeff;
        if (*p == 'x')
        {
           coeff = 1;
        }
        else if (*p == NULL)
        {
            coeff = 0;
        }
        else
        {
            char *endptr;
            coeff = strtol(p, &endptr, 10);
        }
        coefficient[a] = coeff;
        p = strtok(NULL, " +");
        a++;
    }

    for (int i = 0; i < counter; i++)
        cout << coefficient[i] << endl;
}

void extractExp (char *str, char *copy)
{   
    char *p = strtok(str, " x^"); // extract the first time
    //char *search;
    int counter = 0;
    while (p) 
    {
        //search = strstr(p, "x^");
        //cout << "Token: " << p << endl;
        //cout << "Search " << search << endl;
        p = strtok(NULL, " x^");
        counter++;
    }

    cout << copy << endl;

    // find coeff
    int *exp;
    exp = new int[counter];

    p = strtok(copy, " x^"); // extract the third time
    int b = 0;
    while (p)
    {
        cout << "p2: " << p << endl;
        int expVal;
        if (*p == NULL)
        {
            expVal = 0;
        }
        else
        {
            char *endptr;
            expVal = strtol(p, &endptr, 10);
        }
        exp[b] = expVal;
        p = strtok(NULL, " x^");
        b++;
    }

    for (int i = 0; i < counter; i++)
        cout << exp[i] << endl;
}

You posted this in the C language forum, yet you are using C++ constructs. Is this supposed to be in C, or in C++? Or does it not matter?

This article has been dead for over six months. Start a new discussion instead.