LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   program regarding special characters in c++ (http://www.linuxquestions.org/questions/programming-9/program-regarding-special-characters-in-c-773124/)

ajcapri 12-03-2009 02:23 AM

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??

wje_lq 12-03-2009 05:36 AM

Quote:

Originally Posted by ajcapri (Post 3778030)
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??

  1. Define in your own mind what "special character" means.
  2. Learn a text editor such as vi. Some people might recommend emacs instead of vi.
  3. Get a good book on C++. I'd recommend one by Bjarne Stroustrup, who invented C++, but others on this forum might recommend other books or online tutorials.
  4. Design and code your program.
  5. Try it out. (Be sure to wear eye protection.)
  6. If it works, you're done. If it doesn't, post the code in this forum (sufficiently complete so we can compile it without having to modify it) and ask specific questions, describing in detail the code's behavior and what you'd like that behavior to be instead.
Good luck! We're cheering you on! :)

Gutsy_Iain_08 12-03-2009 05:49 AM

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!

ajcapri 12-03-2009 07:51 AM

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

smeezekitty 12-03-2009 03:11 PM

@Gutsy_Iain_08 & ajcapri
Your code is impossable to read without [ code ] tags..

Gutsy_Iain_08 12-04-2009 01:47 AM

@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:

if( !(x>47 & x<58) || !(x>65 && x< 93)|| !(x>96 && x<123) || (x!=32 || x!=10 || x!=13) )
inside a function called something that indicates it will find the special characters.


All times are GMT -5. The time now is 11:43 PM.