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...

Searching a number pattern within a big interger..

Again, after a very long time my techblog is getting a post. Really happy to share something..

Once we cross our beginning of our professional career as a Programmer, we reach a stage where we need to manage people or team or some activitiy which will spoil our current interests and passions about programming, also at the same time we are forced to learn and like different kind of activities.

As I travelled couple of times to different countries and worked for different clients, projects what I understood is working is india for longer time is more challenging than working in abroad, especially US. I mean, in US, you can be a individual contributor for your whole life but in India, you cannot or may be very very difficult.

Anyway, apart from that, recently one programming contest had one question which I couldnt do in test and did at home staying till 3AM.


Searching a number pattern within a Big Integer:

When a given number if 123456789, you have to find out occrence of 56 or 567 kind of pattern. I was trying to use itoa() function which was not available which made me to write my own itoa() function. Also, I did in both way by considering as integers as well as by converting both the numbers to strings, which is nothing but subtring searching.

// Searching an number pattern in a whole integer (as a integer)
search_pattern()
{
    int temp, org, i=0;
    temp = a;
    org = b;
    for(; temp!=0 ; )
    {
        if( temp%10 == b%10 )
        {
            i++;
            b = b / 10;
        }
        else if(i>0  &&  b!=0 )
        {
            i = 0;
            b = org;
        }
        temp = temp / 10;
    }
    if(b==0 && i!=0)
        printf("FOUND\n");
    else 
        printf("NOT FOUND\n");
    
    return;
}

Following example is how I did it as strings. Assume that both the numbers are already converted to strings using sprintf or itoa library functions. I will post my own itoa function in my next blog. 


//searching a number pattern within a number (as strings)
search_substring(char *ptr, char *ptr1)
{
    int i, j, k, success=0;

    for(i=0, j=0;i
    {
      if(ptr[i] == ptr1[j])
      {
          j++;
          for(k=i+1;( k
          {
            if(ptr[k] == ptr1[j])
            {
               success = j;
               j++;
            }
            else
            {
               j=0;
               break;
            }            
        }
        if(j == strlen(ptr1))
        {
          printf("Found... :)\n");
          break;
        }
    }
  }
  if(j==0)
      printf("Not found..\n");
}

Happy Programming....