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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:27:47+00:00 2026-05-13T23:27:47+00:00

I have a string 14 22 33 48 . I need to insert each

  • 0

I have a string "14 22 33 48". I need to insert each of the values in the string into the respective location in the array:

int matrix[5];

so that

matrix[0] = 14;
matrix[1] = 22;
matrix[2] = 33;
matrix[3] = 48;

How do I do this?

  • 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-13T23:27:47+00:00Added an answer on May 13, 2026 at 11:27 pm

    Robust string processing in C is never simple.

    Here’s a procedure that popped immediately to mind for me. Use strtok() to break the string into individual tokens, then convert each token to an integer value using strtol().

    Things to be aware of: strtok() modifies its input (writes 0 over the delimiters). This means we have to pass it a writable string. In the example below, I create a buffer dynamically to hold the input string, and then pass that dynamic buffer to strtok(). This guarantees that strtok() is operating on writable memory, and as a bonus, we preserve our input in case we need it later.

    Also, strtol() allows us to catch badly formed input; the second argument will point to the first character in the string that wasn’t converted. If that character is anything other than whitespace or 0, then the string was not a valid integer, and we can reject it before assigning it to our target array.

    #include <stdio.h>
    #include <string.h>  
    #include <stdlib.h>
    #include <ctype.h>
    
    /**
     * Convert a space-delimited string of integers to an array of corresponding
     * integer values.
     *
     * @param str     [in]  -- input string
     * @param arr     [in]  -- destination array
     * @param arrSize [in]  -- max number of elements in destination array
     * @param count   [out] -- number of elements assigned to destination array
     */
    void stringToIntList(char *str, int *arr, size_t arrSize, size_t *count)
    {
      /**
       * The token variable holds the next token read from the input string
       */
      char *token;
    
      /**
       * Make a copy of the input string since we are going to use strtok(),
       * which modifies its input.
       */
      char *localStr = malloc(strlen(str) + 1);
      if (!localStr)
      {
        /**
         * malloc() failed; we're going to treat this as a fatal error
         */
        exit(-1);
      }
      strcpy(localStr, str);
    
      /**
       * Initialize our output
       */
      *count = 0;
    
      /**
       * Retrieve the first token from the input string.
       */
      token = strtok(localStr, " ");
      while (token && *count < arrSize)
      {
        char *chk;
        int val = (int) strtol(token, &chk, 10);
        if (isspace(*chk) || *chk == 0)
        {
          arr[(*count)++] = val;
        }
        else
        {
          printf("\"%s\" is not a valid integer\n", token);
        }
    
        /**
         * Get the next token
         */
        token = strtok(NULL, " ");
      }
    
      /**
       * We're done with the dynamic buffer at this point.
       */
      free(localStr);
    }
    
    int main(void)
    {
      int values[5];
      char *str = "14 22 33 48 5q";
      size_t converted;
    
      stringToIntList(str, values, sizeof values / sizeof values[0], &converted);
    
      if (converted > 0)
      {
        size_t i;
        printf("Converted %lu items from \"%s\":\n", (unsigned long) converted, str);
        for (i = 0; i < converted; i++)
          printf("arr[%lu] = %d\n", (unsigned long) i, arr[i]);
      }
      else
      {
        printf("Could not convert any of \"%s\" to equivalent integer values\n", str);
      }
    
      return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 347k
  • Answers 347k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Maybe something like this will do: //get rid of empty… May 14, 2026 at 6:24 am
  • Editorial Team
    Editorial Team added an answer On the rights of a workaround: you can add the… May 14, 2026 at 6:24 am
  • Editorial Team
    Editorial Team added an answer You can implement IDataErrorInfo on your model and apply model-wide… May 14, 2026 at 6:24 am

Related Questions

I need to undo the following ASP.Net processes in PHP so I can get
I'm looking at some open source Java projects to get into Java and notice
I have a list of string array as input. The array dimension are static
I have three tables tag , page , pagetag With the data below page
Today, I came across quite strange problem. I needed to calculate string length of

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.