Question: 23
Write a function named “count_lines()” in C++ to count and print total lines not starting with ‘a’ or ‘A’.
Sol:
#include<iostream> #include<fstream> #include<conio.h> #include<stdio.h> #include<stdlib.h> #include<string.h> using namespace std; void count_lines() { ifstream pqr; char line[80]; int c=0; pqr.open("article.txt"); if (pqr.fail()) { cout<<"Unable to open the file"<<endl; exit(1); } while(!pqr.eof()) { pqr.getline(line,80); if(!(line[0]=='a' || line[0]=='A')) { cout<<line<<endl; c++; } } pqr.close(); cout<<"Total lines "<<c<<endl; } int main() { /* clrscr(); */ count_lines(); return(0); }
Question: 24
Write a function in C++ named “lines_reverse()” to read the contents of the file line by line and print reverse of each line
Sol:
example:
hello how are you
hope fine
output:
uoy era woh olleh
enif epoh
#include<iostream> #include<fstream> #include<conio.h> #include<stdio.h> #include<stdlib.h> #include<string.h> using namespace std; void count_lines() { ifstream pqr; char line[80]; pqr.open("article.txt"); if (pqr.fail()) { cout<<"Unable to open the file"<<endl; exit(1); } while(!pqr.eof()) { pqr.getline(line,80); cout<<strrev(line)<<endl; } pqr.close(); } int main() { /* clrscr(); */ count_lines(); return(0); }