Write a 'C' program to store the records of student (stud_no,stud_name,stud_addr,stud_percentage )in a file using structure.



#include<stdio.h>
#include<conio.h>
void main()
{
  struct stud
{
int sno;
float per;
char name[20],add[20];
}s;
  int i,n;
  char ch;
  FILE *fp;
  fp=fopen("a.txt","w");
  if(fp==NULL)
  {
  printf("\nUnable to open file!");
  }
  else
   {
    printf("\nEnter the stud no:->");
    scanf("%d",&s.sno);
    printf("\nEnter the stud name:->");
    scanf("%s",s.name);
    printf("\nEnter the stud add:->");
    scanf("%s",s.add);
    printf("\nEnter the stud per:->");
    scanf("%f",&s.per);
    fprintf(fp,"%d\n%s\n%s\n%f",s.sno,s.name,s.add,s.per);
            }
   
  fclose(fp);
  getch();
}
/* output

Enter the stud no:->1
Enter the stud name:->amol
Enter the stud add:->baramati
Enter the stud per:->80*/

Write a ‘C’ Program to sort elements of an array in ascending order using dynamic memory allocation


#include<stdio.h>
#include<conio.h>
#include<alloc.h>
void main()
 {
 int a[5];
 int i,j,n,temp;
 clrscr();
 printf("\n enter the limit of array");
 scanf("\n %d",&n);
a[i]=(int) calloc(n,sizeof(int));
 printf("\n enter the array element");
 for(i=0;i<n;i++)
  {
  scanf("\n %d",&a[i]);
  }
  for(i=0;i<n;i++)
   {
   for(j=i+1;j<n;j++)
            {
            if(a[i]>a[j]){
             temp=a[i];
             a[i]=a[j];
             a[j]=temp;
             }}}
   printf("\n the asending elements are");
   for(i=0;i<n;i++)
            {
            printf("\n %d",a[i]);}
   getch();
  }
/* output

 enter the limit of array5

 enter the array element 56  8 34 1 78
 the asending elements are
 1
 8
 34
 56
 78
*/

Write a ‘C’ Program to count the number of characters, number of words and number of lines from a text file and display the result


#include<stdio.h>
#include<conio.h>
void main()
 {
 FILE *fp;
 char ch;
 int noc=0,now=0,nol=0,not=0;
 clrscr();
 fp=fopen("a.txt","r");
 if(fp==NULL)
  {
  printf("\n unable to open file");
  }
  while((ch=fgetc(fp))!=EOF)
   {
   if(ch==EOF)
   {
            break;
            }
            noc++;
            if(ch==' ')
            {
            now++;
            }
            if(ch=='\n')
            {
            nol++;
            }
            if(ch=='\t')
            {
            not++;
  }
  }
  fclose(fp);
  printf("\n the no of characters are %d",noc);
  printf("\n the no of words are %d",now+nol);
  printf("\n the no of lines are %d",nol);
  printf("\n the no of tabs are %d",not);
  getch();
 }

Write a C program to accept string from the user & replace all occurrences of character ‘a’ by ‘*’ symbol.


#include<stdio.h>
#include<conio.h>
void main()
{
int i=0,flag=0;
char s[50];
clrscr();
printf("\n enter the string:-");
gets(s);

while(s[i]!=NULL)
{
if(s[i]=='a')
{
s[i]='*';
}

i++;
}
printf("\n the string is %s",s);
getch();
}
/* output

 enter the string:- india
 the string is  indi*     */