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();
}

Wednesday, January 4, 2012

Hello world!

Welcome to WordPress.com. After you read this, you should delete and write your own post, with a new title above. Or hit Add New on the left (of the admin dashboard) to start a fresh post.

Here are some suggestions for your first post.

  1. You can find new ideas for what to blog about by reading the Daily Post.

  2. Add PressThis to your browser. It creates a new blog post for you about any interesting  page you read on the web.

  3. Make some changes to this page, and then hit preview on the right. You can always preview any post or edit it before you share it to the world.

Monday, January 2, 2012

ASCII values printing on CONSOLE

/*program for printing ASCII values*/

#include

void main()
{
int x;
char p;
clrscr();
printf("ASCII values\n");
x=1;
while(x<=255)
{
printf("%d--%c\t",x,x);
x++;
}

getch();
}

ASCII values printing on CONSOLE

/*program for printing ASCII values*/

#include

void main()
{
int x;
char p;
clrscr();
printf("ASCII values\n");
x=1;
while(x<=255)
{
printf("%d--%c\t",x,x);
x++;
}

getch();
}

DECIMAL to BINARY conversion (for a range of decimal values)

/* C program for DECIMAL TO BINARY CONVERSION for a range of values */


#include
void main()
{
int num,i=0,start,end,j;
int bin[10]={0};
clrscr();
printf(" decimal to binary converting\n\n\n");
printf("enter START and END of range: "); //suppose if range is 1to50, then you
scanf("%d%d", &start,&end); //should enter START=1 and END=50
printf("\n\n\t the binary form is :\n\n");
for(num=start;num<=end;num++)
{
i=0;
j=num;
while(j>=1)
{
if(j==1)
{
bin[i]=1;
break;
}
bin[i]=j%2;
j=j/2;
i++;
}

printf("%4d -- ",num);
for(i=9;i>=0;i--)printf("%d",bin[i]);
printf("\n");
}

getch();
}

DECIMAL to BINARY conversion (for a range of decimal values)

/* C program for DECIMAL TO BINARY CONVERSION for a range of values */


#include
void main()
{
int num,i=0,start,end,j;
int bin[10]={0};
clrscr();
printf(" decimal to binary converting\n\n\n");
printf("enter START and END of range: "); //suppose if range is 1to50, then you
scanf("%d%d", &start,&end); //should enter START=1 and END=50
printf("\n\n\t the binary form is :\n\n");
for(num=start;num<=end;num++)
{
i=0;
j=num;
while(j>=1)
{
if(j==1)
{
bin[i]=1;
break;
}
bin[i]=j%2;
j=j/2;
i++;
}

printf("%4d -- ",num);
for(i=9;i>=0;i--)printf("%d",bin[i]);
printf("\n");
}

getch();
}

C program for DECIMAL to BINARY conversion

/* program for DECIMAL to BINARY converter */

#include
void main()
{
int num,i=0,bin,next;
clrscr();
printf(" decimal to binary converting\n\n\n");
printf("enter any decimal no. ");
scanf("%d", &num);
printf("please note that the binary form is represented in the\n form
(LSB ...MSB)\n");
printf("\n\n\t The binary form is :");


while(num>=2)
{
bin=num%2;
num=num/2;
printf("%d",bin); //logic can easily implemented using ARRAYs
if(num==1) //but i am not using arrays here.
printf("1"); //this is the MSB
}

getch();
}

C program for DECIMAL to BINARY conversion

/* program for DECIMAL to BINARY converter */

#include
void main()
{
int num,i=0,bin,next;
clrscr();
printf(" decimal to binary converting\n\n\n");
printf("enter any decimal no. ");
scanf("%d", &num);
printf("please note that the binary form is represented in the\n form
(LSB ...MSB)\n");
printf("\n\n\t The binary form is :");


while(num>=2)
{
bin=num%2;
num=num/2;
printf("%d",bin); //logic can easily implemented using ARRAYs
if(num==1) //but i am not using arrays here.
printf("1"); //this is the MSB
}

getch();
}