Tuesday, 24 January 2012

Pattern Matching

c program for pattern matching, pattern matching in c

Pattern matching in c: C programming code to check if a given string is present in another string, For example the string "programming" is present in "c programming". If the string is present then it's location (i.e. at which position it is present) is printed. We create a function match which receives two character pointers and return the position if matching occurs otherwise returns -1. We are implementing naive string search algorithm in our c program



#include<stdio.h>
#include<conio.h>

int match(char*, char*);

void main()
{
    char a[100], b[100];
    int position;
    clrscr();
    printf("Enter some text\n");
    gets(a);
    printf("Enter a string to find\n");
    gets(b);
    position = match(a, b);
    if(position!=-1)
    printf("Found at location %d\n", position+1);
    else
    printf("Not found.\n");
    getch();
}

int match(char *a, char *b)
{
    int c;
    int position = 0;
    char *x, *y;
    x = a;
    y = b;
    while(*a)
    {
        while(*x==*y)
        {
            x++;
            y++;
            if(*x=='\0'||*y=='\0')
            break;
        }
        if(*y=='\0')
        break;
        a++;
        position++;
        x = a;
        y = b;
    }
    if(*a)
    return position;
    else
    return -1;
}

Next Prime Palindrome

C program to find next prime palindrome

C program to find next prime palindrome: In this code user will enter a number(say n) and we have to find a number greater than n which is a palindrome as well as prime. For example if the input is 7 then output will be 11 as it is prime as well as palindrome, if input is 21 then output will be 101. In our code we first check if a number is palindrome and then check if it is prime as it will take less time as primes occur more frequently then palindromes.



#include<stdio.h>
#include<conio.h>
#include<math.h>
#define TRUE 1

void main()
{
    long n, t, b = 0, c, d;
    clrscr();
    printf("Enter an integer\n");
    scanf("%ld",&n);      /* n must be a natural number */
    while(TRUE)
    {
        n++;
        t=n;
        while(t)
        {
            b*=10;   /* Compound assignment operator b*=10 => b=b*10 */
            b+=t%10;
            t/=10;
        }
        if(b==n)
        {
            d=(int)sqrt(n);
            for(c=2; c<=d; c++)
            {
                if(n%c==0)
                break;
            }
            if(c==d+1)
            break;
        }
        b=0;
    }
    printf("%ld\n",n);
    getch();
}

Anagrams

anagram in c

Anagram in c: c program to check whether two strings are anagrams or not, string is assumed to consist of alphabets only. Two words are said to be anagrams of each other if the letters from one word can be rearranged to form the other word. From the above definition it is clear that two strings are anagrams if all characters in both strings occur same number of times. For example "abc" and "cab" are anagram strings, here every character 'a', 'b' and 'c' occur only one time in both strings. Our algorithm tries to find how many times characters appears in the strings and then comparing their corresponding counts.



#include<stdio.h>
#include<conio.h>

int check(char [], char []);

void main()
{
    char a[100], b[100];
    int flag;
    printf("Enter first string\n");
    gets(a);
    printf("Enter second string\n");
    gets(b);
    flag = check(a,b);
    if (flag == 1)
    printf("\"%s\" and \"%s\" are anagrams.\n", a, b);
    else
    printf("\"%s\" and \"%s\" are not anagrams.\n", a, b);
    getch();
}

int check(char a[], char b[])
{
    int first[26] = {0}, second[26] = {0}, c = 0;
    while ( a[c] != '\0' )
    {
        first[a[c]-'a']++;
        c++;
    }
    c = 0;
    while ( b[c] != '\0' )
    {
        second[b[c]-'a']++;
        c++;
    }
    for ( c = 0 ; c < 26 ; c++ )
    {
        if( first[c] != second[c] )
        return 0;
    }
    return 1;
}

Moving Car

#include <graphics.h>
#include <dos.h>
#include <conio.h>

main()
{
    int i, j = 0, gd = DETECT, gm;
    initgraph(&gd,&gm,"D:\\TC\\BGI");
    settextstyle(DEFAULT_FONT,HORIZ_DIR,2);
    outtextxy(25,240,"Press any key to view the moving car");
    getch();
    setviewport(0,0,639,440,1);
    for( i = 0 ; i <= 420 ; i = i + 10, j++ )
    {
        rectangle(50+i,275,150+i,400);
        rectangle(150+i,350,200+i,400);
        circle(75+i,410,10);
        circle(175+i,410,10);
        setcolor(j);
        delay(100);
        if( i == 420 )
        break;
        clearviewport();
    }
    getch();
    closegraph();
    return 0;
}

Captcha Program

#include<stdlib.h>
#include<dos.h>
#include<graphics.h>

main()
{
    int i = 0, key, num, midx, gd = DETECT, gm;
    char a[10];
    initgraph(&gd,&gm,"D:\\TC\\BGI");
    midx = getmaxx()/2;
    settextstyle(SCRIPT_FONT,HORIZ_DIR,5);
    settextjustify(CENTER_TEXT,CENTER_TEXT);
    setcolor(GREEN);
    outtextxy(midx,20,"CAPTCHA");
    settextstyle(SCRIPT_FONT,HORIZ_DIR,2);
    outtextxy(midx,125,"Press any key to change the generated random code \"captcha\"");
    outtextxy(midx,150,"Press escape key to exit...");
    setcolor(WHITE);
    setviewport(100,200,600,400,1);
    setcolor(RED);
    randomize();
    while(1)
    {
        while(i<6)
        {
            num = random(3);
            if ( num == 0 )
            a[i] = 65 + random(26);     /* 65 is the ASCII value of A */
            else if ( num == 1)
            a[i] = 97 + random(26);     /* 97 is the ASCII value of a */
            else
            a[i] = 48 + random(10);     /* 48 is the ASCII value of 0 */
            i++;
        }
        a[i] = '\0';
        outtextxy(210,100,a);
        key = getch();
        if( key == 27 )                     /* escape key*/
        exit(0);
        clearviewport();
        i = 0;
    }
}