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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T07:57:37+00:00 2026-06-07T07:57:37+00:00

I am converting (from javascript) a program that will take a string of variable

  • 0

I am converting (from javascript) a program that will take a string of variable length (but always under 100 char) and return the data contained in the string in individual variables. This is the first portion of my code, and obviously, I am new to C and programming in general. This code is for the first section of the code, but learning how to properly code this would give me the know how to code the rest.

I need:

  • the first 4 digits to be stored as ‘stringID’
  • the 5th digit to be stored as ‘myindicator’
  • the 6th through (indicator + 6) digits to be stored as ‘var1’

Example input:

‘12345678901234567890123’

Example output:

  • stringID = 1234
  • myindicator = 5
  • var1 = 67890123456

When I run the program, it returns ‘String ID: H>a’ and then the program crashes. Any help would be appreciated. No, this is not homework.

int main()

{
char mystring[100];
char *stringID;
int nep;
int *myindicator;
char *var1;


nep = 0;
printf("Please enter your CODE\n");
scanf("%s", &mystring);

stringID = (char *)malloc(4 * sizeof(char));

if(NULL != stringID)
{

    strncpy(stringID, mystring, 4);
    stringID[4] = '\0';
    free(stringID);
    nep = nep +4;
    printf("stringID: %s\n",myindicator);
}


if(NULL != myindicator)
{
    strncpy(myindicator, (mystring+nep, 1);
    nep++; 
    myindicator = *myindicator - '0';
    printf("Indicator : %d\n",myindicator);
}

var1 = (char *)malloc((nep + 6) * sizeof(char));
if(NULL != var1)
{
    strncpy(var1, mystring+nep, (myindicator+nep+6));
    var1[myindicator+nep+6] = '\0';
    free(var1);

    printf("Var 1: %s", var1);

    nep = nep +myindicator+6;
}

getchar();
return 0;
}
  • 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-07T07:57:38+00:00Added an answer on June 7, 2026 at 7:57 am

    I fixed something, find it in the comments. But you need to check a C language manual…!

    int main()
    {
       char mystring[100];
       char *stringID;
       int nep;
       // Changed to integer, not pointer to int.
       int myindicator;
       char *var1;
    
       nep = 0;
       printf("Please enter your CODE\n");
    
       /*
           This scanf is a bad idea for the same reason for which, below, we take
           care to allocate memory enough for whatever we have to do.
           Should someone input 250 characters in a buffer of size 100, those 150
           extra characters would wreak havoc and possibly endanger the system.
       */
       // scanf("%s", &mystring);
       fgets(mystring, sizeof(mystring)-1, stdin);
       // fgets will read at most "sizeof(mystring)-1", that is, 99 bytes,
       // from "stdin" (STanDard INput), the same as scanf. But it will halt
       // when reaching the limit given. It's up to us to give a "real" limit
       // (nothing stops you from saying 15000 -- even if the true value is 100).
    
       // C strings are made of characters, terminated by a zero byte.
       // So you need 5 here, to store 4 characters
       stringID = (char *)malloc(5 * sizeof(char));
    
       if (NULL == stringID)
       {
           // Serious out of memory error: no sense going on.
           // fprintf(stderr, "Out of memory\n");
           abort();
       }
    
       strncpy(stringID, mystring, 4);
       stringID[4] = '\0';
    
       printf("ID: %s\n", stringID);
    
       free(stringID);
    
       nep = nep + 4;
       printf("NEP: %d\n", nep);
    
       // Now we want to decode the fifth digit.
    
       // I use '0' as character. So if the fifth digit is '0', '0'-'0' will give 0
       // and if it is '9', '9'-'0' will give 9 (the number).
       // The trick does not work with more than one digit, of course.
       myindicator = mystring[nep] - '0';
    
       // Had I wanted to read 3 digits, I would have had to copy them into a 
       // temporary buffer, add a zero in the fourth position, then run atol()
       // on the resulting buffer: atol("12345\0" /* A STRING */) = 12345 /* A NUMBER */;
    
       printf("VLI : %d\n", myindicator);
    
       // Copy "myindicator" bytes, so alloc myindicator+1 chars
       var1 = (char *)malloc((myindicator + 1) * sizeof(char));
    
       // Check that var1 is not null and abort if it is
       if (NULL == var1)
            abort();
    
       strncpy(var1, mystring + 6, myindicator);
       var1[myindicator+1] = '\0';
    
       // Moved this printf before the free. See why below.
       printf("Prefix : %s\n", var1);
    
       // NEVER use a variable after you freed it!!!
       // it might APPEAR to work, but will stab you in the back the first chance it gets.
       // Good if paranoid habit: null a var as soon as you've freed it.
       free(var1); var1 = NULL;
    
       getchar(); 
       return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In JavaScript, when converting from a float to a string, how can I get
I'm pretty new to jQuery, and I am converting a page from regular javascript
I am converting an encryption script from PHP to JavaScript and am struggling... The
I'm currently building a converter in C#. My program is converting from/to Decimal, Binary
I am converting an application from Flex to Javascript. My workflow within Eclipse for
I'm converting something from VBScript to javascript which runs RegExp() on a textbox to
I'm converting some javascript to jQuery from ExtJS and I don't know what this
I have some JSON data that I get from a server. In my JavaScript,
I am trying to catch redirect response of asp.net from client-side by javascript/jquery but
I am Creating a web application that will get homework from schools website. 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.