char c,fname[10]; ofstream filout; filout.open(”STRS”); contents to store in file (Enter * to stop) :\n”; cout«”Enter while((c=getcharO) !=‘*‘) filout«c; } Q. 4: Write a code snippet that will create an object called filout for writing, associate it with the filename STRS. The code should keep on writing strings to it as long as the user wants. Ans.: void main{ char c,fname[10]; ofstream filout; filout.open(”STRS”); contents to store in file (Enter * to stop) :\n”; cout«”Enter while((c=getcharO) !=‘*‘) filout«c; filout.closeO; } Q. 5 : Write a program that counts the number of characters up to the first $ in input and that leaves the $ in the input stream. Ans. : void main{ char s[80],ch; mt count=0; ifstreani file (“abc.txt”); while ( !file.eof) ( file.getline (s, 90); for(int i=0;i<80;i++) ( if(s[i]==’$’) break; count++; cout«count; fi1e.c1ose(); } Q. 6: Write a program that reads a text file and creates another file that is identical expect that every sequence of consecutive blank space is replaced by a single space. Ans.: void main() { char ch; int count=O; ifstream instream; ofstream outstream; instream.open(”A.txt”); outstream.open(”B.txt”); while(!instream.eof()) ch = (char)instream.get( ); if (isspace (ch)) count++; if (count >= 2) ch=’ ‘; Count = O;
char c,fname[10]; ofstream filout; filout.open(”STRS”); cout«”Enter contents to store in file (Enter * to stop) :\n”; while((c=getcharO) !=‘*‘) filout«c; } Q. 4: Write a code snippet that will create an object called filout for writing, associate it with the filename STRS. The code should keep on writing strings to it as long as the user wants. Ans.: void main{ char c,fname[10]; ofstream filout; filout.open(”STRS”); cout«”Enter contents to store in file (Enter * to stop) :\n”; while((c=getcharO) !=‘*‘) filout«c; filout.closeO; } Q. 5 : Write a program that counts the number of characters up to the first $ in input and that leaves the $ in the input stream. Ans. : void main{ char s[80],ch; mt count=0; ifstreani file (“abc.txt”); while ( !file.eof) ( file.getline (s, 90); for(int i=0;i<80;i++) ( if(s[i]==’$’) break; count++; cout«count; fi1e.c1ose(); } Q. 6: Write a program that reads a text file and creates another file that is identical expect that every sequence of consecutive blank space is replaced by a single space. Ans.: void main() { char ch; int count=O; ifstream instream; ofstream outstream; instream.open(”A.txt”); outstream.open(”B.txt”); while(!instream.eof()) ch = (char)instream.get( ); if (isspace (ch)) count++; if (count >= 2) ch=’ ‘; Count = O;