Friday, November 15, 2013

C Program to find if Two strings are Rotations of each other

Approach#1: The naive approach will be to find the first character occurrence in another string and then check whether the consecutive characters match with the other strings or not.

Approach#2:
The solution here is pretty simple:
i)  first append string which is rotated, to itself.
ii) find if second string is a substring of above appended string, if yes then return true, else return false.

here's the Implementation of the above algortihm:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int check_rotations(char* str1, char* str2)
{
    int x=strlen(str1);
    int y=strlen(str2);

    if(x!=y)
        return 0;

    char *temp=(char*) malloc(sizeof(char)*(2*x+1));
    strcpy(temp,str1);
    strcat(temp,str1);
    return strstr(temp,str2);

}

int main()
{
    char str1[]="DABC";
    char str2[]="ABCD";
    if(check_rotations(str1,str2))
        printf("strings are rotations of each other");
    else
        printf("strings are not rotations of each other");
}

Viva La Raza
Sid



Program to reverse words in a given string

Today's post is all about reversing strings. Without feeling the need to mention the algorithm, I am jumping straight to code after an example.

Input string: "This is yoyo honey singh"
output: "singh honey yoyo is This"

Here's the implementation:

#include<stdio.h>
#include<stdlib.h>

void reverse(char* str);
void reverse_wrds(char*str1,char*str2);

void reverse(char* s)
{
    char* temp=s;
    char* beg=s;

    while(*temp)
    {
        temp++;
        if(*temp == '\0')
        {
            reverse_wrds(beg,temp-1);
        }
        if(*temp ==' ')
        {
            reverse_wrds(beg,temp-1);
            beg=temp+1;
        }
    }
reverse_wrds(s,temp-1);
}

void reverse_wrds(char* beg,char* end)
{
    char temp;
    while(beg<end)
    {
        temp=*beg;
        *beg++=*end;
        *end--=temp;

    }
}

int main()
{
    char s[]="This is yoyo honey singh";
    char* temp=s;
    reverse(temp);
    puts(temp);
}

This code to handle cases where starting of the string or in the the middle there are extra spaces a slight modification is required. Here's the pseudo code for that:

beg=NULL;
while(*temp)
{
if(beg==NULL && *temp!=' ' )
{
beg=temp;
}

if(beg!=NULL&& (*temp+1==' '|| *temp+1='\0'))
{
reverse_wrds(beg,temp);
beg=NULL;
}
reverse(the whole string)
}

Viva La Raza
Sid


Wednesday, November 13, 2013

C Program to Find Factorial Of LARGE NUMBERS

Today's post is all about calculating factorial. Seems to be easy at first look, calculating factorial is not that easy and you can believe that  from the experience of a guy who has lost a bet and a career opportunity due to this.

yes career opportunity.. that's another story which i will write, but later...

Approach #1:

The naive approach will be to run a loop till that number and storing the multiplication in a variable from 1 to that number and finally displaying the result.
But, there's a flaw in this approach, while multiplying numbers as soon as the data goes out OF RANGE of that number there will be discrepancy in the result, No matter you increase the size of data type. even long long unsigned int is not enough even for calculating the factorial of a small number like 55 or 60.   

Approach#2:

I learned from experience that whenever you are struck with something go back to the basics.
we will do the multiplication here exactly the same way we used to do in our maths class.
We will use arrays to store the number and perform multiplication on single integer index by index.

Here's the code for the implementation:

#include<stdio.h>
#define max 10000
void factorial(int);
void multiply(int);
int length=0;
int fact[max];


void factorial(int num)
{
    int i;
    for(i=2;i<=num;i++)
        multiply(i);
}
void multiply(int num)
{
    long i,rem=0;
    int j;
    int arr[max];
    for(i=0;i<=length;i++)
        arr[i]=fact[i];

    for(i=0;i<=length;i++)
    {
        fact[i]=((arr[i]*num)+rem)%10;
        rem=((arr[i]*num)+rem)/10;
    }

 if(rem!=0)
 {
     while(rem!=0)
     {
         fact[i]=rem%10;
         rem=rem/10;
         i++;
     }
 }

    length=i-1;
}
int main()
{
    int num,i;
    printf("\nEnter number whose factorial is to be calculated\n");
    scanf("%d",&num);
    fact[0]=1;
    factorial(num);
    printf("\nFactorial is: \n");
    for(i=length;i>=0;i--)
        printf("%d",fact[i]);

    return 0;

}

Have fun it's an easy code to understand. Comments below if you have any doubt's or suggestions. :)

Viva La Raza
Sid