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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:54:02+00:00 2026-06-13T06:54:02+00:00

I am trying to take user input: (1 345 44 23) and make it

  • 0

I am trying to take user input: (1 345 44 23) and make it into a tokenized char string then into ints. Surprisingly I could not find much help for what I would think would be a common task.

Any ideas how to convert the char string into an in string using tokens?

My program crashes when it gets to the conversion (after the tokenization [I realize this is not a word]).

Thanks!

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#define StrSZE 81


void strInput (char str[], int maxChars);
void custatoi(char * tokenArray[], int * data, int numOfTok);


int main(int argc, char *argv[])
{
    char str[StrSZE];
    char* tokenArray;
    int maxChars=StrSZE-1, cont=1, numOfToken=0, i=0;
    int* data;


    strInput(str, maxChars);


    tokenArray = strtok(str, " \t");
    while (tokenArray)
    {
        printf("token: %s\n", tokenArray);
        tokenArray = strtok(NULL, " \t");
        numOfToken++;
    }


    data = (int *) malloc(numOfToken * sizeof(int));

    custatoi(tokenArray, data, numOfToken);

    system("PAUSE");
    return 0;
}



void strInput (char str[], int maxChars)
{
    char garbage;
    int k=0;

    str[0]='\0';

    printf("Please type a string of whole numbers (intigers).\n\n");

    while ((k<80) && ((str[k] = getchar()) != '\n'))
        k++;

    /* Clears the keyboard buffer.  */
    if (k==80)
        while((garbage = getchar()) != '\n')
            ;

    /* Place null at the end of the line read in from user */
    str[k]='\0';

    printf("str after input is: %s\n\n", str);
}


void custatoi(char * tokenArray[], int * data, int numOfTok)
{
    int i;

    for (i=0; i < numOfTok; i++)
        data[i] = atoi(tokenArray[i]);
}
  • 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-13T06:54:03+00:00Added an answer on June 13, 2026 at 6:54 am

    I corrected the errors in yours code: There was some mistakes in main(), tokenArray data type was not correct.

    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <string.h>
    #define StrSZE 81
    
    
    void strInput (char str[], int maxChars);
    void custatoi(char*  tokenArray[], int * data, int numOfTok);
    
    
    int main(int argc, char *argv[])
    {
        char str[StrSZE];
    
    
        int maxChars=StrSZE-1, cont=1, numOfToken=0, i=0;
        int* data;
        char* tokenArray[50];    // Declared correctly 
    
        strInput(str, maxChars);
    
    
        tokenArray[i] = strtok(str, " \t");   // Also made a change here!
        while (tokenArray[i])
        {
            printf("token: %s\n", tokenArray[i]);
            i++;
            tokenArray[i] = strtok(NULL, " \t");
            numOfToken++;
        }
    
    
        data = (int *) malloc(numOfToken * sizeof(int));
    
        custatoi(tokenArray, data, numOfToken);
    
        printf("data\n");
        for(i=0;i<numOfToken;i++){
            printf(" %d\n",data[i]);
    
        }
    
        system("PAUSE");
        return 0;
    }
    
    
    
    void strInput (char str[], int maxChars)
    {
        char garbage;
        int k=0;
    
        str[0]='\0';
    
        printf("Please type a string of whole numbers (intigers).\n\n");
    
        while ((k<80) && ((str[k] = getchar()) != '\n'))
            k++;
    
        /* Clears the keyboard buffer.  */
        if (k==80)
            while((garbage = getchar()) != '\n')
                ;
    
        /* Place null at the end of the line read in from user */
        str[k]='\0';
    
        printf("str after input is: %s\n\n", str);
    }
    
    
    void custatoi(char*  tokenArray[], int * data, int numOfTok)
    {
        int i;
    
        for (i=0; i < numOfTok; i++)
            data[i] = atoi(tokenArray[i]);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to take user input directly from console using getline into a
I was trying to make a code that will take input from a user
I'm trying to take the user input, which may or may not have a
I am trying to receive user input and then put it into a list.
I'm trying to take a user input, turn it into a JS variable, and
I am trying to take 2 dimensional char data from user, but it's not
I'm trying to take user form input and display it back to the user,
I am trying to using the JS to take user input and modify certain
I am trying to display a Spinner list for user to select, then take
I am trying to take input from the user and want to compile regex

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.