Tuesday, 14 October 2014

C Program to crate lexical analyzer which will do space removal.

#include
#include
#include
#include

int key = 0;
char expr[100];
char cont[][20]={"CONTROLS","for","do","while","NULL",};
char cond[][20]={"CONDITION","if","then","NULL"};
char oprt[][20]={"OPERATOR","+","-","*","/","%","<","<=",">",">=","=","(",")","NULL"};
char branch[][20]={"BRANCHING","goto","jump" ,"NULL"};

void checking(char[],char[][20]);

void main()
{
int i,j,l,k,m,n;
char sbexpr[50],txt[3];
clrscr();
cout<<"Enter the expression:";
gets(expr);
for(i=0;expr[i]!=NULL;i++)
{
key=0;
for(j=i,k=0;expr[j]!=32 && expr[j]!=NULL;i++,j++,k++)
sbexpr[k]=expr[j];
sbexpr[k]=NULL;

if(key==0) checking(sbexpr,cond);

if(key==0) checking(sbexpr,cont);

if(key==0) checking(sbexpr,branch);

if(key==0)
{
for(m=0;sbexpr[m]!=NULL;m++)
{
key=0;
txt[0]= sbexpr[m];
txt[1] = NULL;

if(key==0) checking(txt,oprt);
if((key==0) || ((sbexpr[m]>=97 && sbexpr[m]<=122) || (sbexpr[m]>=65 && sbexpr[m]<=90)))
{
cout<<"\n"<"<<"Identifier\n";
key = 1;
}
}
}
if(key == 0)
{
cout<<"\n"<"<<"Address\n";
key = 1;
}
}

getch();
}


void checking (char expr[],char check[][20])
{
for(int i=1;strcmp(check[i],"NULL")!=0;i++)
{
if(strcmp(expr,check[i])==0)
{
cout<"<key = 1;
}
}
}

C Program to implement Recursive Deceptive Parsing

#include
#include
#include
void main()
{
int i,j,k;
char a[20],b[20],c[20],m,n[10],o[10];
clrscr();
printf("Enter a recursive string:");
gets(a);
if(a[0]==a[3]) {
m=a[0];
printf("%c\n",m);
}
else {
printf("Entered string is not recursive");
}
j=0;
for(i=4;a[i]!=NULL;i++) {
if(a[i]!='/') {
b[j++]=a[i];
printf("%c",b[j]);
}
else {
goto a1;
}
}
a1:
b[j]='\0';
j=0;
for(i=i+1;a[i]!=NULL;i++) {
c[j]=a[i];
j++;
}
c[j]='\0';
n[0]=m;
n[1]='-';
n[2]='>';
j=3;
for(i=0;c[i]!=NULL;i++) {
n[j]=c[i];
j++;
}
n[j++]=m;
n[j++]='`';
n[j++]='\0';
printf("\n\nOutput string1 is:");
puts(n);
o[0]=m;
o[1]='-';
o[2]='>';
j=3;
for(i=0;b[i]!=NULL;i++)
{
o[j]=b[i];
j++;
}
puts(b);
o[j++]=m;
o[j++]='`';
o[j++]='/';
o[j++]='$';
o[j++]='\0';
printf("\n\nOutput string is:");
puts(o);
getch();
}

C Program to implement Recursive Descentive Parsing.

#include
#include
#include
#include
#include
char ip_sym[15],ip_ptr=0,op[50],tmp[50];
void e_prime();
void e();
void t_prime();
void t();
void f();
void advance();
int n=0;
void e(){
            strcpy(op,"TE'");
 printf("E=%-25s",op);
 printf("E->TE'\n");
            t();
 e_prime();
}
void e_prime(){
int i,n=0,l;
for(i=0;i<=strlen(op);i++)
            if(op[i]!='e')
 tmp[n++]=op[i];
strcpy(op,tmp);
l=strlen(op);
for(n=0;n < l && op[n]!='E';n++);
if(ip_sym[ip_ptr]=='+')            {
                         i=n+2;
 do {
                                    op[i+2]=op[i];
 I++;
                        }while(i<=l);
                         op[n++]='+';
                        op[n++]='T';
                        op[n++]='E';
                        op[n++]=39;
                        printf("E=%-25s",op);
                        printf("E'->+TE'\n");
                        advance();
                        t();
e_prime();
            }
else{
                        op[n]='e';
                        for(i=n+1;i<=strlen(op);i++)
                        op[i]=op[i+1];
                        printf("E=%-25s",op);
                        printf("E'->e");
}
}
void t(){
 int i,n=0,l;
            for(i=0;i<=strlen(op);i++)
            if(op[i]!='e')
            tmp[n++]=op[i];
            strcpy(op,tmp);
            l=strlen(op);
for(n=0;n < l && op[n]!='T';n++);
i=n+1;
            do{
                        op[i+2]=op[i];
                        i++;
            }while(i < l);
            op[n++]='F';
            op[n++]='T';
 op[n++]=39;
            printf("E=%-25s",op);
            printf("T->FT'\n");
 f();
 t_prime();
}
void t_prime(){
int i,n=0,l;
for(i=0;i<=strlen(op);i++)
             if(op[i]!='e')
            tmp[n++]=op[i];
strcpy(op,tmp);
l=strlen(op);
for(n=0;n < l && op[n]!='T';n++);
if(ip_sym[ip_ptr]=='*')            {
                         i=n+2;
 do{
                                    op[i+2]=op[i];
                                    i++;
                        }while(i < l);
                        op[n++]='*';
                        op[n++]='F';
                        op[n++]='T';
                        op[n++]=39;
                        printf("E=%-25s",op);
                        printf("T'->*FT'\n");
                        advance();
                        f();
                        t_prime();
            }
            else{
op[n]='e';
            for(i=n+1;i<=strlen(op);i++)
            op[i]=op[i+1];
            printf("E=%-25s",op);
            printf("T'->e\n");
            }
}
void f(){
int i,n=0,l;
for(i=0;i<=strlen(op);i++)
             if(op[i]!='e')
 tmp[n++]=op[i];
            strcpy(op,tmp);
l=strlen(op);
for(n=0;n < l && op[n]!='F';n++);
            If((ip_sym[ip_ptr]=='i')||(ip_sym[ip_ptr]=='I')){
                        op[n]='i';
                        printf("E=%-25s",op);
                        printf("F->i\n");
                        advance();
            }
            else{
                        if(ip_sym[ip_ptr]=='(')                         {
                                    advance();
                                    e();
                                    if(ip_sym[ip_ptr]==')'){
                                                advance();
                                                i=n+2;
                                                do{
                                                            op[i+2]=op[i];
                                                            i++;
                                                }while(i<=l);
                                    op[n++]='(';
                                    op[n++]='E';
                                    op[n++]=')';
                                    printf("E=%-25s",op);
                                    printf("F->(E)\n");
                        }
            }
            else{
                        printf("\n\t syntax error");
                        getch();
                        exit(1);
                        }
            }
}
void advance(){
            ip_ptr++;
}
void main(){
            int i;
            clrscr();
            printf("\nGrammar without left recursion");
 printf("\n\t\t E->E+T/T \n\t\t T->T*F/F \n\t\t F->(E)/Id ");
 printf("\n Enter the input expression:");
            gets(ip_sym);
            printf("Expressions");
            printf("\t Sequence of production rules\n");
            e();
            for(i=0;i < strlen(ip_sym);i++){
                        if(ip_sym[i]!='+'&&ip_sym[i]!='*'&&ip_sym[i]!='('&&
                        ip_sym[i]!=')'&&ip_sym[i]!='i'&&ip_sym[i]!='I'){
                                    printf("\nSyntax error");
                                    break;
                        }
                        for(i=0;i<=strlen(op);i++)
                        if(op[i]!='e')
                        tmp[n++]=op[i];
                        strcpy(op,tmp);
                        printf("\nE=%-25s",op);
            }
            getch();
}

C Program To Find Follow Of Given String.

#include
#include

intn,m=0,p,i=0,j=0;
char a[10][10],f[10];
void follow(char c);
void first(char c);

int main()
{
inti,z;
charc,ch;
printf("Enter the no.of productions:");
scanf("%d",&n);
printf("Enter the productions(epsilon=$):\n");
for(i=0;i
scanf("%s%c",a[i],&ch);
do{
m=0;
printf("Enter the element whose FOLLOW is to be found:");
scanf(" %c",&c);
follow(c);
printf("FOLLOW(%c) = { ",c);
for(i=0;i
printf("%c ",f[i]);
printf(" }\n");
printf("Do you want to continue(0/1)?");
scanf("%d%c",&z,&ch);
}         
while(z==1);
}
void follow(char c)
{
            if(a[0][0]==c)f[m++]='$';
for(i=0;i
{
for(j=2;j
{
if(a[i][j]==c)
{
if(a[i][j+1]!='\0')first(a[i][j+1]);
if(a[i][j+1]=='\0'&&c!=a[i][0])
follow(a[i][0]);
                                                }
}
}
}
void first(char c){
int k;
if(!(isupper(c)))f[m++]=c;
for(k=0;k
{
if(a[k][0]==c)
{
if(a[k][2]=='$') follow(a[i][0]);
else
if(islower(a[k][2]))f[m++]=a[k][2];

else first(a[k][2]);
}
}

}

C Program To Find First Of Given Sting

#include
#include
void FIRST(char );
intcount,n=0;
charprodn[10][10], first[10];
main(){
inti,choice;
charc,ch;
printf("How many productions ? :");
scanf("%d",&count);
printf("Enter Number Of Productions  :");
for(i=0;i
scanf("%s%c",prodn[i],&ch);
do{
n=0;
printf("Element :");
scanf("%c",&c);
FIRST(c);
printf("\n FIRST(%c)= { ",c);
for(i=0;i
printf("%c ",first[i]);
printf("}\n");
printf("press 1 to continue : ");
scanf("%d%c",&choice,&ch);
}
while(choice==1);
}
void FIRST(char c){
int j;
if(!(isupper(c)))first[n++]=c;
for(j=0;j
if(prodn[j][0]==c){
if(prodn[j][2]=='$') first[n++]='$';
else
if(islower(prodn[j][2]))first[n++]=prodn[j][2];
else FIRST(prodn[j][2]);
}
}

}

Sunday, 24 August 2014

C Program to Draw Pascal's triangle

           1
         1   1
       1   2   1
     1   3   3    1
   1  4    6   4   1
 1  5   10   10  5   1


#include
int main()
{
    int rows,coef=1,space,i,j;
    printf("Enter number of rows: ");
    scanf("%d",&rows);
    for(i=0;i    {
        for(space=1;space<=rows-i;space++)
        printf("  ");
        for(j=0;j<=i;j++)
        {
            if (j==0||i==0)
                coef=1;
            else
               coef=coef*(i-j+1)/j;
            printf("%4d",coef);
        }
        printf("\n");
    }
}

Write a C program to display reverse pyramid.

* * * * * * * * *
  * * * * * * *
    * * * * *
      * * *
        *


 #include
void  main()
{
    int rows,i,j,space;
    printf("Enter number of rows: ");
    scanf("%d",&rows);
    for(i=rows;i>=1;--i)
    {
        for(space=0;space           printf("  ");
        for(j=i;j<=2*i-1;++j)
          printf("* ");
        for(j=0;j            printf("* ");
        printf("\n");
    }
    return 0;
}

Friday, 22 August 2014

C Program to print half pyramid as using number

#include
int main()
{
    int i,j,rows;
    printf("Enter the number of rows: ");
    scanf("%d",&rows);
    for(i=1;i<=rows;++i)
    {
        for(j=1;j<=i;++j)
        {
           printf("%d ",j);
        }
        printf("\n");
    }
    return 0;
}

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

Thursday, 21 August 2014

Pyramid And Structure Using C

#include 
int main()
{
    int i,j,rows;
    printf("Enter the number of rows: ");
    scanf("%d",&rows);
    for(i=1;i<=rows;++i)
    {
        for(j=1;j<=i;++j)
        {
           printf("* ");
        }
        printf("\n");
    }
    return 0;
}
 

o/p
*
* *
* * *
* * * *
* * * * *

Program Of Template Partial Specialization

template 
class sortedVector
{
    public:
    void insert (T val)
    {
        if ( length == vec_size )   // length is the number of elements
        {
            vec_size *= 2;    // we'll just ignore overflow possibility!
            vec_data = new T[vec_size];
        }
        ++length;  // we are about to add an element
        
        // we'll start at the end, sliding elements back until we find the
        // place to insert the new element
        int pos;
        for( pos = length; pos > 0 && val > vec_data[pos - 1]; --pos )
        {
            vec_data[pos] = vec_data[pos - 1];
        }
        vec_data[pos] = val;
    }
    // other functions...
    private:
    T *vec_data;
    int length;
    int size;
};

To declare a partially specialized template that handles any pointer types, we'd add this class declaration


template
class sortedVector
{
    public:
    // same functions as before.  Now the insert function looks like this:
    insert( T *val )
    {
        if ( length == vec_size )   // length is the number of elements
        {
            vec_size *= 2;    // we'll just ignore overflow possibility!
            vec_data = new T[vec_size];
        }
        ++length;  // we are about to add an element
        
        // we'll start at the end, sliding elements back until we find the
        // place to insert the new element
        int pos;
        for( pos = length; pos > 0 && *val > *vec_data[pos - 1]; --pos )
        {
            vec_data[pos] = vec_data[pos - 1];
        }
        vec_data[pos] = val;
    }

    private:
    T** vec_data;
    int length;
    int size;
};

typeid, polymorphic class Using C++ Programing


#include
#include
#include
using namespace std;
class CBase 
virtual void f()
{} 
};
class CDerived : public CBase {};
int main () 
{
try 
{
CBase* a = new CBase;
CBase* b = new CDerived;
cout << "a is: " << typeid(a).name() << '\n';
cout << "b is: " << typeid(b).name() << '\n';
cout << "*a is: " << typeid(*a).name() << '\n';
cout << "*b is: " << typeid(*b).name() << '\n';
catch (exception& e) 
{ cout << "Exception: " << e.what() << endl; 
}
return 0;
}

Type Casting In Class Using C++ Programing


#include
using namespace std;

class CDummy
{
    float i,j;
};

class CAddition
{
int x,y;
  public:
CAddition (int a, int b)
{ x=a; y=b; }
int result()
{ return x+y;}
};

int main ()
{
  CDummy d;
  CAddition * padd;
  padd = (CAddition*) &d;
  cout << padd->result();
  return 0;
}

C++ Example Pointer Program


#include 
#include 
void main()
{
int i=10;
int *Ptr;
clrscr();
Ptr=&i;
cout<<"\nValue Of i :"<cout<<"\nAddress Of i :"<cout<<"\nValue Of Ptr :"<cout<<"\nAddress Of Ptr :"<<&Ptr;
cout<<"\nPtr's Pointer Value:"<<*Ptr;
cout<<"\nPtr Equal to &i :"<<*(&i));

Pointer To Functions In C++


#include
using namespace std;

int addition (int a, int b)
{ return (a+b); }

int subtraction (int a, int b)
{ return (a-b); }

int operation (int x, int y, int (*functocall)(int,int))
{
  int g;
  g = (*functocall)(x,y);
  return (g);
}

int main ()
{
  int m,n;
  int (*minus)(int,int) = subtraction;

  m = operation (7, 5, addition);
  n = operation (20, m, minus);
  cout <  return 0;
}

Example Of Pointer In C++

#include
using namespace std;
int main ()
{
int firstvalue, secondvalue;
int * mypointer;
mypointer = &firstvalue;
*mypointer = 10;
mypointer = &secondvalue;
*mypointer = 20;
cout << "firstvalue is " << firstvalue << endl;
cout << "secondvalue is " << secondvalue << endl;
return 0;
}

Factorial off a number using "do while" loop

#include
#include
void main(){int n,f=1;clrscr();printf("enter any number=");scanf("%d",&n);do{f=f*n;n--;}while(n>0);printf("factorial number is=%d",f);getch();}

Example Of XOR In C


#include
#include

struct xnode
{
int data;
unsigned long direction;
};
struct xnode *add_data(int data, struct xnode* list);
void walk_list(struct xnode *list);
int main(void)
{
struct xnode *l2 = add_data(2, NULL);
struct xnode *l1 = add_data(1, l2);
struct xnode *l3 = add_data(3, l2);
struct xnode *l4 = add_data(4, l3);
printf("front -> back....\n");
walk_list(l1);
printf("back -> front....\n");
walk_list(l4);
return 0;
}
struct xnode *add_data(int data, struct xnode *list)
{
struct xnode *newxnode = malloc(sizeof(struct xnode));
assert(newxnode);
newxnode->direction = (unsigned long)list;
newxnode->data = data;
if(list != NULL)
list->direction ^= (unsigned long)newxnode;
return newxnode;
}
void walk_list(struct xnode *list)
{
unsigned long prev = 0;
while(list != NULL) {
unsigned long next = prev ^ list->direction;
printf("%d ", list->data);
prev = (unsigned long)list;
list = (struct xnode *)next;
}
printf("\n");
}

Wednesday, 20 August 2014

Using C Program Print Fibonacci sequence 1 1 2 3 5 8 13 21 34 55 89 ...

1 1 2 3 5 8 13 21 34 55 89 ...
#include
void main()
{

static int prev_number=0, number=1; // static: so value is not lost

int fibonacci (int prev_number, int number);

printf ("Following are the first 25 Numbers of the Fibonacci Series:\n");

printf ("1 "); //to avoid complexity

fibonacci (prev_number,number);

}


fibonacci (int prev_number, int number)

{
static int i=1; //i is not 0, cuz 1 is already counted in main.
int fibo;

if (i==25)
{
printf ("\ndone"); //stop after 25 numbers

}

else
{
fibo=prev_number+number;
prev_number=number; //important steps

number=fibo;

printf ("\n%d", fibo);
i++; // increment counter

fibonacci (prev_number,number); //recursion

}

}

Program to calculate the factorial of a number. Use the concept of recursion instead of using loops.


#include

void main()

{

int a, fact;


printf("\nEnter any number: ");

scanf ("%d", &a);

fact=rec (a);

printf("\nFactorial Value = %d", fact);

}

rec (int x)

{

int f;


if (x==1)

return (1);

else

f=x*rec(x-1);


return (f);

}

Tuesday, 19 August 2014

C Program For DFA End With 11

#include
#include
void CreateDFA();
int StateMoveInDFA(int,char);
char Letters[10];
int NoOfInputLetter,NoOfStates,NoOfFinalState;
int FinalStates[5];
int DFA[10][10];
void main()
{
int s,i,Accepted;
char Line[10];
CreateDFA();
do
{
s=0;
i=0;
Accepted= 0;
printf("\n\nEnter Input String.. ");
scanf("%s",Line);
while(Line[i]!='\0')
if((s=StateMoveInDFA(s,Line[i++]))<0 p="">break;
for(i=0 ;iif(FinalStates[i] ==s )
Accepted= 1;
(Accepted)?printf("\nInputted String is Valid...") : printf("\nInputed String is Not Valid...");
getch();
printf("\n\nDo you want to continue..??(y/n)");
}
while(getch()=='y' || getch()=='Y');
}
void CreateDFA()
{
int i,j;
char c;
printf("Enter the no. of states.. ");
scanf("%d",&NoOfStates);
printf("Enter the no. of input letters.. ");
scanf("%d",&NoOfInputLetter);
printf("Enter the no. of final states.. ");
scanf("%d",&NoOfFinalState);
for(i=0;iprintf("Final state %d : ",i+1);
scanf("%d",&FinalStates[i]);
}
for(i=0;iprintf("\nInput Letter %d :",i+1);
printf("%c",Letters[i]=getch());
}
printf("\nEnter the State Movements In DFA...\n");
for(i=0;ifor(j=0;jprintf("Move(%d,%c) : ",i,Letters[j]);
scanf("%d",&DFA[i][j]);
}
}
}
int StateMoveInDFA(int s,char c)
{
int j;
for(j=0;jif(c==Letters[j])
return(DFA[s][j]);
return -1;
}

C++ Using Calculator Using Function

#include
#include
#include
void Input (float &x, float &y);

float a=1.0, b=1.0, result;
char operation;


int main ()
{
cout << "Program which performs addition, subtraction, multiplication and subtraction. \n\n";
cout << "Please input calculation operation (eg. 1 + 2): \n";
cin >> a >> operation >> b;
Input (a,b);

cout << "The answer is: " << result << endl;
system ("pause");
return 0;
}


void Input (float &x, float &y)
{
a = x;
b = y;

switch (operation)
{
case '+':
result = x + y;
break;

case '-':
result = x - y;
break;

case '*':
result = x * y;
break;

case '/':
result = x / y;
break;

default:
cout << "Improper operation. Please input a correct calculation operation: \n";
cin >> a >> operation >> b;
Input (a, b);
}
}

Factorial off a number using do-while-loop

#include
#include
void main(){int n,f=1;clrscr();printf("enter any number=");scanf("%d",&n);do{f=f*n;n--;}while(n>0);printf("factorial number is=%d",f);getch();}

C program to find the sum of individual digits of a positive integer


#include

#include

void main()

{

int num, k=1, sum=0;

clrscr();

printf(“Enter the number whose digits are to be added:”);

scanf(“%d”,&num);

while(num!=0)

{

k=num%10;

sum=sum+k;

k=num/10;

num=k;

}

printf(“Sum of the digits:%d”,sum);

getch();

}

C Program to create a linear linked list interactively and print out the list and the total number of items in the list



#include
#include
#define NULL 0
struct linked_list
{
int number;
struct linked_list *next;
};
typedef struct linked_list node; /* node type defined */
main()
{
node *head;
void create(node *p);
int count(node *p);
void print(node *p);
head = (node *)malloc(sizeof(node));
create(head);
printf("\n");
printf(head);
printf("\n");
printf("\nNumber of items = %d \n", count(head));
}
void create(node *list)
{
printf("Input a number\n");
printf("(type -999 at end): ");
scanf("%d", &list -> number); /* create current node */
if(list->number == -999)
{
list->
next = NULL;
}
else /*create next node */
{
list->
next = (node *)malloc(sizeof(node));
create(list->next); */ Recursion occurs */
}
return;
}
void print(node *list)
{
if(list->next != NULL)
{
printf("%d-->",list ->number); /* print current item */
if(list->next->next == NULL)
printf("%d", list->next->number);
print(list->next); /* move to next item */
}
return;
}
int count(node *list)
{
if(list->next == NULL)
return (0);
else return(1+ count(list->next));
}

Assign Value Of Pointer In C

#include<stdio.h>
int main()
{
int num[2] = {10,20};
const int *ptr;

ptr = &num[0];

*ptr = 30;

printf("%d",*ptr);

return(0);
}

Splitting a string into tokens in C

#include 
#include 
#include 

#define MAX_LINE_LEN 256
#define NULL_TERM '\0'

/* A function that will print the content of the token_list */
int printcharlist(char **tok_list)
{
    char **pptr = tok_list;

    while (*pptr) {
        printf ("++ %s\n", *pptr);
        pptr++;
    }

    return 0;
}

/* returns the string between 2 pointers (not forgetting to append NULL_TERM */
char *getStr(char *start, char *end)
{
    int length=end-start;
    int i=0;
    char *result = (char *)malloc (sizeof (char) * (length+1));

    while (i < length) {
    result[i] = *start;
    start++;
    i++;
    }

    result[i] = NULL_TERM;

    return result;
}

Finding Tokens in a String Using Strock

#include
#include
#include

void main()
{
char str[80];
char s[]={' ',',',':',';'};
char *t;

clrscr();
printf("enter value\n");
gets(str);
/* get the first token */
t = strtok(str, s);
printf("token is ::\n");
/* walk through other tokens */
while( t != NULL )
{


printf( " %s\n", t );

t = strtok(NULL, s);
;

}