Replacing a character

void main()
{
int replace_char(char *,char,char);
char *str;
char old_char,new_char;
int r;
clrscr();
printf("\n\n\n\t\t\t Program for Replacing a character");

printf("\n\n\n\t\t\t Enter a line of text :");
printf("\n\n\t >> ");

gets(str);

printf("\n\n\t Enter the Character to find = ");
flushall();
scanf("%c",&old_char);

printf("\n\n\t Enter the Character to be replaced = ");
flushall();
scanf("%c",&new_char);

r=replace_char(str,old_char,new_char);

printf("\n\n\n\t\t\t Line after replacing the characters :");
printf("\n\n\t >> %s",str);
printf("\n\n\t Total character replaced = %d",r);




getch();
}

int replace_char(char *s,char o,char n)
{
int i,f=0;
for(i=0;s[i]!='\0';i++)
{
if(s[i]==o)
{
s[i]=n;
f++;
}
}
return(f);
}

Program for writing to and from a file

#include
#include
#include

void main()
{
FILE *fp;
char *str;
int i;
int flag;
char temp[2];
clrscr();

printf("\n\n\n\t\t\t Program for writing to and from a file");

fp=fopen("test.txt","w");

if(fp!=NULL)
printf("\n\n\t\t\t File opened properly...");
else
{
printf("\n\n\t\t\t Error : Unable to open file...");
return 1;
}

printf("\n\n\t\t\t Enter few line of text ('end' to finish )");
printf("\n\n\t >> ");

while(1)
{
fflush(stdin);
gets(str);
fputs(str,fp);

fputs("\n",fp);
if(strcmpi(str,"end")==0)
break;
printf("\t ");


}


flag=fclose(fp);

if(flag==-1)
{
printf("\n\n\t\t\t Error : File was not closed properly...");
exit();
}
else
printf("\n\n\t\t\t File was closed properly...");

printf("\n\n\n\t\t\t Now opening for reading...");
//delay(1500);


fp=fopen("test.txt","r");

if(fp!=NULL)
printf("\n\n\t\t\t File opened properly...");
else
{
printf("\n\n\t\t\t Error : Unable to open file...");
exit();
}

printf("\n\n\n\t >> ");

while(fgets(str,75,fp)!=NULL)
{
i=0;
while(i<3)
{
temp[i]=str[i];
i++;
}

if(strcmpi(temp,"end")==0)
break;

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

}

flag=fclose(fp);

if(flag==-1)
{
printf("\n\n\t\t\t Error : File was not closed properly...");
exit();
}

else
printf("\n\n\t\t\t File was closed properly...");


getch();

}

Graphics - Draw Rectangle

#include
main()
{
int driver,mode,i=10;
driver=0;
mode=VGAHI;
initgraph(&driver,&mode,"\\tc\\bgi");
for(i=10;i<=100;i+10)
{
rectangle(254,236,456,368);
clearviewpart();
delay(500);
moveto(i,i);
}
getch();
restorecrtmode();
closegraph();
}

fibonaci number

main()
{
int n,T ;
clrscr();
printf(" program to generate a fibonaci number");
scanf("d",&n);
fib(n);
return 0;
}
fib( int T )

{
int flast = 1, slast = 0;
int curname;
printf(" febonacci number :\n");
printf("\n");
do
{
curname =flast+slast;
printf("%u\n",curname);
slast=flast;
flast=curname;
T--;
}
while(T>1);
return;
getch();
}

calculating power of a number

/* Problem : sol4_3_2.c
Solution : */




#include
#include


void main()
{
double power(double,int);
double n,ans;
int p;
clrscr();

printf("\n\n\n\t\t\t Program for calculating power of a number");


printf("\n\n\n\t\t\t Enter the base number = ");
scanf("%lf",&n);

printf("\n\t\t\t Enter the power = ");
scanf("%d",&p);

ans=power(n,p);

printf("\n\n\t\t\t Result of %.2lf to power %d is %.2lf",n,p,ans);

getch();

}

double power(double nn,int pp)
{
if(pp==0)
return (1);
else
return(nn*power(nn,pp-1));
}

Finding Maximum & Minimum number from an array

#include
#include

void main()
{
int i,max,min,max_index,min_index;
int num[10];
clrscr();

printf("\n\n\n\t Program for finding Maximum & Minimum number from an
array");

printf("\n\n\n\t\t\t Enter any 10 Numbers");

for(i=0;i<10;i++)
{
printf("\n\n\t\t\t Enter number for num[%d] = ",i);
scanf("%d",&num[i]);
}

max=0;
min=num[0];

for(i=0;i<10;i++)
{
if(num[i]>max)
{
max=num[i];
max_index=i;
}
if(num[i]<=min)
{
min=num[i];
min_index=i;
}
}

printf("\n\n\t\t\t Maximum number is %d at index %d",max,max_index);
printf("\n\n\t\t\t Minimum number is %d at index %d",min,min_index);

getch();

}