Delete a specific char from string

//Delete a specific char from string,,,,char is 'F' in string= "YOUSUF".the resultant string will be "YOUSU".
#include <iostream>
#include <string>
#include <exception>
using namespace std;

void DeleteCharFromString(string* str, char c) {
  if (!str || str->empty()) {
    throw exception();
  }
  int pos = 0;
  int new_pos = 0;
  while (pos < str->length()) {
    if ((*str)[pos] != c) (*str)[new_pos++] = (*str)[pos++];
    else pos++;
  }
  *str = (*str).substr(0, new_pos);
}

int main(int argc, char** argv) {
  if (argc <= 1) return 0;
  string str(argv[1]);
  char c = argv[2][0];
  DeleteCharFromString(&str, c);
  cout << str << endl;
}

 

posted @ 2013-09-22 18:01  dmthinker  阅读(154)  评论(0)    收藏  举报