The Programming Project

Thursday, January 3, 2013

C Program #8


A program to demonstrate different operation on list using array. You might get unexpected output if two different operation is done on the list in the same run.
#include<stdio.h>
#include<stdlib.h>
void menu(void);
void create(void);
void insert(int a[],int);
void deletee(int a[],int);
void merge(int a[],int);
void split(int a[],int);
void sort(int a[],int);
void search(int a[],int);
void append(int a[],int);
int list[50],list_size,c=0;
int main()
{
menu();
return 0;
}
void menu(void)
{
while(1)
{
int choice;
printf("\n ***********MENU FOR LIST*********** \n");
printf("\n Press 0 to create \n");
printf("\n Press 1 to inserte \n");
printf("\n Press 2 to delete \n");
printf("\n Press 3 to merge \n");
printf("\n Press 4 to split \n");
printf("\n Press 5 to sort \n");
printf("\n Press 6 to search \n");
printf("\n Press 7 to append \n");
printf("\n Press 8 to exit \n");
printf("\n Enter your choice\n");
scanf("%d",&choice);
switch(choice)
     {
     case 0:
     create();
     break;
     case 1:
     insert(list,list_size);
     break;
     case 2:
     deletee(list,list_size);
     break;
     case 3:
     merge(list,list_size);
     break;
     case 4:
     split(list,list_size);
     break;
     case 5:
     sort(list,list_size);
     break;
     case 6:
     search(list,list_size);
     break;
     case 7:
   append(list,list_size);
     break;
     case 8:
     exit(0);
     break;
     default:
     puts("Wrong choice");
     break;
     }
}
return;
}
void split(int list[],int list_size)
{
     if(c==0)
          {
              puts(" LIST NOT CREATED, CREATE LIST FIRST ");
             
          }
     else{
     int b[25],i,j,pos;
   printf("\n List items are: \n");
     for(i=0;i<list_size;i++)
     printf("\t %d",list[i]);
     printf("\n Enter the position at which the list will be splitted:\n");
     scanf("%d",&pos);
   printf("\n List after splitting:\n");
     printf("\n List #1 \n");
     for(i=pos;i<list_size;i++)
          {
          b[i-pos]=list[i];
          printf("\t %d",b[i-pos]);
          }
     printf("\n List #2 \n");
     for(i=0;i<pos;i++)
     printf("\t %d",list[i]);
     list_size=list_size-pos;
   }
}
void create(void)
{
     c++;
   int i;
     printf("\n Enter the list size \n");
     scanf("%d",&list_size);
     printf("\n Enter the elements:\n");
     for(i=0;i<list_size;i++)
          scanf("%d",&list[i]);
}
void insert(int list[],int list_size)
{
     int item,pos,i;
   if(c==0)
          {
              puts(" LIST NOT CREATED, CREATE LIST FIRST ");
             
          }
     else{
     printf("\n Enter the item to be inserted and position\n");
     scanf("%d %d",&item,&pos);
     printf("\n List before insertion \n");
     for(i=0;i<list_size;i++)
          printf("\t %d",list[i]);
     for(i=list_size;i>pos-1;i--)
          list[i]=list[i-1];
          list[pos-1]=item;
     list_size+=1;
     printf("\n List after insertion \n");
     for(i=0;i<list_size;i++)
          printf("\t %d",list[i]);
     }
return ;
}
void deletee(int list[],int list_size)
{
     int item,pos,flag=1,i;
   if(c==0)
          {
              puts(" LIST NOT CREATED, CREATE LIST FIRST ");
             
          }
     else{
     printf("\n Enter the item to be deleted:\n");
     scanf("%d",&item);
     for(i=0;i<list_size;i++)
          {
              if(item==list[i])
                   {
                   pos=i;
                   flag=1;
                   break;
                   }
           else
           flag=0;
           }
     if(flag==0)
              printf("\n Item not found, cannot be deleted:\n");
     else
     {
     printf("\n List before deletion \n");
     for(i=0;i<list_size;i++)
          printf("\t %d",list[i]);
     for(i=pos;i<list_size;i++)
          list[i]=list[i+1];
          list_size-=1;
     printf("\n List after deletion \n");
     for(i=0;i<list_size;i++)
          printf("\t %d",list[i]);
     }
     return ;
     }
}
void merge(int list[],int list_size)
{
     int a[50],n,i;
   if(c==0)
          {
              puts(" LIST NOT CREATED, CREATE LIST FIRST ");
             
          }
     else{
     printf("\n Enter the size of the list to be merged \n");
     scanf("%d",&n);
     printf("\n Enter the items of the new list:\n");
     for(i=0;i<n;i++)
     scanf("%d",&a[i]);
     for(i=list_size;i<list_size+n;i++)
     list[i]=a[i-list_size];
     printf("\n List before merging \n");
     for(i=0;i<list_size;i++)
          printf("\t %d",list[i]);
     list_size+=n;
     printf("\n List after merging \n");
     for(i=0;i<list_size;i++)
          printf("\t %d",list[i]);
      }
     return;
}
void sort(int list[],int list_size)
{
     int i,temp,j;
   if(c==0)
          {
              puts(" LIST NOT CREATED, CREATE LIST FIRST ");
             
          }
     else{
     printf("\n List before sorting \n");
     for(i=0;i<list_size;i++)
          printf("\t %d",list[i]);
     for(i=0;i<list_size;i++)
          {
              for(j=i;j<list_size-1;j++)
                   {
                        if(list[i]<list[j])
                             {
                                  temp=list[i];
                                  list[i]=list[j];
                                  list[j]=temp;
                             }
                   }
          }
     printf("\n List after sorting \n");
     for(i=0;i<list_size;i++)
          printf("\t %d",list[i]);
     }
     return;
}
void search(int list[],int list_size)
{
     int item,flag=1,i,count=0,a[10];
   if(c==0)
          {
              puts(" LIST NOT CREATED, CREATE LIST FIRST ");
             
          }
     else{
     printf("\n List items are: \n");
     for(i=0;i<list_size;i++)
          printf("\t %d",list[i]);
     printf("\n Enter the item to be searched:\n");
     scanf("%d",&item);
     for(i=0;i<list_size;i++)
          {
              if(item==list[i])
                   {
                   a[count]=i;
                   count++;
                   flag=1;
                   continue;
                   }
           else
           flag=0;
           }
     if(flag==0 && count==0)
              printf("\n Item not found:\n");
     else
          {
              printf("\n Item %d is at postion",item);
              for(i=0;i<count;i++)
                   printf("\t %d",a[i]+1);
          }
     }
     return;
}
void append(int list[],int list_size)
{
     int item,pos,i;
   if(c==0)
          {
              puts(" LIST NOT CREATED, CREATE LIST FIRST ");
             
          }
     else{
     printf("\n List items are: \n");
     for(i=0;i<list_size;i++)
          printf("\t %d",list[i]);
     printf("\n Enter the item to be appended & position:\n");
     scanf("%d %d",&item,&pos);
     list[pos-1]=item;
     printf("\n List after appending: \n");
     for(i=0;i<list_size;i++)
          printf("\t %d",list[i]);
     }
     return;
}


Sunday, December 16, 2012

Deletion of text inside brackets : C Code


C program to read a string and remove anything written inside closed brackets '(' ,')':

#include<stdio.h>
int main()
{
char s[200],temp;
int l,i=0,flag=1,j;
printf("\n Enter the string:");
do
      {
       s[i]=getchar();
       }
       while(s[i++]!='\n');
       s[i]='\0';
       i=0;
       while(s[i]!='\0')
       {

        if(s[i]=='(')
                  {
                  while(s[i]!=')')
                  ++i;
                  }
     if(s[i]!=')')   
        printf("%c",s[i]);
     i++;
        }
      return 0;
}
INPUT/OUTPUT:
Enter the string:amhjdg (dshjg)(dbh)hjdv dgfv (a)
OUTPUT: amhjdg hjdv dgfv

C Program #6


/* program to write a sentence is one file and print the entire sentence
to the second file by reversing the non palindrome word only*/
#include<stdio.h>
#include<string.h>
int palin(char c[15]);
int main()
{
FILE *fp,*fq;
char c='a',ct[15];
int i,len,j;
fp=fopen("first.txt","w");
if(fp==NULL)
      {
      printf("\n ERROR opening file:");
      }
else
      {
      printf("\n Enter the text:");
      while(c!='\n')
            {
      scanf("%c",&c);
            fprintf(fp,"%c",c);
            }
      }
fclose(fp);
fq=fopen("first.txt","r");
fp=fopen("second.txt","w");
if(fq==NULL)
      {
      printf("\n ERROR opening file:");
      }
else
      {
      while(!feof(fq))
            {
                  i=0;
                  c=fgetc(fq);
                  while(c!=' ' && c!='\n')
                        {
                        ct[i]=c;
                        i++;
                        c=fgetc(fq);
                        if(c==EOF)break;
                        }ct[i]='\0';
                  if(palin(ct)==0)
                        {
                        fputs(ct,fp);
                        fputc(' ',fp);
                        }
                  else
                        {
                        len=strlen(ct);
                        for(j=len-1;j>=0;j--)
                              {
                        fputc(ct[j],fp);
                              }
                        fputc(' ',fp);
                        }

            }
      }
fcloseall();
fp=fopen("second.txt","r");
      if(fp==NULL)
                  printf("\n ERROR OPENING FILE:");
      else
                  {
         printf("\n Text is the second file:\n");
                  while(!feof(fp))
                        {
                        fscanf(fp,"%c",&c);
                        printf("%c",c);
                        }
                  }
fcloseall();
return 0;
}
int palin(char c[])
{
int len,i,j=0;
char p[15];
len=strlen(c);
for(i=len-1;i>=0;i--)
      {
      p[j]=c[i];
      j++;
      }
      p[j]='\0';
      if(strcmp(p,c)==0)
            return 0;
            else
                  return 1;

}

C Program #5



/* Program to reverse the alternate words of a string */
#include<stdio.h>
#include<string.h>
void reverse(char rev[],int n);
int main()
{
char str[100],rev[10];
int len,i,j,count;
printf("\n Enter a string:");
gets(str);
len=strlen(str);
printf("\n Converted string is:\n");
for(i=0;i<len;i++)
      {
       printf("%c",str[i]);
       if(str[i]==' ')
            {

                  j=i;
                  count=0;
                  while(str[++j]!=' ')
                        {
                        rev[count]=str[j];
                        count ++;
                        }
                        reverse(rev,count);
                        i=j;
            putc(' ',stdout);
                  }

       }
      return 0;
}
void reverse(char rev[],int n)
{
 int i;
      for(i=n-1;i>=0;i--)
            printf("%c",rev[i]);
}
INPUT/OUTPUT:
Enter a string:Program to reverse the alternate words of a string

 Converted string is:
Program ot reverse eht alternate sdrow of a string 

Saturday, December 15, 2012

RSA Encryption : C Code

RSA is an algorithm for public-key cryptography that is based on the presumed difficulty of factoring large integers, the factoring problem. RSA stands for Ron Rivest, Adi Shamir and Leonard Adleman, who first publicly described it in 1977. Clifford Cocks, an English mathematician, had developed an equivalent system in 1973, but it was classified until 1997. A user of RSA creates and then publishes the product of two large prime numbers, along with an auxiliary value, as their public key. The prime factors must be kept secret. Anyone can use the public key to encrypt a message, but with currently published methods, if the public key is large enough, only someone with knowledge of the prime factors can feasibly decode the message.Whether breaking RSA encryption is as hard as factoring is an open question known as the RSA problem. Below is the C program to demonstrate the algorthm (using small prime numbers, not suitable for real life application)

#include<iostream.h>
#include<math.h>
#include<string.h>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
void keygen(void);
void ran(void);
void man(void);
void encrypt(void);
void decrypt(void);
int sqrmul(int,int,int);
int invr1(int,int);
int invr(int,int);
int prime(int);
int gcd(int,int);
void main()
{
      int chk;
      do
      {
      printf("\n-------------------------------------------\n");
      printf("\n*MAIN MENU*");
      printf("\n");
      printf("\t \t \t \n *PROGRAM TO DEMONSTRATE RSA CIPHER         
      SYSTEM*");
      printf("\n--------------------------------------------\n");
      printf("\n Press 1 to ENCRYPT:");
      printf("\n Press 2 to DECRYPT:");
      printf("\n Press 3 to GENERATE KEY:");
      printf("\n Press 4 to EXIT:");
      printf("\n Please enter your choice......");
      scanf("%d",&chk);
      printf("\n");
      switch(chk)
            {
            case 1:
            encrypt();
            break;
            case 2:
            decrypt();
            break;
            case 3:
            keygen();
            break;
            case 4:
            break;
            default:
            printf("\n You have enter a wrong choice!!!!!!!!\n");
            break;
            }
      }
      while(chk!=4);
}

void encrypt()
{
      char pln[100];
      int len,e,n,tm;
      printf("\n Enter the plain text:");
      cin.getline(pln,'\100');
      printf("\n Enter the public key (e,n):");
      scanf("%d %d",&e,&n);
      len=strlen(pln);
      printf("\n The cipher text is:");
      for(int i=0;i<len;i++)
            {
             tm=pln[i];
             printf("%d ",sqrmul(tm,e,n));
             }
      printf("\n Length of cipher text is: %d",len);
      getche();
}
void decrypt()
{
int cip[50],e,n1,l;
printf("\n Enter the lenth:");
scanf("%d",&l);
printf("\n Enter d and n:");
scanf("%d %d",&e,&n1);
printf("\n Enter the cipher text:");
for(int i=0;i<l;i++)
scanf("%d",&cip[i]);
printf("\n The decoded message is: ");
for(int j=0;j<l;j++)
      {
      printf("%c",sqrmul(cip[j],e,n1));
      }
getche();
}
void keygen()
{
int ch;
do
      {
      printf("\n\n");
      printf("\nKEY GENERATOR FOR RSA");
      printf("\nPress 1 for random selection");
      printf("\nPress 2 for manual selection");
      printf("\nPress 3 to return to main menu\n\t\t");
      scanf("%d",&ch);
            switch(ch)
            {
            case 1:
            ran();
            break;
            case 2:
            man();
            break;
            case 3:
            break;
            default:
            printf("\n Wrong choice:");
            }
      }while(ch!=3);
}
void man(void)
{
int u,i,ar[1000],k=0,p,q,ph,n,e;
printf("\n Enter the upper limit:");
scanf("%d",&u);
printf("\n You can choose any two primes from the following prime(s):\n");
for(i=2;i<=u;i++)
      {
      if(prime(i)==1)
            {
            ar[k]=i;
            printf("\t%d(%d)",ar[k],k);
            k++;
            }
      }
printf("\n");
printf("\n Enter  two numbers between 0 and %d :",k-1);
scanf("%d %d",&p,&q);
printf("\n");
printf("\n Your choice of p and q is:%d %d",ar[p],ar[q]);
ph=(ar[p]-1)*(ar[q]-1);
n=(ar[p])*(ar[q]);
printf("\n");
printf("\n phi(%d) is: %d",ar[p]*ar[q],ph);
int z=0;
printf("\n");
printf("\n You can choose any one number for 'e':\n");
for(i=2;i<ph;i++)
      {
      if(gcd(i,ph)==1)
            {
            ar[z]=i;
            printf("\t%d(%d)",ar[z],z);
            z++;
            }
      }
printf("\n");
printf("\n Enter  one number between 0 and %d :",z-1);
scanf("%d",&e);
printf("\n");
printf("\n Your choice of e is:%d",ar[e]);
printf("\n");
printf("\n You have chosen the public key:(%d,%d)",ar[e],n);
printf("\n");
printf("\n Private key is: (%d,%d)",invr(ar[e],ph),n);
printf("\n Press any key............");
getche();
}
void ran()
{
int u,i,ar[400],k=0,p,q,ph,n,e;
printf("\n Enter the upper limit:");
scanf("%d",&u);
printf("\n p and q will be seleted automatically from the list below:\n");
printf("\n");
for(i=2;i<=u;i++)
      {
      if(prime(i)==1)
            {
            ar[k]=i;
            printf("\t%d",ar[k]);
            k++;
            }
      }
printf("\n");
p=rand()%k;
do
      {
      q=rand()%k;
      }while(q==p);
printf("\n");
printf("\n Selected value p and q is:%d %d",ar[p],ar[q]);
printf("\n");
ph=((ar[p]-1)*(ar[q]-1));
n=(ar[p])*(ar[q]);
printf("\n");
printf("\n phi(%d) is: %d",n,ph);
printf("\n");
int z=0;
printf("\n");
printf("\n e will be seleted automatically from the list below :\n");
printf("\n");
for(i=2;i<ph;i++)
      {
      if(gcd(i,ph)==1)
            {
            ar[z]=i;
            printf("\t%d",ar[z]);
            z++;
            }
      }
printf("\n");
e=rand()%z;
printf("\n");
printf("\n Selected e is:%d",ar[e]);
printf("\n");
printf("\n The public key:(%d,%d)",ar[e],n);
printf("\n");
printf("\n Private key is: (%d,%d)",invr(ar[e],ph),n);
printf("\n Press any key.........");
getche();
}
int prime(int n)
{
int i,rem,k=0;
for(i=2;i<=sqrt(n);i++)
      {
      rem=n%i;
      if(rem==0)
            {
            k++;
            }
      }
if(k>0)
return 0;
else
return 1;
}
int gcd(int a,int b)
{
int x,y,rem;
x=abs(a);
y=abs(b);
if(x<y)
{
int tmp=y;
y=x;
x=tmp;
}
do
{
rem=x%y;
x=y;
y=rem;
} while(rem!=0);
if(x==1)
return 1;
else
return 0;
}
int sqrmul(int p,int e,int n)
{
      unsigned long int rem,q,a[20],i=19,mod;
while(q!=1)
      {
      rem=e%2;
      q=(e-rem)/2;
      e=q;
      a[i]=rem;
      i--;
      }
      a[i]=1;
      long int k=0,tmp;
for(int j=i;j<20;j++)
{
      if(k==0)
      {
      mod=p%n;
      tmp=mod;
      }
      else
      {
            if(a[j]==1)
            {
            mod=(tmp*tmp*p)%n;
            tmp=mod;
            }
            else
            {
            mod=(tmp*tmp)%n;
            tmp=mod;
            }
      }

      k++;

}
return(tmp);

}
int invr(int a,int b)
{
long int temp=0,inv;
do
      {
      inv=a*temp-1;
      ++temp;
      }
       while(inv%b!=0);
       return (temp-1);
}
int invr1(int e,int n)
{
long int temp=0,inv;
do
      {
      inv=e*temp-1;
      ++temp;
      }
       while(inv%n!=0);
       return(temp-1);
}