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