PHP – Split sentence into words


Q.) Ignore ‘Spaces’ and split sentence into words and store into array

Function:

<?php
function splitIt($s){
			$arr = array('.',',');
			$s = str_replace($arr," ",$s);
			$s = preg_replace("/( )+/", " ", $s);
			$s = preg_replace("/^( )+/", "", $s);
			$s = preg_replace("/( )+$/", "", $s);
			$tokens = explode(" ",$s);
			return $tokens;
		}
?>

Input:

 <?php
 $answer = splitIt("   hello world. prt this sentence,please  ");
 print_r($answer);
?>

Output:

 Array ( 
    [0] => hello
    [0] => world
    [0] => prt
    [0] => this
    [0] => sentence
    [0] => please
)

The number which is prime and also palindrome.


After many days of gap…  back to blogging…….

Q.) An integer is said to be a palindrome if it is equal to its reverse. For example, 79197 and 324423 are palindromes. In this task you will be given an integer N, 1 ≤ N ≤ 1000000. You must find the smallest integer M ≥ N such that M is a prime number and M is a palindrome. For example, if N is 31 then the answer is 101.

Input format
A single integer N, (1 ≤ N ≤ 1000000), on a single line.
Output format
Your output must consist of a single integer, the smallest prime palindrome greater than or equal to N

Sol.)

#include<stdio.h>
void main()
{
   long int a;
   printf("Enter number: ");
   scanf("%li",&a);
   if(a <= 1000000)
    while(1)
    {
     if(palindrome(a)){
     if(prime(a)) {printf("Number is: %d",a);getch();return;}
    }
  a=a+1;
}
else printf("Plz enter number less than 1000000 next time");
getch();
}
int prime(long int a){
long int i;
for(i=2;i<=a/2;i++) if(a%i==0)return 0;

return 1;
}

int palindrome(long int a){
  long int temp=a,reverse=0;
 while( temp != 0 ){
  reverse = reverse * 10;
  reverse = reverse + temp%10;
  temp = temp/10;
 }
  if(reverse == a) return 1;
  else return 0;
}

Shivaji varma… 😀

Design buttons using css


<html>
<head>

<style>
.navBtn{
	background-color:#EAECED;
	color:#565656;
	width:80px;

	font-family:"Times New Roman", Times, serif;
	font-size:larger;
	font-weight:bold;
	text-align:center;
	padding:6px;

	cursor:pointer;

	border-radius: 4px 4px 4px 4px;
}

.navBtn a,a:visited{
	text-decoration:inherit;
	color:inherit;
}

.navBtn:hover{
	background-color:#1EB4DA;
	color:#EAECED;
}

.navActive{
	background-color:#FF3C3C;
	color:#EAECED;
}

</style>
</head>
<body>
 <div class="navBtn">1</div>
 <br>
 <div class="navBtn">2</div>
 <br>
 <div class="navBtn">3</div>
</body>
</html>

Recursive Descent Parsing


The following c program parser the given string for the given grammar::

E->E+T

E->T

T->T*F

T->F

F->(E)

F->i


Before writing writing recursive descent parser first eliminate left recursion and left factoring.

E->TEplus

Eplus->+TEplus / ∈ (epsilon)

T ->FTplus

Tplus->*FTplus / ∈ (epsilon)

F-> (E)

F-> i


#include<stdio.h>
#include<string.h>
char string[' '];
int len;
int pos=0;
int E();
int Eplus();
int T();
int Tplus();
int F();
int main()
{
printf("Enter the string:: ");
scanf("%s",&string);
len=strlen(string);
if(E()==0) printf("\nNot Accepted");
else if(pos<len) {printf("\nNot Accepted");}
else printf("\nAccepted");
printf("\n");
return 0;
}
int E()
{
if(T()==0) return 0;
if(Eplus()==0) return 0;
return 1;
}
int Eplus()
{
if(string[pos]=='+')
{ pos++;
if(T()==0) return 0;
if(Eplus()==0) return 0;
}
return 1;
}
int T()
{
if(F()==0) return 0;
if(Tplus()==0) return 0;
return 1;
}
int Tplus()
{
if(string[pos]=='*')
{ pos++;
if(F()==0) return 0;
if(Tplus()==0) return 0;
}
return 1;
}
int F()
{
if(string[pos]=='('){
pos++;
if(E()==0) return 0;
if(string[pos]==')') pos++;
else return 0;
}
else if(string[pos]=='i'){
pos++;}
else {return 0;}
return 1;
}

Don’t copy the code directly from this page it may contain HTML meta content.

Just download this text file(code)>>>>Download<<<<

Operator precedence parser


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

FILE *fp;
char prodFile[]="test.txt";
char relFile[]="table.txt";
int numProductions,numTerminals,top=0,terTop=0,ip=0;
int relationTable[' '][' '];
char stack[' ']="$";
char inputBuffer[' '];
char string[' '];
char handle[' '],handTopTer;
char term[' '];

struct
{
  char left;
  char right[' '];
}prod[' '];

void readProductions()
{
  int i,j;
  char line[30];

 fp=fopen(prodFile,"r");

  for(i=0;fscanf(fp,"%s",line)==1;i++)
  {
    prod[i].left=line[0];
    for(j=2;line[j]!='';j++)
       prod[i].right[j-2]=line[j];

      prod[i].right[j-2]='';
  }

  numProductions=i;

    printf("Productions::\n");
  for(i=0;i<numProductions;i++){
   printf("%c>",prod[i].left);
   printf("%s\n",prod[i].right);
   }
}

int validateNonTer(char z)
{
int i;
   for(i=0;i<numProductions;i++)
   {
     if(z==prod[i].left) {return 1;}
   }
   return 0;
}

void findTerminals()
{
int i,j;
numTerminals=0;

   for(i=0;i<numProductions;i++)
     for(j=0;prod[i].right[j]!='';j++)
        if(!validateNonTer(prod[i].right[j])) 
           {term[numTerminals]=prod[i].right[j];numTerminals++;}

    printf("\nTerminals::\n");
    for(i=0;i<numTerminals;i++)
      printf("%c\n",term[i]);
}

void readRelationTable()
{
   int i,j,k=0;
   fp=fopen(relFile,"r");
    for(i=0;i<=numTerminals;i++)
    for(j=0;j<=numTerminals;j++)
    {
      fscanf(fp,"%d",&relationTable[i][j]);
    }

  printf(" ");

  for(i=0;i<=numTerminals;i++)
  if(i==numTerminals)  printf(" $");
   else printf("%2c ",term[i]);

  for(i=0;i<=numTerminals;i++)
  {
   if(numTerminals==i) printf("\n$");
    else printf("\n%c",term[i]);

    for(j=0;j<=numTerminals;j++)  
         printf("%2d ",relationTable[i][j]);
  }

}

int searchProd(char z[50])
{
  int i;

  for(i=0;i<numProductions;i++)
    if(strcmp(z,prod[i].right)==0) return i;

return -1;
}

int pos(char z)
{
int i;

  for(i=0;i<numTerminals;i++)
   if(term[i]==z) {return i;}
    if(z=='$') return i;
     else return -1;

}

void push(char z)
{
top++;
stack[top]=z;
if(pos(stack[top]) != -1) terTop=top;
printf("push::%c\n",z);
}

void terToprec()
{
terTop--;
while(pos(stack[terTop])== -1) terTop--;
}

char pop()
{
if(pos(stack[top])!= -1) 
        {terToprec();handTopTer=stack[top];}
char temp=stack[top];
printf("pop::%c\n",temp);
top--;
return temp;
}

void reverse(char *r)
{
int len=strlen(r),i;
  for(i=0;i<len/2;i++)
  {
   char temp=r[i];
   r[i]=r[len-1-i];
   r[len-1-i]=temp;
  }
}

int Parse()
{
  int i;
  char a,b;

   printf("\n\nEnter string::");
   scanf("%s",string);

for(i=0;string[i]!='';i++)
  inputBuffer[i]=string[i];

  inputBuffer[i]='$';

  while(1)
  {
    if(stack[terTop]=='$' && inputBuffer[ip]=='$') return 1;

   else {
     a=stack[terTop];
     b=inputBuffer[ip];
     printf("\na::%c  b::%c\n",a,b);
     if(relationTable[pos(a)][pos(b)]== 1 ||
relationTable[pos(a)][pos(b)]==0)
     {push(b);ip++;}

     else if(relationTable[pos(a)][pos(b)]==2)
       {
        int k=0;
         handle[k]=pop();
         if(pos(handle[k])== -1) {k++;handle[k]=pop();}
          k++;

  while(relationTable[pos(stack[terTop])][pos(handTopTer)] 
       != 1)
          {handle[k]=pop();k++;}

          while(terTop!=top) {handle[k]=pop();k++;}

          handle[k]='';
          reverse(handle);

          printf("Handle  %s\n",handle);

          if(searchProd(handle)!=-1)
            push(prod[searchProd(handle)].left);
          else return 0;
     }
   else return 0;
  }
  }
}

int main()
{
  readProductions();
  findTerminals();
  readRelationTable();
   if(Parse()) printf("Accepted");
   else printf("InValid");

getch();
}

Don’t copy the code directly from this page it may contain HTML meta content.

Just download this text file(code)>>>>Download<<<<

PHP-MySQL Connection


For connecting to MySQL database, PHP offers several inbuilt functions and hence makes the work a lot easier.
mysql_connect()
mysql_close()
mysql_create_db()

to list a few. These are the most used functions for referring the database.
Here is the simple example:

$link = mysql_connect(‘localhost’, ‘mysql_user’, ‘mysql_password’);if (!$link) {

die(‘Could not connect: ‘ . mysql_error());

}

echo ‘Connected successfully’;

mysql_close($link);

DDL , DML, DCL and TCL


What is the difference between DDL, DML, DCL and TCL? Well look below…

DDL

Data Definition Language (DDL) statements are used to define the database structure or schema. Some examples:

CREATE – to create objects in the database
ALTER – alters the structure of the database
DROP – delete objects from the database
TRUNCATE – remove all records from a table, including all spaces allocated for the records are removed
COMMENT – add comments to the data dictionary
RENAME – rename an object

DML

Data Manipulation Language (DML) statements are used for managing data within schema objects. Some examples:
SELECT – retrieve data from the a database
INSERT – insert data into a table
UPDATE – updates existing data within a table
DELETE – deletes all records from a table, the space for the records remain
MERGE – UPSERT operation (insert or update)
CALL – call a PL/SQL or Java subprogram
EXPLAIN PLAN – explain access path to data
LOCK TABLE – control concurrency

DCL

Data Control Language (DCL) statements. Some examples:

GRANT – gives user’s access privileges to database
REVOKE – withdraw access privileges given with the GRANT command

TCL

Transaction Control (TCL) statements are used to manage the changes made by DML statements. It allows statements to be grouped together into logical transactions.
COMMIT – save work done
SAVEPOINT – identify a point in a transaction to which you can later roll back
ROLLBACK – restore database to original since the last COMMIT
SET TRANSACTION – Change transaction options like isolation level and what rollback segment to use