Friday, June 17, 2016

Happy Programming : My own itoa function and string reversing

Even though I had programs and info while writing my previous post, thought to go for new post which will increase the post count :)

When I did my previous program, then only I wrote my own itoa function (may have limitations) but am putting here for easy traversing. When I had a consraint to not to use any library function, then only this program came out..!

//implementation of itoa - a is the given number

my_itoa ()
{
    char ptr[10];
    int i=0, temp;
    temp = a;

    for(i=0; temp!=0 ; i++)
    {
        ptr[i] = temp%10 + 48;
        temp = temp / 10;
    }
    ptr[i]='\0';
}

Also, I had done string reversing without using temporary string, long back but jus putting here.

//reversing a string
reverse_str(char *ptr)
{
  int i, j, k;
  for(i=0, j=strlen(ptr)-1; i
  {
    k = ptr[i];
    ptr[i] = ptr[j];
    ptr[j]=k;
  }
}

Happy programming...

No comments:

Post a Comment