Tuesday, January 10, 2012

understanding getch(), getche() and getchar() in C

/* program to understand the getch(), getche() and getchar() differences */

//note that these three functions are for getting character from the cosole only but they differ the way they take

#include<stdio.h>
#include<conio.h>
void main()
{
char ch[2],op;
int i=0;
clrscr();
while((i++)<=2)      //just i am rotating this for 3 times
{
printf("\na.getch() \nb. getche() \nc.getchar()\n\t");
puts("enter your option");
ch=getche();

switch(op)
{
case 'a': puts("\nenter char");
ch[0]=getch();      //here character entered is stored but not displayed on console
break;                                             
case 'b':puts("\nenter char");      
ch[1]=getche();   //here char entered is taken(no need to press enter) and displayed on console
break;                                          
case 'c': puts("\nenter char");
ch[2]=getchar();           //here character entered is displayed on console and
break;                           //entered char is stored only after pressing 'enter key'

}
}
printf("\nstored values are %s",ch);
getch();
}

Monday, January 9, 2012

C program for Decoding an Encoded file

/*program to decode an encoded file and storing decoded data into an another file */

#include<stdio.h>
void main()
{
int d,i;
char c;
FILE *fp1,*fp2;
clrscr();
fp1=fopen("resume_e.txt","r");           //open same encoded file in previous post
fp2=fopen("dec_resume.txt","w");

while((c=fgetc(fp1))!=EOF)      
{
d=c;
d=d-30;                     //here you should use the same value ie., in the previous
c=d;                            //post I used d=d+30 so here i should use d=d-30;
fputc(c,fp2);
puts("processing...");              
}
clrscr();
fclose(fp1);
fclose(fp2);
puts("decoding is over");
getch();
}

 

Encoding an Text file using C program

/* program to encode an text file */

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

void main()
{
int d,i;
char c;
FILE *fp1,*fp2;
fp1=fopen("resume.txt","r");         //here i used my resume file, you can use any file but it
fp2=fopen("resume_e.txt","w");  // should be in the BIN folder

while((c=fgetc(fp1))!=EOF)      //main logic
{
d=c;
d=d+30;
c=d;
fputc(c,fp2);
puts("processing...");                 //check by keeping this line before the loop
}
clrscr();
fclose(fp1);
fclose(fp2);
printf("text is encoded and stored in file");
getch();
}

Encoding text using C and storing in FILE

/* program for encoding the text and storing the original and encoded data in files */

#include<stdio.h>
#include<string.h>
void main()
{
char c, data[100], enc[100],enc_bin[8];
int d,i,j;
FILE *fp1,*fp2;
clrscr();
fp1=fopen("original.txt","w");
fp2=fopen("encoded.txt","w");
printf("enter text and press enter --\n\n\t");
i=0;
j=0;

while((c=getchar())!='\n')    //main logic
{
data[i++]=c;
fputc(c,fp1);             //storing the original data in file
d=c;                                //converting char to int (ascii value)
d=d+26;                      // changing its ASCII value by adding 26
c=d;                               // converting back it to char type
fputc(c,fp2);          //putting the encoded text into anotherfile
enc[j++]=c;
}

printf("encoded data is--\n\n\t ");
for(i=0;i<j;i++)
printf("%c",enc[i]);

fclose(fp1);
fclose(fp2);

getch();
}

Encoding text (basic encoding)

/* program for encoding entered text to any symbol (within the ASCII range)*/

#include<stdio.h>
#include<string.h>
void main()
{
char src[100]={0},enc[100]={0},c;
int i=0,d,len;
clrscr();
printf("enter text which you want to encode--");

while((c=getchar())!='\n')        //storing the entered text
 src[i++]=c; 

i=0;                                                                                                        len=strlen(src);

while(i<len)                  //main logic
{
d=src[i];
d=d+13;                      //instead of 13 you can take any value,
c=d;                              // but sum should be <255 because ASCii
enc[i]=c;                  //maximum value is 255
i++;
}

printf("encoded data is :\n\n\t %s",enc);

getch();
}

Converting text to Binary

#include<stdio.h>
#include<string.h>
void main()
{
int d,i=0,j=0,bin[8]={0};
char list[20],c;
clrscr();
printf("enter text press enter at last-");


while((c=getchar())!='\n')        //storing text into array using getchar()
list[i++]=c;


i=0;


while(i<strlen(list))                    //logic starts here
{
d=list[i];                                              // storing character in an dummy var.
j=0;
while(d>=1)                                      // binary conversion logic starts from here
{
if(d==1)
{
bin[j]=1;
break;
}
bin[j]=d%2;
d=d/2;
j++;
}                         


for(j=7;j>=0;j--)                          //this loop is for printing the binary form
printf("%d",bin[j]);
printf("\n");
i++;
}


getch();
}


Friday, January 6, 2012

Conversion of TEXT to BINARY

#include<stdio.h>
#include<string.h>
void main()
{
int d,i=0,j=0,bin[8]={0};
char list[20],c;
clrscr();
printf("enter text press enter at last-");
while((c=getchar())!='n')
list[i++]=c;

i=0;


while(i<strlen(list))
{
d=list[i];
j=0;
while(d>=1)
{
if(d==1)
{
bin[j]=1;
break;
}
bin[j]=d%2;
d=d/2;
j++;
}


for(j=7;j>=0;j--)
printf("%d",bin[j]);
printf("n");
i++;
}


getch();
}