Write a program that merges lines alternately from two files and writes the results to new file. If one file has less number of lines than the other, the remaining lines from the larger file should be simply copied into the target file.
#include "stdio.h"
void main()
{
FILE *fp,*ft,*fs;
char ch;
int f1=1,f2=1,counter=1;
fp=fopen("c:\\ctxt\\first.txt","r");
ft=fopen("c:\\ctxt\\second.txt","r");
fs=fopen("c:\\ctxt\\combine.txt","w");
while(1)
{
if(counter==1||f2==0)
{
ch=fgetc(fp);
if(ch==EOF)
f1=0;
if(ch=='\n'||ch==46)
counter=2;
if(f1!=0)
fputc(ch,fs);
}
if(counter==2||f1==0)
{
ch=fgetc(ft);
if(ch==EOF)
f2=0;
if(ch=='\n'||ch==46)
counter=1;
if(f2!=0)
fputc(ch,fs);
if(f1==0&&f2==0)
break;
}
}
fclose(fp);
fclose(ft);
fclose(fs);
}
Wednesday, November 11, 2009
Chap12[C]e Lowercase to uppercase in file
Write a program to copy one file to another. While doing so replace all lowercase characters to their equivalent uppercase characters.
#include "stdio.h"
void main()
{
FILE *fp,*ft;
char ch;
fp=fopen("c:\\ctxt\\txt1.txt","r");
ft=fopen("c:\\ctxt\\txt2.txt","w");
if(fp==NULL)
{
puts("Cannot open file");
exit();
}
if(ft==NULL)
{
puts("Cannot open file");
exit();
}
while(1)
{
ch=fgetc(fp);
if(ch==EOF)
break;
if(ch>=97&&ch<=122)
ch-=32;
fputc(ch,ft);
}
fclose(fp);
fclose(ft);
}
#include "stdio.h"
void main()
{
FILE *fp,*ft;
char ch;
fp=fopen("c:\\ctxt\\txt1.txt","r");
ft=fopen("c:\\ctxt\\txt2.txt","w");
if(fp==NULL)
{
puts("Cannot open file");
exit();
}
if(ft==NULL)
{
puts("Cannot open file");
exit();
}
while(1)
{
ch=fgetc(fp);
if(ch==EOF)
break;
if(ch>=97&&ch<=122)
ch-=32;
fputc(ch,ft);
}
fclose(fp);
fclose(ft);
}
Chap12[C]c Add contents of file on another
Write a program to add the contents of one file at the end of another.
#include "stdio.h"
void main()
{
FILE *fp,*ft;
char ch,ch2;
int f=0;
fp=fopen("c:\\ctxt\\12cc1.txt","a");
ft=fopen("c:\\ctxt\\12cc2.txt","r");
fprintf(fp,"\n");
while(1)
{
ch=fgetc(ft);
if(ch==EOF)
break;
fputc(ch,fp);
}
fclose(fp);
fclose(ft);
}
#include "stdio.h"
void main()
{
FILE *fp,*ft;
char ch,ch2;
int f=0;
fp=fopen("c:\\ctxt\\12cc1.txt","a");
ft=fopen("c:\\ctxt\\12cc2.txt","r");
fprintf(fp,"\n");
while(1)
{
ch=fgetc(ft);
if(ch==EOF)
break;
fputc(ch,fp);
}
fclose(fp);
fclose(ft);
}
Chap12[C]a Read file and display line numbers
Write a program to read a file and display contents with its line numbers.
#include "stdio.h"
void main()
{
FILE *fp;
char ch;
int c=1;
fp=fopen("c:\\ctxt\\12ca.txt","r");
if(fp==NULL)
{
puts("File cannot be opened");
exit();
}
clrscr();
while(1)
{
ch=fgetc(fp);
if(ch==EOF)
break;
if(ch=='\n')
c++;
printf("%c",ch);
}
printf("\nThe number of lines is %d",c);
}
#include "stdio.h"
void main()
{
FILE *fp;
char ch;
int c=1;
fp=fopen("c:\\ctxt\\12ca.txt","r");
if(fp==NULL)
{
puts("File cannot be opened");
exit();
}
clrscr();
while(1)
{
ch=fgetc(fp);
if(ch==EOF)
break;
if(ch=='\n')
c++;
printf("%c",ch);
}
printf("\nThe number of lines is %d",c);
}
Wednesday, November 4, 2009
Chap10D[h] Creating link list(inserting from front)
Linked list is a very common data structure often used to store similar data in memory. While the elements of an array occupy contiguous memory locations, those of a linked list are not constrained to be stored in adjacent location. The individual elements are stored “somewhere” in memory, rather like a family dispersed, but still bound together. The order of the elements is maintained by explicit links between them. Thus, a linked list is a collection of elements called nodes, each of which stores two item of information—an element of the list, and a link, i.e., a pointer or an address that indicates explicitly the location of the node containing the successor of this list element. Write a program to build a linked list by adding new nodes at the beginning, at the end or in the middle of the linked list. Also write a function display( ) which display all the nodes present in the linked list.
Reference: http://en.wikipedia.org/wiki/Malloc
http://cslibrary.stanford.edu/103/LinkedListBasics.pdf
#include alloc.h (include arrows)
struct list
{
int x;
struct list *next;
};
void main()
{
struct list *newlist();
struct list *a1=newlist();
struct list *current;
int i,k;
char j[1];
for(i=1,current=a1;current!=NULL;current=current->next,i++)
{
printf("\nThe No. %d in the list is %d",i,current->x);
}
while(1)
{
printf("\nEnter a value to key into the No. %d list",i);
scanf("%d",&k);
push(&a1,k);
printf("\nKey q to quit or any other key to continue keying");
scanf("%s",j);
if(j[0]==113)
break;
}
printf("\nThe new list is as below\n");
for(i=1,current=a1;current!=NULL;current=current->next,i++)
{
printf("\nThe No. %d in the list is %d",i,current->x);
}
}
struct list *newlist()
{
struct list *a1;
struct list *a2;
struct list *a3;
a1=malloc(sizeof(struct list));
a2=malloc(sizeof(struct list));
a3=malloc(sizeof(struct list));
a1->x=1;
a1->next=a2;
a2->x=2;
a2->next=a3;
a3->x=3;
a3->next=NULL;
return(a1);
}
push(struct list **head,int g)
{
struct list *nod=malloc(sizeof(struct list));
nod->x=g;
nod->next=*head;
*head=nod;
}
Reference: http://en.wikipedia.org/wiki/Malloc
http://cslibrary.stanford.edu/103/LinkedListBasics.pdf
#include alloc.h (include arrows)
struct list
{
int x;
struct list *next;
};
void main()
{
struct list *newlist();
struct list *a1=newlist();
struct list *current;
int i,k;
char j[1];
for(i=1,current=a1;current!=NULL;current=current->next,i++)
{
printf("\nThe No. %d in the list is %d",i,current->x);
}
while(1)
{
printf("\nEnter a value to key into the No. %d list",i);
scanf("%d",&k);
push(&a1,k);
printf("\nKey q to quit or any other key to continue keying");
scanf("%s",j);
if(j[0]==113)
break;
}
printf("\nThe new list is as below\n");
for(i=1,current=a1;current!=NULL;current=current->next,i++)
{
printf("\nThe No. %d in the list is %d",i,current->x);
}
}
struct list *newlist()
{
struct list *a1;
struct list *a2;
struct list *a3;
a1=malloc(sizeof(struct list));
a2=malloc(sizeof(struct list));
a3=malloc(sizeof(struct list));
a1->x=1;
a1->next=a2;
a2->x=2;
a2->next=a3;
a3->x=3;
a3->next=NULL;
return(a1);
}
push(struct list **head,int g)
{
struct list *nod=malloc(sizeof(struct list));
nod->x=g;
nod->next=*head;
*head=nod;
}
Manipulating Addresses
DIFFERENTIATING BETWEEN CHANGING ADDRESSES AND CHANGING VALUES

1)For a pointer to an integer
void main()
{
int a=3;
int *b;
b=&a;
func(b);
printf("The output is %d",b);
}
func(int *b)
{
b=b+1;
}
For the above the answer is 4.
2)For this
void main()
{
int a=1,b=2;
int *aa,*bb;
aa=&a;
bb=&b;
printf("\nAddress at aa is %u, Address at bb is %u",aa,bb);
/*Lets say address at a is 001 and b is 002
We create a function to try and swap these addresses so aa has address b and bb has address a*/
fswap(&aa,&bb);
printf("\nValue at aa is %d, Address at aa is %u,\nValue at bb is %d, Address at bb is %u",*aa,aa,*bb,bb);
}
fswap(int **addressaa,int **addressbb)
{
int **tempcc;
*tempcc=*addressaa;
*addressaa=*addressbb;
*addressbb=*tempcc;
}

1)For a pointer to an integer
void main()
{
int a=3;
int *b;
b=&a;
func(b);
printf("The output is %d",b);
}
func(int *b)
{
b=b+1;
}
For the above the answer is 4.
2)For this
void main()
{
int a=1,b=2;
int *aa,*bb;
aa=&a;
bb=&b;
printf("\nAddress at aa is %u, Address at bb is %u",aa,bb);
/*Lets say address at a is 001 and b is 002
We create a function to try and swap these addresses so aa has address b and bb has address a*/
fswap(&aa,&bb);
printf("\nValue at aa is %d, Address at aa is %u,\nValue at bb is %d, Address at bb is %u",*aa,aa,*bb,bb);
}
fswap(int **addressaa,int **addressbb)
{
int **tempcc;
*tempcc=*addressaa;
*addressaa=*addressbb;
*addressbb=*tempcc;
}
Sunday, November 1, 2009
Chap10[D]f Creating a library system
Write a menu driven program that depicts the working of a library. The menu options should be:
1. Add book information
2. Display book information
3. List all books of given author
4. List the title of specified book
5. List the count of books in the library
6. List the books in the order of accession number
7. Exit Create a structure called library to hold accession number, title of the book, author name, price of the book, and flag indicating whether book is issued or not.
struct library
{
int num;
char title[20];
char author[20];
float price;
int flag;
};
void main()
{
void accbook(struct library *l,int count);
void addbook(struct library *l,int count);
int count,a,ac;
char key[20],j[1];
struct library l[100]={
{1,"Rabbit","MrR",5.6,1},
{3,"Hamster","MrH",10.1,1},
{2,"Rice","MrR",7.0,0},
{5,"Hamlet","MrH",20.7,0},
{4,"Mouse","MrM",15.3,1}
};
clrscr();
while(1)
{
a=menu();
if(a==1)
{
clrscr();
count=countbook(&l);
addbook(&l,count);
}
if(a==2)
{
clrscr();
displaybook(&l);
}
if(a==3)
{
clrscr();
printf("\nKey in the author's name\n");
scanf("%s",key);
authorbook(&l,key);
}
if(a==4)
{
clrscr();
printf("\nKey in the accession number of the book\n");
scanf("%d",&ac);
specbook2(&l,ac);
}
if(a==5)
{
clrscr();
count=countbook(&l);
printf("\nNumber of books in the library is %d",count);
}
if(a==6)
{
clrscr();
count=countbook(&l);
accbook(&l,count);
displaybook(&l);
}
if(a==7)
exit();
printf("\n\n\nHit q and enter to quit and any other value to go back to menu\n");
scanf("%s",j);
if(j[0]==113)
break;
clrscr();
}
}
menu()
{
int acc;
printf("\n1. Add book information");
printf("\n2. Display book information");
printf("\n3. List all books of given author");
printf("\n4. List the title of specified book (Accession Number)");
printf("\n5. List the count of books in the library");
printf("\n6. Sort the books in order of accession number and display them");
printf("\n7. Exit");
printf("\n\nTo access the menu, key in the corresponding number\n");
while(1)
{
scanf("%d",&acc);
if(acc<1||acc>7)
{
printf("\nYou keyed an invalid value, key again\n");
}
else
break;
}
return(acc);
}
countbook(struct library *l)
{
int i,count=0;
for(i=0;l[i].num!=0;i++)
count++;
return(count);
}
void addbook(struct library *l,int count)
{
char j[1];
int s=0,c=0,i;
while(1)
{
if(count>99)
{
printf("\nLibrary has no more space");
break;
}
s=0;
printf("\nKey in accession number\n");
scanf("%d",&l[count].num);
if(l[count].num==0)
s=1;
for(i=0;i {
if(l[i].num==l[count].num)
{
printf("\nSimilar accession number,Key again\n");
s=1;
}
}
if(s==1)
continue;
printf("\nKey in title (no spacings)\n");
scanf("%s",l[count].title);
printf("\nKey in author (no spacings)\n");
scanf("%s",l[count].author);
printf("\nKey in price\n");
scanf("%f",&l[count].price);
while(1)
{
printf("\nKey 0 if the book is borrowed otherwise key 1 if available\n");
scanf("%d",&l[count].flag);
if(l[count].flag==0||l[count].flag==1)
break;
}
c++;
printf("\nKey any value to continue adding books or key q to quit to menu");
scanf("%s",j);
if(j[0]==113)
break;
clrscr();
count++;
}
if(c==1)
printf("\nYou keyed in 1 new book");
else if (c>1)
printf("\nYou keyed in %d new books",c);
}
displaybook(struct library *l)
{
int i;
for(i=0;l[i].num!=0;i++)
{
printf("\nNo: %d, Title: %s, Author: %s, Price: %f",l[i].num,l[i].title,l[i].author,l[i].price);
if(l[i].flag==1)
printf(", Availability: Yes");
else
printf(", Availability: No");
}
}
specbook(struct library *l)
{
printf("\nNo: %d, Title: %s, Author: %s, Price: %f",l->num,l->title,l->author,l->price);
if(l->flag==1)
printf(", Availability: Yes");
else
printf(", Availability: No");
}
specbook2(struct library *l,int j)
{
int i,f=0;
for(i=0;l[i].num!=0;i++)
{
if(l[i].num==j)
{
specbook(&l[i]);
f=1;
break;
}
}
if(f==0)
printf("\nNo such book with this accession number");
}
authorbook(struct library *l,char *key)
{
int i,j,c,len,f=0;
len=strlen(key);
for(i=0;l[i].num!=0;i++)
{
for(j=0,c=0;l[i].author[j]!=0;j++)
{
if(*(key+j)==l[i].author[j])
c++;
}
if(c==len)
{
specbook(&l[i]);
f=1;
}
}
if(f==0)
printf("\nThere are no books with this author's name");
}
void accbook(struct library *l,int count)
{
int i,j;
struct library a1,a2;
for(i=0;l[i].num!=0;i++)
{
for(j=i+1;j {
if(l[i].num>l[j].num)
{
a1=l[i];
a2=l[j];
l[i]=a2;
l[j]=a1;
}
}
}
}
linkfloat()
{
float a,*b;
b=&a;
a=*b;
}
1. Add book information
2. Display book information
3. List all books of given author
4. List the title of specified book
5. List the count of books in the library
6. List the books in the order of accession number
7. Exit Create a structure called library to hold accession number, title of the book, author name, price of the book, and flag indicating whether book is issued or not.
struct library
{
int num;
char title[20];
char author[20];
float price;
int flag;
};
void main()
{
void accbook(struct library *l,int count);
void addbook(struct library *l,int count);
int count,a,ac;
char key[20],j[1];
struct library l[100]={
{1,"Rabbit","MrR",5.6,1},
{3,"Hamster","MrH",10.1,1},
{2,"Rice","MrR",7.0,0},
{5,"Hamlet","MrH",20.7,0},
{4,"Mouse","MrM",15.3,1}
};
clrscr();
while(1)
{
a=menu();
if(a==1)
{
clrscr();
count=countbook(&l);
addbook(&l,count);
}
if(a==2)
{
clrscr();
displaybook(&l);
}
if(a==3)
{
clrscr();
printf("\nKey in the author's name\n");
scanf("%s",key);
authorbook(&l,key);
}
if(a==4)
{
clrscr();
printf("\nKey in the accession number of the book\n");
scanf("%d",&ac);
specbook2(&l,ac);
}
if(a==5)
{
clrscr();
count=countbook(&l);
printf("\nNumber of books in the library is %d",count);
}
if(a==6)
{
clrscr();
count=countbook(&l);
accbook(&l,count);
displaybook(&l);
}
if(a==7)
exit();
printf("\n\n\nHit q and enter to quit and any other value to go back to menu\n");
scanf("%s",j);
if(j[0]==113)
break;
clrscr();
}
}
menu()
{
int acc;
printf("\n1. Add book information");
printf("\n2. Display book information");
printf("\n3. List all books of given author");
printf("\n4. List the title of specified book (Accession Number)");
printf("\n5. List the count of books in the library");
printf("\n6. Sort the books in order of accession number and display them");
printf("\n7. Exit");
printf("\n\nTo access the menu, key in the corresponding number\n");
while(1)
{
scanf("%d",&acc);
if(acc<1||acc>7)
{
printf("\nYou keyed an invalid value, key again\n");
}
else
break;
}
return(acc);
}
countbook(struct library *l)
{
int i,count=0;
for(i=0;l[i].num!=0;i++)
count++;
return(count);
}
void addbook(struct library *l,int count)
{
char j[1];
int s=0,c=0,i;
while(1)
{
if(count>99)
{
printf("\nLibrary has no more space");
break;
}
s=0;
printf("\nKey in accession number\n");
scanf("%d",&l[count].num);
if(l[count].num==0)
s=1;
for(i=0;i
if(l[i].num==l[count].num)
{
printf("\nSimilar accession number,Key again\n");
s=1;
}
}
if(s==1)
continue;
printf("\nKey in title (no spacings)\n");
scanf("%s",l[count].title);
printf("\nKey in author (no spacings)\n");
scanf("%s",l[count].author);
printf("\nKey in price\n");
scanf("%f",&l[count].price);
while(1)
{
printf("\nKey 0 if the book is borrowed otherwise key 1 if available\n");
scanf("%d",&l[count].flag);
if(l[count].flag==0||l[count].flag==1)
break;
}
c++;
printf("\nKey any value to continue adding books or key q to quit to menu");
scanf("%s",j);
if(j[0]==113)
break;
clrscr();
count++;
}
if(c==1)
printf("\nYou keyed in 1 new book");
else if (c>1)
printf("\nYou keyed in %d new books",c);
}
displaybook(struct library *l)
{
int i;
for(i=0;l[i].num!=0;i++)
{
printf("\nNo: %d, Title: %s, Author: %s, Price: %f",l[i].num,l[i].title,l[i].author,l[i].price);
if(l[i].flag==1)
printf(", Availability: Yes");
else
printf(", Availability: No");
}
}
specbook(struct library *l)
{
printf("\nNo: %d, Title: %s, Author: %s, Price: %f",l->num,l->title,l->author,l->price);
if(l->flag==1)
printf(", Availability: Yes");
else
printf(", Availability: No");
}
specbook2(struct library *l,int j)
{
int i,f=0;
for(i=0;l[i].num!=0;i++)
{
if(l[i].num==j)
{
specbook(&l[i]);
f=1;
break;
}
}
if(f==0)
printf("\nNo such book with this accession number");
}
authorbook(struct library *l,char *key)
{
int i,j,c,len,f=0;
len=strlen(key);
for(i=0;l[i].num!=0;i++)
{
for(j=0,c=0;l[i].author[j]!=0;j++)
{
if(*(key+j)==l[i].author[j])
c++;
}
if(c==len)
{
specbook(&l[i]);
f=1;
}
}
if(f==0)
printf("\nThere are no books with this author's name");
}
void accbook(struct library *l,int count)
{
int i,j;
struct library a1,a2;
for(i=0;l[i].num!=0;i++)
{
for(j=i+1;j
if(l[i].num>l[j].num)
{
a1=l[i];
a2=l[j];
l[i]=a2;
l[j]=a1;
}
}
}
}
linkfloat()
{
float a,*b;
b=&a;
a=*b;
}
Subscribe to:
Posts (Atom)