String Processing Delete between two characters

Hello all,

I am currently working on string processing because I have not learned it. I have a string = <html><p>Hello < / P>. I am simply trying to remove all characters in between and including the <, > symbol. In the end it should just say Hello because it is not surrounded by <, >. Any tips in the right direction or tips would be helpful. I would like to stick with the text.erase and text.length functions if possible.

Thank you-
Christian

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
27
28
#include <iostream>
#include <string>

using namespace std;

int main() {


    string text = "<html><p>Hello < / P>";

    cout << text << endl;
    for (int a = 0; a < text.length(); a++)
    {
        if (text[a] == '<')
        {
            //use the erase code to get rid of all items
            text.erase(0, 0);
            a--;
        }
        else if (text[a] == '>')
        {

        }

    }
    cout << text << endl;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>
using namespace std;

string stripTags( const string &str )
{
   int state = 0;
   string result;
   for ( char c : str )
   {
      if      ( c     == '<' ) state++;
      else if ( c     == '>' ) state--;
      else if ( state == 0   ) result += c;
   }
   return result;
}


int main()
{
   string text = "<html><p>Hello < / P>";
   cout << text << '\n';
   cout << stripTags( text ); 
}
<html><p>Hello < / P>
Hello 
Last edited on
Topic archived. No new replies allowed.