开发者

C++ introduction: self study

开发者 https://www.devze.com 2023-01-27 23:23 出处:网络
I created the program to read from text file and remove special characters. I can\'t seem to code better the if statement. Please help. I searched online for the right code statements but they have al

I created the program to read from text file and remove special characters. I can't seem to code better the if statement. Please help. I searched online for the right code statements but they have all advanced code statements. The book I am learning from has the last(14th) chapter with strings and file open and closing code. I tried creating an array of special chars, but did not work. Please help me!

     int main()
     {


 string paragraph = "";
 string curChar = "";
 string fileName = "";
 int subscript=0;
 int numWords=0;


 ifstream inFile; //declaring the file variables in the implement
 ofstream outFile;


       cout << "Please enter the input file name(C:\owner\Desktop\para.txt): " << endl;

       cin >> fileName;


 inFile.open(fileName, ios::in); //opening the user entered file

 //if statement for not finding the file
 if(inFile.fai开发者_如何学JAVAl())
 {
  cout<<"error opening the file.";
 }
 else
 {
 getline(inFile,paragraph);
 cout<<paragraph<<endl<<endl;
 }


 numWords=paragraph.length();

 while (subscript < numWords)
 {

  curChar = paragraph.substr(subscript, 1);


      if(curChar==","||curChar=="."||curChar==")"
   ||curChar=="("||curChar==";"||curChar==":"||curChar=="-"
   ||curChar=="\""||curChar=="&"||curChar=="?"||
      curChar=="%"||curChar=="$"||curChar=="!"||curChar=="                ["||curChar=="]"||
   curChar=="{"||curChar=="}"||curChar=="_"||curChar=="  <"||curChar==">"
     ||curChar=="/"||curChar=="#"||curChar=="*"||curChar=="_"||curChar=="+"
   ||curChar=="=")


  {
   paragraph.erase(subscript, 1);
   numWords-=1;
  }
  else 
   subscript+=1;

 }

 cout<<paragraph<<endl;
 inFile.close();


You might want to look into the strchr function which searches a string for a given character:

include <string.h>
char *strchr (const char *s, int c);

The strchr function locates the first occurrence of c (converted to a char) in the string pointed to by s. The terminating null character is considered to be part of the string.

The strchr function returns a pointer to the located character, or a null pointer if the character does not occur in the string.

Something like:

if (strchr (",.();:-\"&?%$![]{}_<>/#*_+=", curChar) != NULL) ...

You'll have to declare curChar as a char rather than a string and use:

curChar = paragraph[subscript];

rather than:

curChar = paragraph.substr(subscript, 1);

but they're relatively minor changes and, since your stated goal was I want to change the if statement into [something] more meaningful and simple, I think you'll find that's a very good way to achieve it.


In <cctype> header we have functions like isalnum(c) which returns true iff c is an alpanumeric character, isdigit(c) etc... I think the condition you are looking for is

if(isgraph(c) && !isalnum(c))

But c must be a char, not an std::string (well, technically speaking c must be int, but the conversion is implicit:) hth

P.S. This isn't the best idea, but if you want to keep sticking with std::string for curChar, c will be this char c = curChar[0]


since you are learning c++, I will introduce you the c++ iterator way of erasing.

for (string::iterator it = paragraph.begin();
        it != paragraph.end();
        ++it)
    while (it != paragraph.end() && (*it == ',' || *it == '.' || ....... ))
        it = paragraph.erase(it);

First, try using iterator. This won't give you best performance, but its concept would help you work with other c++ structure.

if(curChar==","||curChar=="."||curChar==")"  ......

Second, single quote ' and double quote " differs. You use ' for char.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号