One more string and char related question

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>

using namespace std;

int main()
{
    //cout << "Ivesk du string tipo kintamuosius" << endl;
     string first = "Daily Programmer";
     string second = "aeiou ";

     // cin >> pirmas >> antras;
       for(int i = 0; i < first.length(); i++)
       {
           for(int j = 0; j < second.length(); j++)
           {
               if(first[i] == second[j])
               {
                   first[i] = '\0';

               }
           }
       }
         cout << first << endl;
    return 0;
}


So I have this code rn and instead of printing a space for me I want to completely delete that particular character from the 1st word. How should I do It?
Have you tried erasing the letters instead of truncating the string?
How could I do that?
One way to do it:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <string>


int main ()
{
  std::string first = "Daily Programmer";
  std::string second = "aeiou ";
  std::string output;

  for (char ch: first)
  {
    if (second.find(ch) == std::string::npos)
    {
      output += ch;
    }
  }
  std::cout << "String without aeiou " << output << '\n';
  return 0;
}


Another way:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <string>


int main ()
{
  std::string first = "Daily Programmer";
  std::string second = "aeiou ";
  
  size_t pos = first.find_first_of ("aeiou");
  while (pos != std::string::npos)
  {
    first = first.erase (pos, 1);
    pos = first.find_first_of ("aeiou");
  }
  std::cout << "String without aeiou " << first << '\n';
  return 0;
}
Thank you, Tom, once again!!!

And I would like to know where can I get more info about string functions since most of the functions i see you guys using is new to me. And I believe that is the reason why I am not succeeding with these tasks.
You are very welcome.
A good tutorial for the beginning is
http://www.cprogramming.com/tutorial/string.html
Have you tried typing in std::string into your favorite search engine? There amongst the multitude of links you should be able to find documentation for this standard C++ class close to the top of the choices.

Topic archived. No new replies allowed.