Saturday, December 5, 2015

form validation with html and javascript lab

<html>
<head>
  <title>Html form validation</title>
  <script type="text/javascript" src="java.js">
  </script>
</head>
<body>

  <center>
    <form class="forms">
      User Name * <input type="text" id="uname" placeholder="Username">
      <br>
      Password  * <input type="password" id="pass" placeholder="password">
      <br>
      Cell No <input type="text" id="cell" placeholder="Cellno">
       <br>
      Email <input type="text" id="email" placeholder="Email">
      <br>

      Country

      <select>
        <option value="Nepal">Nepal</option>
        <option value="USA">USA</option>
        <option value="UK">UK</option>
        <option value="China">China</option>
      </select>

      <br>

      Sex
      <input type="radio" name="sex" value="male">
      <input type="radio" name="sex" value="female">

      <input type="submit" onclick="formValidation()" value="submit">

    </form>
  </center>
</body>
</html>

Save this with form.html file

 

function formValidation()
{

   
    var cell = document.getElementById("cell").value;
    if(cell.length == 0)
    {
        alert("Enter cell");
        document.getElementById("cell").focus();
    }
   
    var cellpat = /^[0-9]+$/;
    if(cell.match(cellpat))
    {    alert("valid entry");}
    else
    {
        alert("Enter Valid cell");
        document.getElementById("cell").focus();
    }
   

Save this with java.js 

Wednesday, December 2, 2015

Simple HTML form for web technology lab

<html>
<head>
  <title>Html form validation</title>
  <script type="text/javascript">
  </script>
</head>
<body>

  <center>
    <form class="forms">
      User Name * <input type="text" placeholder="Username">
      <br>
      Password  * <input type="password" placeholder="password">
      <br>

      Country

      <select>
        <option value="Nepal">Nepal</option>
        <option value="USA">USA</option>
        <option value="UK">UK</option>
        <option value="China">China</option>
      </select>

      <br>

      Sex
      <input type="radio" name="sex" value="male">
      <input type="radio" name="sex" value="female">

      <input type="submit" value="submit">

    </form>
  </center>
</body>
</html>

Thursday, October 1, 2015

Compiler lab

/* global.h */

#include<stdio.h>  //load i/o routines
#include<ctype.h>  //load character test routines

#define BSIZE 128  //buffer size
#define NONE  -1
#define EOS   '\0'

#define NUM  256
#define DIV  257
#define MOD  258
#define ID   259
#define DONE 260

int tokenval;  //value of token attribute
int lineno;

struct entry {    //from of symbol table entry
char *lexptr;
int token;
};

struct entry symtable[];  //symbol table


/**** lexer.c ************/

#include "global.h"
char lexbuf[BSIZE];
int lineno = 1;
int tokenval = NONE;

int lexan()    //lexical analyzer
{
int t;
while(1){
t = getchar();
if(t==''|| t=='\t')
;              //strip out white space
else if(t=='\n')
lineno = lineno + 1;
else if (isdigit(t)){      //t is a digit
ungetc(t,stdin);
scanf("%d",&tokenval);
return NUM;
}
else if(isalpha(t)){   //tis a letter
int p, b = 0;
while (isalnum(t)){    //t is alphanumeric
lexbuf[b] = t;
t = getchar();
b = b + 1;
if (b>=BSIZE)
error("Compiler Error");
}
lexbuf[b] = EOS;
if (t!=EOF)
ungetc(t, stdin);
p = lookup(lexbuf);
if (p == 0)
p = insert(lexbuf, ID);
tokenval = p;
return symtable[p].token;
}
else if(t == EOF)
return DONE;
else {
tokenval = NONE;
return t;
}
}
}


/******** parser.c **************/

#include "global.h"
int lookahead;

parse()   //parses and translates expression list
{
lookahead = lexan();
while (lookahead = DONE){
expr(); match(';');
}
}

expr()
{
int t;
term();
while(1)
switch (lookahead){
case '+':case '-':
t = lookahead;
match(lookahead); term(); emit(t,NONE);
continue;
default:
return;
}
}

term()
{
int t;
factor();
while(1)
switch(lookahead){
case '*': case '/': case DIV: case MOD:
t = lookahead;
match(lookahead); factor(); emit(t, NONE);
continue;
default:
return;
}
}

factor()
{
switch(lookahead){
case '(':
match('('); expr(); match(')'); break;
case NUM:
emit(NUM, tokenval); match(NUM); break;
case ID:
emit(ID,tokenval); match(ID); break;
default:
error("syntax error");
}
}

match(t)
int t;
{
if (lookahead == t)
lookahead = lexan();
else error("syntax error");
}


/********** Emitter.c ***************/

#include "global.h"

emit(t, tval)    // generates outputs
int t, tval;
{
switch(t){
case '+': case '-': case '*': case '/':
printf("%c\n",t); break;
case DIV:
printf("DIV\n"); break;
case MOD:
printf("MOD\n"); break;
case NUM:
printf("%d\n",tval); break;
case ID:
printf("%s\n",symtable[tval].lexptr); break;
default:
printf("token % d, tokenval %d\n",t,tval);
}
}


/****** symbol.c **************/

#include "global.h"
#define STRMAX 999 //size of lexemes array
#define SYMMAX 100 // size of symtable

char lexemes[STRMAX];
int lastchar = -1;  //last used position in lexemes
struct entry symtable[SYMMAX];
int lastentry = 0;

int lookup(s)
char s[];
{
int p;
for (p = lastentry; p>0 p=p-1)
if(stromp(symtable[p].lexptr, s)==0)
return p;
return 0;
}

int insert(s,tok)  //returns position of entry for s
char s[];
int tok;
{
int len;
len = strlen(s);
if(lastentry + 1 >= SYMMAX)
error("symbol table full");
if (lastchar + len + 1 >=STRMAX)
error("lexemes array full");
lastentry = lastentry + 1;
symtable[lastentry].token = tok;
symtable[lastentry].lexptr = &lexemes[lastchar + 1];
lastchar = lastchar + len + 1;
strcpy(symtable[lastentry].lexptr, s);
return lastentry;
}


/******** init.c **********/

#include "global.h"

struct entry keywords[] = {
"div",DIV,
"mod",MOD,
0,    0
};

init()
{
struct entry *p;
for (p = keywords; p->token; p++)
insert(p->lexptr, p->token);
}


/******** error.c **************/

#include "global.h"

error(m)
char m;
{
fprintf(stderr, "line %d: %s\n", lineno,m);
exit(1);
}


/********* main.c **********/

#include "global.h"
main()
{
init();
parse();
exit(0);
}

Saturday, September 12, 2015

HTML ASSIGNMENT 1

<html>
<head>
<title>Departmental Store </title>
</head>
<body>

<center>
<table border=2 width=920>
<tr>
<td> <img src=sagarmatha.jpg> </td>
<td>THIS IS DEPARMANTAl STORE FOE ALL</td>
<td> <img src=sagarmatha.jpg> </td>
</tr>
</table>

<table cellpadding=40 width=920>
<tr>
<td>Home </td>
<td> About us </td>
<td>Our service </td>
<td>Contact Us </td>
</tr>
</table>

<table border=2 cellpadding=20 width=920>
<tr>
<td rowspan=2> <img src=sagarmatha.jpg> </td>
<td>
<h2>Welcome to XYZ SuperMarket</h2>
</td>
<td rowspan=2>
<ul>
<li><a href="http://www.paypal.com" </a> PAY Vai Paypal </li>
<li><a href="http://www.visa.com" </a> PAY Vai VISA</li>
</ul>
</td>
</tr>

<tr>
<td rowspan=2> <img src=sagarmatha.jpg> <br> <h2>Description</h2> </td>
</tr>

<tr>
<td><img src=sagarmatha.jpg> </td>
<td><img src=sagarmatha.jpg> </td>
</tr>


</table>
</center>


</body>
</html>

Tuesday, April 21, 2015

Rail fence cipher program in C++

#include<iostream>
#include<string.h>
using namespace std;
class RailFence
{
char plain[55],cipher[55],matrix[9][9];
int pl,kl,i,j,k,l,n,m,temp,key[11];

public:
RailFence(){}
void GetPlain()
{
cout<<endl<<"\t\t ***ENTER PLAIN TEXT***"<<endl;
gets(plain);
pl=strlen(plain);
}

void GetKey()
{
cout<<endl<<"\t\t ***ENTER KEY SIZE***"<<endl;
cin>>kl;
cout<<endl<<"\t\t ***ENTER KEY***"<<endl;

for(i=0;i<kl;i++)
{
cin>>key[i];
}
cout<<"KEY IS::";
for(i=0;i<kl;i++)
{
cout<<"\t"<<key[i];
}
cout<<endl;
}

void PlainManipulation()
{
if(pl%kl!=0)
{
temp=((pl/kl)+1)*kl;
for(j=pl;j<temp;j++)
{
plain[j]='x';
}
plain[temp]='\0';
}
else
{
temp=pl;
}
}

void MakeMatrix()
{
k=0;
cout<<"\t";
for(i=0;i<(temp/kl);i++)
{
for(j=0;j<kl;j++,k++)
{
matrix[i][j]=plain[k];
cout<<"\t"<<matrix[i][j];
}
cout<<endl;
cout<<"\t";
}
}

void Encryption()
{
k=1;
m=0;
for(i=0;i<kl;i++)
{
for(j=0;j<kl;j++)
{
if(key[j]==k)
{
k++;
for(l=0;l<temp/kl;l++)
{
for(n=0;n<kl;n++)
{
if(j==n)
{
cipher[m]=matrix[l][n];
m++;
}
}
}
}
}
}
}

void DisplayCipher()
{
cout<<endl<<"\t\t ***CIPHER TEXT IS***"<<endl<<endl<<"\t\t";
for(i=0;i<temp;i++)
{
cout<<cipher[i];
}
cout<<endl<<endl<<"\t\t ***END***"<<endl<<endl;
}

};

int main()
{
RailFence R1;
R1.GetPlain();
R1.GetKey();
R1.PlainManipulation();
R1.MakeMatrix();
R1.Encryption();
R1.DisplayCipher();

return 0;
}

Thursday, April 9, 2015

Vigenere Cipher C++ code

Code
/*
Vigenere Cipher C++ code
Here we assume that plain text length is equal or  more than key length
*/

#include<iostream>
#include<string.h>

using namespace std;

class VigenereCipher
{
char plain[33],cipher[33],key[11];
int i,j,pl,kl;
public:
VigenereCipher(){ }
void GetPlain()
{
cout<<"\t\t\t ***ENTER PLAIN TEXT***"<<endl;
cin>>plain;
pl=strlen(plain);
}
void GetKey()
{
cout<<endl<<"\t\t\t ***ENTER KEY***"<<endl;
cin>>key;
kl=strlen(key);
}
void KeyManip()
{
for(i=0;i<pl;i++)
{
key[i]=key[i%kl];
}
}
void Encryption()
{
for(i=0;i<pl;i++)
{
cipher[i]=(((plain[i]-97)+(key[i]-97))%26)+97;
}
}
void DisplayCipher()
{
cout<<endl<<"\t\t\t ***CIPHER TEXT IS***"<<endl;
for(j=0;j<pl;j++)
{
cout<<cipher[j];
}
cout<<endl<<endl<<"\t\t\t+++END OF PROGRAM+++";
}
};

int main()
{
VigenereCipher v1;
v1.GetPlain();
v1.GetKey();
v1.KeyManip();
v1.Encryption();
v1.DisplayCipher();
return 0;
}

Thursday, April 2, 2015

Bitwise Operation in C and C++

To manipulation of binary (bit) value, we generally perform various operation. Some of them are supported by C and C++ programming language. There are basically six BITWISE  operation available for C and C++ are;


  1. & for Bitwise AND
  2. | for Bitwise OR
  3. ^ for Bitwise Exclusive XOR
  4. << for left shift
  5. >> for right shift
  6. ~ for bitwise NOT
Here is the C++ code example for Bitwise operation

#include<iostream>
using namespace std;

class Bitwise
{
int a,b,r;
public:
Bitwise()
{ }

void GetNumber()
{
cout<<"\t\t ENTER TWO DECIMAL NUMBERS"<<endl;
cin>>a>>b;
}

void BitAnd()
{
r=a & b;
cout<<"\t\t BITWISE AND RESULT"<<endl<<"\t\t"<<r<<endl;
}

void BitOR()
{
r=a | b;
cout<<"\t\t BITWISE OR RESULT"<<endl<<"\t\t"<<r<<endl;
}

void BitXOR()
{
r=a ^ b;
cout<<"\t\t BITWISE XOR RESULT"<<endl<<"\t\t"<<r<<endl;
}

};


int main()
{
Bitwise b1;
b1.GetNumber();
b1.BitAnd();
b1.BitOR();
b1.BitXOR();
return 0;

}

Output is (ALL INPUT AND RESULT ARE IN DECIMAL)

Wednesday, April 1, 2015

lab:5 Simulation of continuous system


Her -> represent forward
<- represent backward
^ represent left or right

code

#include<iostream>
#include<conio.h>
#include<math.h>
using namespace std;

class ContinuousSystem
{
int fc,bc,lrc,i,j,RandNum,x,y;

public:
ContinuousSystem()
{
fc=0;
bc=0;
lrc=0;
x=20;
y=90;
}

void RandGen()
{
for(i=0;i<100;i++)
{
RandNum=rand()%100;

if(RandNum<50)
{
fc++;
cout<<"->\t";
}
else if(RandNum>49 && RandNum<75)
{
bc++;
cout<<"<-\t";
}
else
{
lrc++;
cout<<"^\t";
}
}
}

void Display()
{
cout<<endl<<"fc=="<<fc<<endl;
cout<<"bc=="<<bc<<endl;
cout<<"lrc=="<<lrc<<endl;
}

void CheckDist()
{
if(fc-bc==50)
cout<<"\t\t\t *** YOU ARE IN DISTINATION ***"<<endl;
else
cout<<"\t\t\t *** YOU ARE NOT IN DISTINATION ***"<<endl;
}

};

int main()
{
ContinuousSystem c1;
c1.RandGen();
c1.Display();
c1.CheckDist();


return 0;
}

Monday, March 30, 2015

Hill Cipher Implementation C++

Hill Cipher C++ Code

#include<iostream>
#include<string.h>
using namespace std;

class HillCipher
{
char plain[99],temp[4],cipher1[4],cipher[99],cipher2[99];
int key[3][3],i,j,k,pL,PT[3],n,count,temp2,j1,k1,c[3],z,tt;

public:
HillCipher(){count=0; }

void GetPlain()
{
cout<<"\t\t\t *** ENTER PLAIN TEXT ***"<<endl<<endl;
gets(plain);
}

void PlainModification()
{
pL=strlen(plain);
if(pL%3==1)
{
for(i=pL;i<pL+2;i++)
{
plain[i]='x';
}
plain[i]='\0';
}
if(pL%3==2)
{
for(i=pL;i<pL+1;i++)
{
plain[i]='x';
}
plain[i]='\0';
}
}

void GetKey()
{
cout<<endl<<"\t\t\t *** ENTER KEY ***"<<endl<<endl;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cin>>key[i][j];
}
}
}

void DisplayKey()
{

cout<<"\t\t\t *** KEY IS *** "<<endl<<endl;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<"\t\t"<<key[i][j];
}
cout<<endl;
}
}

void PlainSend()
{
pL=strlen(plain);
for(i=0;i<pL;i=i+3)
{
for(k=0,j=i;k<3,j<i+3;k++,j++)
{
temp[k]=plain[j];
}
temp[3]='\0';
FindNumber();
}

}

void FindNumber()
{
for(n=0;n<3;n++)
{
PT[n]=temp[n]-97;
}
MatricMul();
}

void MatricMul()
{
for(j1=0;j1<3;j1++)
{
temp2=0;
for(k1=0;k1<3;k1++)
{
temp2=temp2+key[k1][j1]*PT[k1];
}
c[j1]=temp2;
}
Encryption();
}

void Encryption()
{
for(n=0;n<3;n++)
{
c[n]=c[n]%26;
}
for(n=0;n<3;n++)
{
cipher1[n]=c[n]+97;
}
cipher1[3]='\0';
UpdateCipher();

}

void UpdateCipher()
{
count++;
tt=(count-1)*3;
for(z=0,n=tt;z<3,n<pL;z++,n++)
{
cipher[n]=cipher1[z];
}

}

void DisplayCipher()
{
cout<<endl<<"\t\t\t *** CIPHER TEXT IS ***"<<endl<<endl<<"\t\t\t ";
for(z=0;z<count*3;z++)
{
cout<<cipher[z];
}
cout<<endl<<endl<<"\t\t\t *** END OF PROGRAM *** "<<endl;
}

};

int main()
{
HillCipher h1;
h1.GetPlain();
h1.PlainModification();
h1.GetKey();
h1.DisplayKey();
h1.PlainSend();
h1.DisplayCipher();

return 0;
}

Saturday, March 21, 2015

Playfair cipher implementation in c

Code

/*
#PLAYFAIR CIPHER C IMPLEMENTATION
*/

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

int main()
{

// START OF INITIALIZATION

char a[9],mk[99],table[5][5],in[88];
int i,k,key_leagth,j,cn=0,sl,ct=0,m=0,inl,c1,c2,r1,r2,tst=0,length_plain;

//END OF INITIALIZATION


// INPUT KEY AND TAKE ALL ALPHABET

printf("\t \t \t*** ENTER KEY ***\n \n \n");
gets(a);
key_leagth=strlen(a);
strcpy(mk,a);
for(i=key_leagth;i<26+key_leagth;i++)
{
mk[i]=97+cn;
cn++;
}

//MAKE UNIQUE 25 ALPHABET

printf("\t \t \t*** REQUIRED MATRIX IS ***\n \n \n");

k=strlen(mk);
while(sl>26)
{
for(i=1;i<k;i++)
{
for(j=0;j<i;j++)
{
if(mk[i]==mk[j] || mk[i]=='j')
{
for(j=i;j<k;j++)
{
mk[j]=mk[j+1];
}
}
}
}
i=1;
sl=strlen(mk);
ct++;
if(sl!=26 && ct==5)
break;
}

//MAKE 5*5 MATRIX

for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
if(mk[m]!='j')
{
table[i][j]=mk[m];

}
m++;
if(mk[m]!='j')
printf("\t%c\t",table[i][j]);
}
printf("\n");
}

//MANIPULATION OF INPUT PLAIN TEXT

printf("\n \n \t \t \t*** ENTER PLAIN TEXT ***\n \n \n");

gets(in);
length_plain=strlen(in);
  for(i=0;i<length_plain;i=i+2)
    {
        if(in[i]==in[i+1])
        {
            for(j=length_plain;j>=i+1;j--)
            {
                in[j+1]=in[j];
            }
            in[i+1]='x';
            length_plain++;
        }
    }
    if(length_plain%2==1)
    {
        in[length_plain+1]='\0';
        in[length_plain]='x';

    }
   
    //CONVERT 'J' TO 'I'
   
    inl=strlen(in);
 
    for(i=0;i<inl;i++)
    {
    if(in[i]=='j')
    in[i]='i';
    }
   
    /*
CIPHER TEXT MAKING
    CORE ENCRYPTION PART
    */
   
    printf("\n \n \t \t \t*** CIPHER TEXT ***\n \n \n");
   

for(k=0;k<inl;k=k+2)
{
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
if(table[i][j]==in[tst])
{

r1=i;
c1=j;
}
if(table[i][j]==in[tst+1])
{
r2=i;
c2=j;
}
}
}
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
if(r1==r1)
{

in[tst]=table[r1][(c1+1)%5];
in[tst+1]=table[r2][(c2+1)%5];
}
if(c1==c2)
{
in[tst]=table[(r1+1)%5][c1];
in[tst+1]=table[(r2+1)%5][c2];
}
if(c1!=c2 && r1!=r2)
{
in[tst]=table[r1][c2];
in[tst+1]=table[r2][c1];
}
}
}

printf("%c%c",in[tst],in[tst+1]);
tst=tst+2;
}
printf("\n \n \t \t \t*** END OF PROGRAM ***\n \n \n");

return 0;

}

Saturday, February 28, 2015

CRYPTOGRAPHY LAB 2(MONOALPHABETIC CIPHER)


CODE

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

int main()
{
    int a[26];
    int i,j,value,flag=0,length;
    char akey[26],plaintext[999],ciphertext[999];
    char alp[26]={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
    for(i=0;i<26;i++)
    {
        value=rand()%26;
        for(j=0;j<i;j++)
        {
            if(a[j]==value)
            {
                flag=1;
            }
        }
        if(flag==0)
            a[i]=value;
        else
        {
            i--;
            flag=0;
        }
    }
    for(i=0;i<26;i++)
    {
        akey[i]=a[i]+65;
        printf("%c \t",akey[i]);
    }
    printf("\n\nenter plain text in capital letter \n");//input plain text
    gets(plaintext);
    length=strlen(plaintext);

    for(i=0;i<length;i++)
    {
        for(j=0;j<26;j++)
        {
           if(plaintext[i]!=32) //FRO SPACE DECTIION
           {
               if(plaintext[i]==alp[j])
               ciphertext[i]=akey[j];
           }
           else
           ciphertext[i]=plaintext[i];

        }

    }
    ciphertext[length]='\0';
    puts(ciphertext);        //display cipher text


}


Cryptography Lab 1 (caesar cipher encryption)




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

int caesar_encryption (int, char);                     //encryption function


int main()
{
char plaintext[999],ciphertext[900];
int key,textlength,i;

printf("enter plain text:\n");
gets(plaintext);

printf("enter value of key\n");                         //input the key
scanf("%d",&key);

textlength=strlen(plaintext);
for (i=0;i<textlength;i++)
{
if(plaintext[i]!=32)
{
ciphertext[i]=caesar_encryption(key, plaintext[i]);
   }
   else{
   ciphertext[i]=plaintext[i];
   }
}
 printf("cipher text: \n");
puts(ciphertext);
return 0;
}

int caesar_encryption (int key, char a)
{

  char b;
b=(((a-97)+key)%26)+97;                                   //core incryprion function
 return b;

}