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 7907833
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T11:39:22+00:00 2026-06-03T11:39:22+00:00

if for instance i have a string: Hi:my:name:is:lacrosse1991: how could I use strtok to

  • 0

if for instance i have a string: Hi:my:name:is:lacrosse1991: how could I use strtok to retrieve just the is (in other words the fourth field)? This would be similar to how you would use cut in bash, where i would just do cut -d ":" -f 4. If this is the wrong function to be doing such a thing, please let me know, any help is much appreciated, thanks! (I’ve just recently started teaching myself C, I apologize ahead of time if these are obvious questions that i am asking)

here is an example of the cut command that I would use

x=$(echo "$death" | cut -d ':' -f 4)
y=$(echo "$death" | cut -d ':' -f 5)
z=$(echo "$death" | cut -d ':' -f 6)
  • 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-03T11:39:29+00:00Added an answer on June 3, 2026 at 11:39 am

    I’d avoid strtok for this purpose (and most others, for that matter). Under the circumstances, I think I’d just use sscanf with a scanset conversion:

    char field_four[128];   
    sscanf(input_string, "%*[^:]:%*[^:]:%*[^:]:%127[^:]:", field_four);
    

    In this, we start by repeating %*[^:]: three times. Each of these reads a string including any characters except a colon, then a colon. The * means that should be read from the input, but ignored (not assigned to anything). Then, for the fourth field we have the same thing, except we do not include the *, so that field does get assigned, in this case to field_four (though you should obviously use a more meaningful name).

    For the fourth field, I’ve also added a specification of the maximum length. Since sscanf always includes a \0 to terminate the string, but doesn’t include it in the count, we need to specify a size one smaller than the size of the buffer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For instance I have a string like this : abc123[*]xyz[#]098[~]f9e [*] , [#] and
I have this string for an instance: 2 + 2 - 2 and when
I have this string : <meis xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance uri=localhost/naro-nei onded=flpSW531213 identi=lemenia id=75 lastStop=bendi xsi:noNamespaceSchemaLocation=http://localhost/xsd/postat.xsd xsd/postat.xsd>
Suppose I have a class with a string instance attribute. Should I initialize this
Say I have a class like this: public class MyClass { public string Name;
Lets simplify. Lets say I have this class: class Foo { public string Name
I have a struct like this: public struct Vehicles { public string Name {
For instance, in class Athlete I have: uint ID; string Name; Dictionary<string, string> statistics
I have one string as instance var and I want to draw it in
I have an XML string like <Segment xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xsi:type=Air> <carrier /> ... </Segment> I

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.