Thread: find a char in a string

  1. #1
    Registered User
    Join Date
    Nov 2008

    find a char in a string


    I am trying to find and replace a character in a string.
    #include <iostream>
    #include <string>
    using namespace std;
    int main()
    	string s("This is a piece of text.  All of the \"a's\" will be replaced by \"XXX's\".");
    	const char target = 'a';
    	string replace_with("XXX");
    	size_t pos = 0;
    	while(pos != string::npos)
    		pos = s.find(target, pos, 1);
    		if(pos != string::npos)
    			s.replace(pos, 1, replace_with);
    	cout << s << endl;
    	return 0;
    I am getting an error on the line that defines target.
    error: invalid conversion from ‘char’ to ‘const char*’
    If I put "a", in find(), in place of target it works.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    The version of find that takes a single char as the first argument does not contain a third argument. (In fact, most of the versions of find don't take a third argument.)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 05-28-2009, 09:30 AM
  2. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  3. String Class Operations
    By Aidman in forum C++ Programming
    Replies: 10
    Last Post: 04-06-2003, 02:29 PM
  4. "Operator must be a member function..." (Error)
    By Magos in forum C++ Programming
    Replies: 16
    Last Post: 10-28-2002, 02:54 PM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM