end folding algoritmn in c

im try implement end folding with a 10 digit number.  this following code is giving very strange results.  i need to read the number in as a string in order to find the the first 2 digits and the last 3.  i though i could use a for loop and an array but its not working right please help.


#include <stdio.h>
int main()
{
char record_key[10];
char start[2];
char middle[5];
char end[4];
int i,c=0;
printf("\n\nEnter record key ");
gets(record_key);
//get the first 2 digits
for(i=0;i<2;i++)
{
      start[i]=record_key[i];
}

start[2]='\0';

//get the middle 5
for(i=3;i<7;i++,c++)
{
      middle[c]=record_key[i];
}

middle[5]='\0';
//last 3 digits
for(i=7;i<10;i++)
{
  end[i]=record_key[i];
}

printf("start is %s\n",start);
printf("middle is %s\n",middle);
printf("end is %s\n",end);
return 0;
}


LVL 2
gerrymcdAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ozoCommented:
You're setting end[7] but you've only declared end[4]
also, end[0] is never set
0
ozoCommented:
middle[4] is also not set, and middle[5] is invalid since you only delclare middle[5], which allocates the 5 elements middle[0],middle[1],middle[2],middle[3],middle[4]
likewise start[2] is invalid
0
gerrymcdAuthor Commented:
ok ill look into it and come back
0
Amazon Web Services

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

LDCCommented:
include <stdio.h>
                         int main()
                         {
                         char record_key[10];
                         char start[3];
                         char middle[6];
                         char end[4];
                         int i,c=0;
                         printf("\n\nEnter record key ");
                         gets(record_key);
                         //get the first 2 digits
                         for(i=0;i<2;i++)
                         {
                         start[i]=record_key[i];
                         }

                         start[2]='\0';

                         //get the middle 5
                         for(i=3;i<7;i++,c++)
                         {
                         middle[c]=record_key[i];
                         }

                         middle[5]='\0';
                         //last 3 digits
c = 0;                        
for(i=7;i<10;i++,c++)
                         {
                           end[c]=record_key[i];
                         }
end[4] = '\0';
                         printf("start is %s\n",start);
                         printf("middle is %s\n",middle);
                         printf("end is %s\n",end);
                         return 0;
                         }
0
gerrymcdAuthor Commented:
i have made the program implement the end folding algoritmn but even though it gives annser the correct anser it crashes in Turbo c 3.0  i hvae trried it in v c 6.0 and it works no prob can some help me out i keeps crashing after it prints out the anser.  its a dos crash "sayting that this program has called an illegal instruction to be carried out"



/*
code to implement the end folding algorithm
*/
#include <stdio.h>
#include<string.h>
#include<stdlib.h>
long int End_Folding(const char record_key[]);
int main()
{
char record_key[10];

printf("\n\nEnter record key ");
gets(record_key);
End_Folding(record_key);
return 0;
}

long int End_Folding(const char record_key[])
{
      char start[2];
      char middle[5];
      char end[4];
      char start_end[10];
      int i,c=0;
      long int first_loc_avail=16541;
      long int keys=30000;
      long int middle_int,startend_int;
      long int startend_mid=0;
      long remainder,address_location;
      //get the first 2 digits
      for(i=0;i<2;i++)
      {
            start[i]=record_key[i];
      }
      start[2]='\0';
      //get the middle 5
      c=0;
      for(i=2;i<7;i++,c++)
      {
            middle[c]=record_key[i];
      }
      middle[5]='\0';
      c=0;
      //last 3 digits
      for(i=7;i<10;i++,c++)
      {
            end[c]=record_key[i];
      }
      start_end[0]=start[0];
      start_end[1]=start[1];
      start_end[2]=end[0];
      start_end[3]=end[1];
      start_end[4]=end[2];
      start_end[5]='\0';

      printf("start is %s\n",start);
      printf("middle is %s\n",middle);
      printf("end is %s\n",end);
      printf("start end is %s\n",start_end);

      middle_int=atol(middle);
      startend_int=atol(start_end);
      //printf("middle int %ld\n",middle_int);
      startend_mid=middle_int+startend_int;

      //printf("start end mid is %ld\n",startend_mid);
      remainder=startend_mid%keys;
      //printf("remainder is %d\n",remainder);
      address_location=first_loc_avail+remainder;
      printf("Adress location is %ld\n",address_location);

      return 0;
}




0
gerrymcdAuthor Commented:
LDC

sorry ur answer is spot on but i needmore help. ill give u the points if can sort the next prob i got see above commnet by me.
0
ozoCommented:
lint 10238603.c
(39) warning: array subscript cannot be > 1: 2
(46) warning: array subscript cannot be > 4: 5

function returns value which is always ignored
    printf              gets                End_Folding    

declared global, could be static
    End_Folding         10238603.c(23)


char start[2];
/* declares the two elements start[0] and start[1], but you set */
start[2]='\0';
/* which is undefined */
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gerrymcdAuthor Commented:
thanks man thats great.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.