Pages

Tuesday, February 12, 2013

A Simple version of Grep with two meta-characters * and ?

int search(char *source, char *pattern){

    int result = 0;

    while(*pattern != '\0'){

        if(*pattern == '*'){

            if(*(pattern+1) == '\0'){

                pattern++;

            }

            else{

                while((*source !='\0') && (*source != *(pattern+1))) source++;

                if(*source == '\0'){

                    result = 1;

                    break;

                }

                else pattern++;

            }

        }

        else if(*pattern == '?'){

            moveNext(&source, &pattern);

        }

        else if ((*pattern >= 'A' && *pattern <= 'Z') ||

                 (*pattern >= 'a' && *pattern <= 'z')) {

            if (*source == *pattern){

                moveNext(&source, &pattern);

            }

            else{

                result = 1;

                break;

            }

        }

    }

    return result;

}

void moveNext(char **s,char **p){

    (*s)++;

    (*p)++;

}

int main()

{

    char *source;

    char *pattern;

    source = (char *) malloc(sizeof(char)*200);

    pattern = (char *) malloc(sizeof(char)*200);

    printf("\tGrep Implementation (allowed operators are *, ?\n");

    printf("\n\tEnter a Source String     : ");

    scanf("%s",source);

    printf("\n\tEnter a pattern to Search : ");

    scanf("%s",pattern);

    if (search(source, pattern) == 0)

        printf("\n\tString found\n");

    else

        printf("\n\tNot Found\n");

    return 0;

}

Friday, November 9, 2012

Getting the Big picture

I am happy to share a nice story told by a senior people in our company.

The story starts like this, a curious journalist wandering along the roadside. He saw some people working in a construction site. He went to some people and asked a question.

The question is "What you are working at?"

First person said that he was laying bricks.

Second one said that he was constructing a wall.

Third one said "I am building my nation".

The journalist wondered and asked him to explain. The worker said, "We are constructing a bridge and using the bridge the two ends will get better connectivity thereby commerce will improve and finally my nation's economic growth will increase."

Among the three workers the third one gave a big picture (goal) of what he is doing.

Similarly we should be aware of "What we are really doing?".

Sunday, July 22, 2012

Simple AJAX demo

In this article, I would like to give a quick demo of how to code a basic AJAX function.

function ajaxfunction(arguments_if_any){
    if (typeof XMLHttpRequest != "undefined"){
        xmlHttp = new XMLHttpRequest();
    }
    else if (window.ActiveXObject){
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }

    if (xmlHttp == null){
        alert ("Browser does not support XMLHTTP Request");
 return false;
    }

    var url;
     
    url = "/application-name/any-serverside-program";
    url += "?name="+value;
        
    xmlHttp.onreadystatechange = callback-function-name;
    xmlHttp.open("GET", url, true);
    xmlHttp.send(null);
    return true;    

}

function callback-function-name(){
    if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
        /* Stuff to do */
 document.getElementById('id').innerHTML = xmlHttp.responseText;
    }
}


Explanation
  1. First the function 'ajaxfunction' is called during a particular event like clicking a div tag or others.
  2. It checks whether the browser is able to send and receive a XMLHttpRequest object through which asynchronous request and response is achieved.
  3. If the support is available, an asynchronous request is made to a server side script (php/JSP) and once the response is received a 'callbackfunction' is executed.
  4. The third parameter in the open() method, in this case true implies that async is turned ON.
  5. The send() method is useful if we are using POST method - we can send large information using it. Example:- xmlHttp.send("fname=Athi&lname=Ruban");
  6. To assign a stream of HTML from a server output to a div tag we use InnerHtml property.