Friday, July 27, 2012


Tower Of Hanoi Program. 

#include

void towers(int,char,char,char);

void towers(int n,char frompeg,char topeg,char auxpeg)
{
    // If only 1 disk, make the move and return
    if(n==1)
    {
        printf("\nMove disk 1 from peg %c to peg %c",frompeg,topeg);
        return;
    }
    // Move top n-1 disks from A to B, using C as auxiliary
    towers(n-1,frompeg,auxpeg,topeg);
    // Move remaining disks from A to C
    printf("\nMove disk %d from peg %c to peg %c",n,frompeg,topeg);
    // Move n-1 disks from B to C using A as auxiliary
    towers(n-1,auxpeg,topeg,frompeg);
}
main()
{
    int n;
    printf("Enter the number of disks : ");
    scanf("%d",&n);
    printf("The Tower of Hanoi involves the moves :\n\n");
    towers(n,'A','C','B');
    return 0;
}

Thursday, July 26, 2012

Program to validate the given IPv4 address:)


#include
#include

int isValidIp (char *str)
{
    int segs = 0;
    int chcnt = 0;
    int accum = 0;

    if (str == NULL)
        return 0;
    while (*str != '\0')
    {
        if (*str == '.')
        {
            if (chcnt == 0)
                return 0;
            if (++segs == 4)
                return 0;
            chcnt = accum = 0;
            str++;
            continue;
        }
        if ((*str < '0') || (*str > '9'))
            return 0;
        if ((accum = accum * 10 + *str - '0') > 255)
            return 0;
        chcnt++;
        str++;

    }
    if (segs != 3)
        return 0;
    if (chcnt == 0)
        return 0;
    return 1;
}

int main()
{
    char strg[18];
    while(1)
    {
        printf("\n Enter IP : ");
        scanf("%s", strg);
        isValidIp(strg) ? printf("\n Valid IP") : printf("\n Invalid IP");
    }
    return 0;
}