:: [intv] Replace characters in string. ::
HOME


[Date Prev][Date Next][Date Index]

[intv] Replace characters in string.


//Replace some characters in a string
//Save as: main.cpp and compile as: g++ -o main main.cpp

#include <iostream>
#include <string>
#include <map>
using namespace std;

typedef map< char, int, less<char> > MAP;


int main(void){ string s1 = "shashank is a good boy"; string s2 = "aeiou";

  MAP hash;

  for (int i = 0; i < s2.length(); i++){
    hash.insert(MAP::value_type(s2[i], 1));

  }

  MAP::iterator itr;

  for (int i = 0; i < s1.length(); i++){
    itr = hash.find(s1[i]);
    if (itr != hash.end()){
      //Found in Hash
      s1[i] = ' '; //Note the Single Quotes. If you use "" then u will get an error.
    }

  }

  cout << s1 << endl;


}


Comments and corrections are appreciated and can be sent to papers@mia.ece.uic.edu. Click here for ©opyright information.