Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8754895
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:43:58+00:00 2026-06-13T13:43:58+00:00

Given a C-string: how would I be able to write a function that will

  • 0

Given a C-string: how would I be able to write a function that will get the next token in the string, and a function that will peek the next token and return that without using global variables?

What I’m trying to do is have a static variable that will hold the string, and when called, it would just increment a pointer, and it will reset that static variable throwing out the token that has been retrieved. The problem is: how would I be able to differentiate between the first call (when it will actually store the string) and the other calls, when I am just retrieving it?

Any thoughts on this?

EDIT:
Here’s what I have now that “works” but I want to make sure that it should actually work and its not just a coincidence of a pointer being null:

char next_token(char *line) {
    static char *p;
    if (p == NULL)
        p = line;
    else {
        char next_token = p[0];
        p++;
        return next_token;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T13:43:59+00:00Added an answer on June 13, 2026 at 1:43 pm

    The code in your edit is wrong. You are handling the NULL case incorrectly.

    I initially answered in terms of emulating strtok which seemed to be what you wanted, but you have clarified that you want single characters.

    The if-condition should be:

    if (line != NULL) p = line;
    

    And you presumably remove the else so that code executes every time… Unless you don’t want a result on the first call (you should at least return a value though).

    You call like this:

    char token = next_token(line);
    
    while( 0 != (token = next_token(NULL)) ) {
        // etc
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was looking for a quick PHP function that, given a string, would count
Given this code, tableButton = new JButton(new ImageIcon(80F_FG2015.GIF)); how would I get that String
Given the string Wibble eq wobble myhost.example.com I would like to be able to
Given the following directory: string fullpath = C:\MyDir1\MyDir2\MyDir3; I would like to return MyDir3
I would like to be able to read and write (get and set) certain
what would be the regular expression to check if a given string contains atleast
Given a very large string. I would like to process parts of the string
Given a dictionary of <string, Drink> , how would you bind the dictionary.Values to
I would like to slice random letters from a string. Given s="howdy" I would
Given a .proto that looks like this: message Base { string Dummy = 1

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.