1

I am about to code a tokenizer, and I am actually surprised, I have managed to do a compilable code :D but I have a problem I don't know how to solve.

I start with making an array[WORD_MAX_SIZE]={0}. After I use fopen *'r'. Word max size is global defined. My program is running fine and I get the out I want, but I want to change a thing, but I don't know how. I print the result out to the screen in the end of my While loop. But I want to save the full result in one string. Instead of printing it, I would like it in a stringset, so I can play with it later. Can anyone help?

Thanks in advance.

while (feof(fp)==0)
{
    c=fgetc(fp);

    if(isalpha(c))
    {
        array[i]=c;
        i++;
    }
    else if (i!=0)
    {
        array[i]='\0';
        i=0;
        printf("%s\n", array);
    }
}

fclose(fp);

return 0;
1

1 Answer 1

1

You can always use sprintf() instead of printf(). printf() goes to the console. With sprintf(), you can choose where to print.

For example:

char returnedArray[100]; // or change the size
sprintf(returnedArray, "%s\n", array);

Now, you have your char array (remember you don't have strings in C) saved in returnedArray.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your respond. If i use that notation sprintf(returnedArray, "%s\n", array); Does it mean it is saved in one char array?
Yes, after that, the content of array will be saved in returnedArray (with a line break character in the end because of '\n'). If this answer helped you, could you please accept it? :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.