You are on page 1of 13

1. Write a C program using an array of structure to represent a bank account. Include the following members. i.

Name of the account holder ii. Account number iii. Type of account iv. Balance account in the account The program should read the information about 50 such bank accounts. The program should also allow an account holder to i. ii. iii. View his balance Withdraw an amount permissible as per current balance (a minimum balance of Rs. 1000/- is to be maintained) Deposit an account

Source Code: #include<stdio.h> #include<conio.h> #include<stdlib.h> struct bank {char name[20]; int acc_no; char type [10]; int blnc_amnt; }a[20]; void main(){ int acc_no,choice,tag,n,i,dep_amnt,widrw_amnt; char another='y'; clrscr(); printf("\nENTER THE NUMBER OF ACCOUNT HOLDERS=" ); scanf("%d",&n); for(i=0;i<n;i++){ printf("\nENTER DATA OF %d account holder\n",i+1); printf("Enter Name of %d account holder= ",i+1);

scanf(" %[^\n]s",a[i].name); printf("\nEnter Account no of %d account holder=",i+1); scanf("%d",&a[i].acc_no); printf("\nEnter type of acount="); scanf("%s",a[i].type); printf("\nEnter opening balance="); scanf("%d",&a[i].blnc_amnt); } while(another=='Y'){ printf("\nEnter the account number to search="); scanf("%d",&acc_no); for(i=0;i<n;i++){ if(a[i].acc_no==acc_no) tag=i; } printf("\nto view present balance press 1\nto withdraw an amount press 2\nto deposit some amount press 3"); printf("\nyour choice= "); scanf("%d",&choice); switch(choice){ case 1:printf("\nbalance is=%d",a[tag].blnc_amnt); break; case 2:printf("\nenter the amount you want to withdraw="); scanf("%d",&widrw_amnt); if((a[tag].blnc_amnt-widrw_amnt)>1000) {a[tag].blnc_amnt=a[tag].blnc_amnt-widrw_amnt; printf("\nnew balance=%d",a[tag].blnc_amnt); } else printf("\nsorry.minimum balance of 1000 should be maintained");

break; case 3:printf("\nenter the amount you want to deposit="); scanf("%d",&dep_amnt); a[tag].blnc_amnt=a[tag].blnc_amnt+dep_amnt; printf("\nnew balance after deposit=%d",a[tag].blnc_amnt); break; } printf("\nWant to do again?(y/n)"); fflush(stdin); another=getche(); } }

2. Design a C program to read down loaded documents and find sequences of Abbreviations occurring between first brackets and store each pair of Abbreviations and its full form as a tuple in a separate text file. For example consider the following portion of a document: ... specific decision tree methods include Classification and Regression Trees (CHART) and Chi Square Automatic Interaction Detection (CHAID)... From this generated records in the output text file should look like: <CART><Classification and Regression Trees> <CHAID><Chi Square Automatic Interaction Detection>

Source Code:

#include<stdio.h> #include<conio.h> void main(){ FILE *fp1,*fp2; char file1[50],file2[50]; char ch; int i,cnt; long int pos; clrscr(); printf("Enter source file name: "); scanf("%s",file1); printf("Enter destination file name: "); scanf("%s",file2); fp1=fopen(file1,"r"); fp2=fopen(file2,"w"); while(fscanf(fp1,"%c",&ch)>0){ if(ch=='(') { pos=ftell(fp1); i=0; fprintf(fp2,"< "); while(1){ fscanf(fp1,"%c",&ch); if(ch==')') break; else{ fprintf(fp2,"%c",ch); fprintf(fp2," > < "); fseek(fp1,pos-1,0);

i++;

} }

fseek(fp1,0,1); cnt=0; while(cnt!=i) { fseek(fp1,-2,1); fseek(fp1,0,1); fscanf(fp1,"%c",&ch); if(ch==' ') cnt++; } while(1){ fscanf(fp1,"%c",&ch); if(ch=='(') break; else fprintf(fp2,"%c",ch); fprintf(fp2,">\n"); fseek(fp1,pos+i+1,0); } } printf("Destination file created!!"); fcloseall(); getch(); }

3. Write a program to enter in a file named info the roll number of certain students and the marks obtained by them in physics,chemistry and mathematics. The program reads the information from the keyboard and writes them into the files. Also the program which will take input from the above file info & find the total marks obtained by each student and write all these information (roll,phy mark,chem mark,math marks,total marks,grade, Scholarship) in another file named result. The grade is given in the following way, Grade E A B C D Fail Total Marks 90% and above 80% to 90% 70% to 80% 60% to 70% 50% to 60% Less than 50% Scholarship Yes Yes No No No No

Source Code: #include<stdio.h> #include<process.h> #include<string.h> #include<conio.h> #include<math.h> struct stu_info { int p,c,m,t_m; float sd; char r[12]; }; void main() { stu_info x[50]; int n,i; float per; FILE *fp,*fq; fp=fopen("info.txt","w"); if(fp==NULL) { printf("The could not be open"); getch(); exit(1); } else { printf("Enter the number of students : "); scanf("%d",&n);

for(i=0;i<n;i++){ printf("\n%d Student information : \n",i+1); printf("\nRoll Number : "); scanf("%s",x[i].r); printf("\nMarks of Physics : "); printf("\nMarks of Chemistry : "); printf("\nMarks of Mathematics : "); scanf("%d",&x[i].p); scanf("%d",&x[i].c); scanf("%d",&x[i].m); }

fprintf(fp,"%s %d %d %d\n",x[i].r,x[i].p,x[i].c,x[i].m); fclose(fp); }

fq=fopen("result.txt","w"); if(fq==NULL) { printf("The could not be open"); getch(); else { fprintf(fq,"ROLL SCHOLARSHIP fp=fopen("info.txt","r"); if(fp==NULL){ printf("The could not be open"); getch(); else { while(fscanf(fp,"%s%d%d%d",&x[i].r,&x[i].p,&x[i].c,&x[i].m)!=EOF) for(i=0;i<n;i++) { x[i].t_m=x[i].p+x[i].c+x[i].m; x[i].sd=sqrt(((x[i].p*x[i].p+x[i].c*x[i].c+x[i].m*x[i].m)/3)((x[i].p+x[i].c+x[i].m)/3)*((x[i].p+x[i].c+x[i].m)/3)); per=x[i].t_m/3; if(per>=90){ fprintf(fq,"%s %d %d %f\n",x[i].r,x[i].p,x[i].c,x[i].m,x[i].t_m,x[i].sd); %d %d E exit(1); } PHYSICS SD\n"); CHEMISTRY MATHEMATICS TOTAL GRADE exit(1); }

Yes

break;

if(per>=80&&per<90) { fprintf(fq,"%s %d %d %d %f\n",x[i].r,x[i].p,x[i].c,x[i].m,x[i].t_m,x[i].sd); break; } if(per>=70&&per<80) { fprintf(fq,"%s %d %d %d %f\n",x[i].r,x[i].p,x[i].c,x[i].m,x[i].t_m,x[i].sd); break; } %d B %d A

Yes

No

if(per>=60&&per<70) { fprintf(fq,"%s %d %d %d %f\n",x[i].r,x[i].p,x[i].c,x[i].m,x[i].t_m,x[i].sd); break; } %d C

No

if(per>=50&&per<60){ fprintf(fq,"%s %d %d %d %f\n",x[i].r,x[i].p,x[i].c,x[i].m,x[i].t_m,x[i].sd); break; } if(per<50){ fprintf(fq,"%s %d %d %f\n",x[i].r,x[i].p,x[i].c,x[i].m,x[i].t_m,x[i].sd); break; } fclose(fp); fclose(fq); getch(); } } } } %d %d Fail %d D

No

No

4. Write a program to replace every occurrence of a specified character in a string with another specified character. Replace a with z, b with y etc.

Source Code: #include<stdio.h> #include<conio.h> #include<string.h> void main() { char src[20],rep[20],des[20],pat[20]; int i,j=0; printf("\n enter the string :"); gets(src); printf("\n enter the pattern to be replaced :"); gets(rep); printf("\n enter the pattern with which it is to be replaced :"); gets(pat); for(i=0;src[i]!='\0';i++) { if(src[i]!=' ') { des[j]=src[i]; // printf("\n%c",des[j]); j++; } else { des[j]='\0'; // puts(des); if(strcmp(des,rep)==0) { strcat(des," "); printf(" "); printf("%s",pat); } else { strcat(des," "); printf(" "); printf("%s",des);} j=0; } } // printf("\n%s\n",des); des[j]='\0'; // puts(des); if(strcmp(des,rep)==0) { printf(" "); printf("%s",pat); } else { printf(" "); printf("%s",des); } getch(); }

5. Write a program to count the number of lines, words and characters in a given file and display them. The programs will receive the name as command line argument. The programs also count separately the frequency. 1. Articles 2. Prepositions 3. Verbs 4. Pronouns Source Code: #include<stdio.h> #include<conio.h> #include<string.h> #include<malloc.h> void main() { FILE *fp; char ch,*word; char file1[50]; int w,l,c,a,p,v,pr,i; clrscr(); printf("Enter file name: "); scanf("%s",&file1); fp=fopen(file1,"r"); word=(char*)malloc(20*sizeof(sizeof(char))); w=l=c=a=p=v=pr=i=0; while(fscanf(fp,"%c",&ch)>0) { c++; if(ch==' ') { w++; *(word+i)='\0'; i=0; if(strcmpi(word,"Articles")==0) a++; else if(strcmpi(word,"Prepositions")==0) p++; else if(strcmpi(word,"Verbs")==0) v++; else if(strcmpi(word,"Pronouns")==0) pr++; } else *(word+(i++))=ch; if(ch=='\n') { *(word+i)='\0'; i=0; if(strcmpi(word,"Articles")==0) a++; else if(strcmpi(word,"Prepositions")==0) p++; else if(strcmpi(word,"Verbs")==0) v++; else if(strcmpi(word,"Pronouns")==0) pr++; l++; } }

printf("\nNo. of characters= %d\nNo. of words=%d\nNo. of lines=%d",c,(++w),l); printf("\nArticles=%d\nPrepositions=%d\nVerbs=%d\nPronouns=%d",a,p,v,pr); fcloseall(); getch(); }

5.

Write a program that compares two files two files and prints the lines where they differ.

Source Code: #include<stdio.h> #include<conio.h> #include<process.h> #include<string.h> #include<malloc.h> int check(char *line1, char *line2) { if(strcmp(line1,line2)==0) return 0; else return 1; }

void main() { FILE *fp,*fs; char c1,c2,*line1,*line2; int l1=0,l2=0,flag1=0,flag2=0; clrscr(); fp=fopen("file1.txt","w"); if(ferror(fp)) { printf(" file1 can't be opened"); exit(1); } fs=fopen("file2.txt","w"); if(ferror(fs)) { printf(" file2 can't be opened"); exit(1); } printf("\nEnter some text for file1: "); while((c1=getchar())!='#') { fputc(c1,fp); } fflush(stdin); printf("\nEnter some text for file2:"); while((c1=getchar())!='#') { fputc(c1,fs); } fflush(stdin); fclose(fp); fclose(fs); fp=fopen("file1.txt","r"); if(ferror(fp)) { printf("\nfile1 can't be opened"); exit(1); } fs=fopen("file2.txt","r"); if(ferror(fs)) { printf("\n file2 can't be opened"); exit(1); } line1=(char *)malloc(200*sizeof(char)); line2=(char *)malloc(200*sizeof(char)); while(1) { c1=fgetc(fp);

if(c1!='\n' && c1 !=EOF) { *(line1+l1)=c1; l1++; } else { *(line1+l1)='\0'; l1=0; if(c1==EOF) { flag1=1; break; } } c2=fgetc(fs); if(c2!='\n' && c2!=EOF) { *(line2+l2)=c2; l2++; } else { *(line2+l2)='\0'; l2=0; if(c2==EOF) { flag2=1; break; } } if(c1=='\n' || c2=='\n') { l1=l2=0; if(flag1==1 && flag2==1) { if(check(line1,line2)) printf("\n\n%s\n%s",line1,line2); /*else printf("\nlines are identical");*/ } else if(flag1==1 && flag2==0) { while(1) { c2=fgetc(fs); if(c2!=EOF) { *(line2+l2)=c2; l2++; } else { *(line2+l2)='\0'; break; } } } else if(flag1==0 && flag2==1) { while(1) { c1=fgetc(fp); if(c1!=EOF) { *(line1+l1)=c1; l1++; } else { *(line1+l1)='\0'; break; } } } if(check(line1,line2)) printf("\n%s\n%s",line1,line2); /*else printf("\nFiles are identical");*/ } } getch(); }

You might also like