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

  • Home
  • SEARCH
  • 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 6001263
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:48:36+00:00 2026-05-23T00:48:36+00:00

I am looking for a (relatively) simple way to parse a random string and

  • 0

I am looking for a (relatively) simple way to parse a random string and extract all of the integers from it and put them into an Array – this differs from some of the other questions which are similar because my strings have no standard format.

Example:

pt112parah salin10n m5:isstupid::42$%&%^*%7first3

I would need to eventually get an array with these contents:

112 10 5 42 7 3

And I would like a method more efficient then going character by character through a string.

Thanks for your help

  • 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-05-23T00:48:37+00:00Added an answer on May 23, 2026 at 12:48 am

    A quick solution. I’m assuming that there are no numbers that exceed the range of long, and that there are no minus signs to worry about. If those are problems, then you need to do a lot more work analyzing the results of strtol() and you need to detect '-' followed by a digit.

    The code does loop over all characters; I don’t think you can avoid that. But it does use strtol() to process each sequence of digits (once the first digit is found), and resumes where strtol() left off (and strtol() is kind enough to tell us exactly where it stopped its conversion).

    #include <stdlib.h>
    #include <stdio.h>
    #include <ctype.h>
    
    int main(void)
    {
        const char data[] = "pt112parah salin10n m5:isstupid::42$%&%^*%7first3";
        long results[100];
        int  nresult = 0;
    
        const char *s = data;
        char c;
    
        while ((c = *s++) != '\0')
        {
            if (isdigit(c))
            {
                char *end;
                results[nresult++] = strtol(s-1, &end, 10);
                s = end;
            }
        }
    
        for (int i = 0; i < nresult; i++)
            printf("%d: %ld\n", i, results[i]);
        return 0;
    }
    

    Output:

    0: 112
    1: 10
    2: 5
    3: 42
    4: 7
    5: 3
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking for a relatively simple method of hooking into all click events in
I'm looking at a SSO (Single Sign On) solution to fit our relatively simple
I am looking for a way to condense relatively small areas of text on
I'm a relatively new Java programmer coming from C++/STL, and am looking for a
I'm looking for a relatively simpler (when compared with writing a parser) way to
I'm looking for a way to create a simple HTML table that can be
I am looking to start developing a relatively simple web application that will pull
I'm using jQuery's autocomplete in a relatively simple way: $(document).ready(function() { var data =
I am looking for a relatively good and well supported, and preferably open source
I am relatively new to MSBuild and am looking out for good resources to

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.