Saturday, 8 September 2018

Write a C/C++ program to identify whether a given line is comment or not.GTU Complier Design 2170701


               Code :

               #include<stdio.h>
               #include<stdlib.h>
               int main()
               {
                    FILE *f1,*f2;
                    f1=fopen("input.c","r");
                    f2=fopen("output.txt","w");
                    char ch=fgetc(f1);
                    int count=1;
                    while(ch!=EOF)
                    {
                                if(ch=='\n')
                                {
                                            count++;
                                }
                                if(ch=='/')
                                {
                                            ch=fgetc(f1);
                                            if(ch=='/')
                                            {
                                                        fprintf(f2,"\nSingle line comment at line %d",count);
                                            }
                                            else if(ch=='*')
                                            {
                                                        fprintf(f2,"\nMultiple line comment start at line %d",count);
                                                        ch=fgetc(f1);
                                                        while(ch!='*')
                                                        {
                                                                    if(ch=='\n')
                                                                    {
                                                                                count++;
                                                                    }
                                                                    ch=fgetc(f1);
                                                        }
                                                        if(ch=='*')
                                                        {
                                                                    ch=fgetc(f1);
                                                                    if(ch=='/')
                                                                    {
                                                                                fprintf(f2," and end at line %d",count);
                                                                    }
                                                        }
                                            }
                                }
                                ch=fgetc(f1);
                    }
                    printf("Check the output in outpt.txt");
                    return 0;
               }

Design a lexical analyzer for given language and the lexical analyzer should ignore redundant spaces, tabs and new lines. It should also ignore comments. Although the syntax specification states that identifiers can be arbitrarily long, you may restrict the length to some reasonable value. Simulate the same in C language. GTU Complier Design (2170701)


#include<stdio.h>
#include<ctype.h>
#include<string.h>
void keyw(char *p);
int i=0,id=0,kw=0,num=0,op=0;
char keys[32][10]={"auto","break","case","char","const","continue","default", "do","double","else","enum","extern","float","for","goto","if","int","long","register","return","short","signed","sizeof","static","struct","switch","typedef","union","unsigned","void","volatile","while"};
void main()
{
     char ch,str[25],seps[15]=" \t\n,;(){}[]#\"<>",oper[]="!%^&*-+=~|.<>/?"; int j;
     FILE *f1;
     f1 = fopen("input.txt","r");
     while((ch=fgetc(f1))!=EOF)
     {
                 for(j=0;j<=14;j++)
                 {
                             if(ch==oper[j])
                             {
                                         printf("%c is an operator\n",ch); op++;
                                         str[i]='\0';
                                         keyw(str);
                             }
                 }
                 for(j=0;j<=14;j++)
                 {
                             if(i==-1) break;
                             if(ch==seps[j])
                             {
                                         if(ch=='#')
                                         {
                                                     while(ch!='>')
                                                     {
                                                                 printf("%c",ch);
                                                                 ch=fgetc(f1);
                                                     }
                                                     printf("%c is a header file\n",ch); i=-1;
                                                     break;
                                         }
                                         if(ch=='"')
                                         {
                                                     do
                                                     {
                                                                 ch=fgetc(f1);
                                                                 printf("%c",ch);
                                                     }
                                                     while(ch!='"');
                                                     printf("\b is a literal\n"); i=-1;
                                                     break;
                                         }
                                         str[i]='\0';
                                         keyw(str);
                             }

                 }
                 if(i!=-1)
                 {
                             str[i]=ch;
                             i++;
                 }
                 else i=0;
     }
     printf("Keywords: %d\nIdentifiers: %d\nOperators: %d\nNumbers: %d\n",kw,id,op,num);
}

void keyw(char *p)
{
     int k,flag=0; for(k=0;k<=31;k++)
     {
                 if(strcmp(keys[k],p)==0)
                 {
                             printf("%s is a keyword\n",p); kw++;
                             flag=1;
                             break;
                 }

     }
     if(flag==0)
     {
                 if(isdigit(p[0]))
                 {
                             printf("%s is a number\n",p); num++;
                 }
                 else     
                 {
                             if(p[0]!='\0')
                             {         
                                         printf("%s is an identifier\n",p); id++;
                             }

                 }

     }
     i=-1;
}

Friday, 20 July 2018

Play Fair Cipher Encryption Decryption GTU

#include<stdio.h>
#include<string.h>
int main()
{
char box[5][5];
char key[25],plain[100];
int i,j,k=0,l=0;
char ch = 'a';
char alpha[25],temp[25];

printf("\nEnter the key");
scanf("%s",key);


for(i=0;i<25;i++)
{
    if(ch=='i')
    {
        i = i-1;
    }
    else
    {
        alpha[i] = ch;
    }
    ch++;
}

i=0,l=0;
while(key[i]!='\0')
{
    for(j=0;j<25;j++)
    {
        if(alpha[j]==key[i])
        {
            alpha[j] = '*';
            break;
        }
    }
    i++;
}
i=0,l=0,k=0;
for(i=0;i<25;i++)
{
    if(alpha[l]=='*')
    {}
    else
    {
        temp[k] = alpha[l];
        k++;
    }
    l++;
}
i=0;
j=0;
for(i=0;key[i]!='\0';i++)
{
    j= j+1;
}

i=0,l=0,k=0;
for(i=j;i<25;i++)
{
        key[i] = temp[l];
        l++;
}
k=0;
for(i=0;i<5;i++)
{
    for(j=0;j<5;j++)
    {
        box[i][j] = key[k];
        k++;
    }
}

printf("\nBOX:\n");

for(i=0;i<5;i++)
{
    for(j=0;j<5;j++)
    {
        printf(" %c ",box[i][j]);
    }
    printf("\n");
}


printf("\nEnter Plain text:");
scanf("%s",plain);
int size=0;
size= strlen(plain);
for(i=0;plain[i]!='\0';i++)
{
    if(plain[i]==plain[i+1])
    {
        for(j=size;j>i;j--)
        {
            plain[j]=plain[size-1];
            size--; 
        }
        plain[j+1] = 'x';   
    }
    printf("%c",plain[i]);
}
size= 0 ;
size=strlen(plain);
if(size%2!=0)
{
    plain[size]='x';
    plain[size+1]='\0';
}

printf("\nFinal Plain text is: %s",plain);
size= 0 ;
size=strlen(plain);
printf("\nEncryption is:");

i=0;j=0,k=0,l=0;
int row1=0,col1=0,row2=0,col2=0,q=0;
char pti,ptj,ci1,ci2;
char te[size];
while(plain[l]!='\0')
{
    pti = plain[l];
    ptj = plain[l+1];
   
    for(i=0;i<5;i++)
    {
        for(j=0;j<5;j++)
        {           
            if(box[i][j] == pti)
            {
                row1 = i;
                col1 = j;
                k++;
            }
           
            if(box[i][j] == ptj)
            {
                row2 = i;
                col2 = j;
                k++;
            }
        }
    }
   
    if(row1 == row2)
    {
        ci1 = box[row1][(col1+1)%5];
        ci2 = box[row2][(col2+1)%5];
       
        te[q] = ci1;
        te[q+1] = ci2;
       
        printf("%c%c",ci1,ci2);
   
    }
    else if(col1 == col2)   
    {
        ci1 = box[(row1+1)%5][col1];
        ci2 = box[(row2+1)%5][col2];
       
        te[q] = ci1;
        te[q+1] = ci2;
       
        printf("%c%c",ci1,ci2);
    }
    else
    {
        ci1 = box[row1][col2];
        ci2 = box[row2][col1];
       
        te[q] = ci1;
        te[q+1] = ci2;
       
        printf("%c%c",ci1,ci2);
    }
   
    l=l+2;
    q=q+2;
}

size= 0 ;
for(i=0;te[i]!='\0';i++)
{
    size=size+1;
}
printf("\nDecryption is:");

i=0;j=0,k=0,l=0;
row1=0,col1=0,row2=0,col2=0,q=0;

while(plain[l]!='\0')
{
    pti = te[l];
    ptj = te[l+1];
   
    for(i=0;i<5;i++)
    {
        for(j=0;j<5;j++)
        {
            if(box[i][j] == pti)
            {
                row1 = i;
                col1 = j;
                k++;
            }
            if(box[i][j] == ptj)
            {
                row2 = i;
                col2 = j;
                k++;
            }
        }
    }
    if(row1 == row2)
    {
        ci1 = box[row1][(col1-1)%5];
        ci2 = box[row2][(col2-1)%5];
        printf("%c%c",ci1,ci2);
    }
    else if(col1 == col2)   
    {
        ci1 = box[(row1-1)%5][col1];
        ci2 = box[(row2-1)%5][col2];
        printf("%c%c",ci1,ci2);
    }
    else
    {
        ci1 = box[row1][col2];
        ci2 = box[row2][col1];
        printf("%c%c",ci1,ci2);
    }
   
    l=l+2;
}
return 0;
}

It's time To increase blogging capability. To have a chance to contribute in digital world. Any Interested People who want to make t...