program regarding special characters in c++
How do i write a program in C++, to read each line from a file, and then find out and display the line number and word which contains the special character??
|
Quote:
|
Create a class and make a counter that increments each time a line is read.
#include <iostream> #include "stdio.h" using namespace std; class Count{ int cnt; // your counter public: Count(){ cnt = 0; count_lines(); // a function in your class constructor } void count_lines(){ // the function string str; while(cin >> str){ cnt++; // count how many times it happens with the ++ cout << cnt << " " << str << endl; }} ~Count(){ cout << "Counted " << cnt << " lines"<< endl;} // finally destroy the class and tell how many lines you read. }; int main(int argc, char ** argv) { freopen("file.txt", "r", stdin); // open file for reading Count(); // count the lines in the file. return 0; } If you want to add a function that reads the string and finds the special characters then you can do it too. void special_characters(string str){ // manipulate str } // and put it in the count_lines function! |
Thanks for the replies...
this is what i've tried...de code is not fully correct.. #include<conio.h> #include <fstream.h> #include <iostream.h> #include <string.h> #include <stdio.h> int main() { char GetFileName[30],*line; int linecount=0,i=0,x; cout << "System >> Enter file to read: \n"; cout << "User >> "; cin >> GetFileName; ifstream filename; filename.open( GetFileName,ios::in); if(! filename ) { cout << "Unable to open file: " << GetFileName << endl; return 1; } else { while (filename.getline(line,100,'\n')) //Loop through lines { char str[] = filename.getline(line,100,'\n'); linecount++; char * pch; pch = strtok (str," "); while (pch != NULL) { do { str[i]<-getc(pch); x=int(str[i]); if( !(x>47 & x<58) || !(x>65 && x< 93)|| !(x>96 && x<123) || (x!=32 || x!=10 || x!=13) ) { cout<<"\nline number is : "<<linecount<<endl; cout<<"\nWord containing special character is : "<<pch<<endl; } }while( (str[i++]<-pch) !='\0'); //printf ("%s\n",pch); pch = strtok (NULL, " "); } } filename.close(); } return 0; } can any1 solve the problem??? many thnks |
@Gutsy_Iain_08 & ajcapri
Your code is impossable to read without [ code ] tags.. |
@ajcapri, your code seems like it can make it if you keep at it, but put it into a class because it will work better that way, just use your line
Quote:
|
All times are GMT -5. The time now is 10:53 PM. |