-1
//Add words from the file to the vector
while (inputFile >> word) {
    listWords.push_back(word);
    wordCount +=1;  //Count the words
}

for (int i = 0; i < listWords.size(); i++) {
    char *p;
    p = strchr(listWords.at(i), 'c');
    if ( p != NULL ) {
        cout << "null";
    }
}

The code I have here adds a rather large text file of words to what I have declared as a string vector listWords. I would like to use strchr and various other Cstring functions to get rid of individual words with certain letters and characters. I'm coming across an error when trying to do so saying "No matching function call to strchr." I have already included the libraries <vector> <string> <cstring> <fstream>. Pretty sure my error lies between the pointers.

char *strchr(const char *str, int ch)

Any tips to what I should do here to make strchr work?

Vishaal Shankar
  • 1,648
  • 14
  • 26
  • Please provide [mcve]. Important things: what is the type of `listWords`. – 273K Mar 07 '18 at 04:05
  • listWords is of type string and it is a vector. – Show_me_now Mar 07 '18 at 04:08
  • 1
    Does `listWords.at(i)` returns a `string` or a `char*`? If listWords is a `std::string vector`... you may need to try something like `listWords.at(i).c_str()` which will return a const char* – Ricardo González Mar 07 '18 at 04:08
  • 3
    If `listWords` is a vector of `std::string` you should use [std::string::find](http://en.cppreference.com/w/cpp/string/basic_string/find). – super Mar 07 '18 at 04:20

1 Answers1

1

Better

for (int i = 0; i < listWords.size(); i++) {
    const auto p = listWords.at(i).find('c');
    if ( p != std::string::npos ) {
        cout << "null";
    }
}

Worse

for (int i = 0; i < listWords.size(); i++) {
    const char *p = strchr(listWords.at(i).c_str(), 'c');
    if ( p != NULL ) {
        cout << "null";
    }
}
273K
  • 29,503
  • 10
  • 41
  • 64