Click here to Skip to main content
15,918,742 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Simple C to C++ Pin
Software200714-Sep-11 9:02
Software200714-Sep-11 9:02 
GeneralRe: Simple C to C++ Pin
Chris Losinger14-Sep-11 9:06
professionalChris Losinger14-Sep-11 9:06 
GeneralRe: Simple C to C++ Pin
Software200714-Sep-11 9:16
Software200714-Sep-11 9:16 
GeneralRe: Simple C to C++ Pin
Chris Losinger14-Sep-11 9:21
professionalChris Losinger14-Sep-11 9:21 
GeneralRe: Simple C to C++ Pin
Software200714-Sep-11 9:28
Software200714-Sep-11 9:28 
GeneralRe: Simple C to C++ Pin
Chris Losinger14-Sep-11 9:36
professionalChris Losinger14-Sep-11 9:36 
GeneralRe: Simple C to C++ [modified] Pin
Software200714-Sep-11 9:49
Software200714-Sep-11 9:49 
GeneralRe: Simple C to C++ Pin
David Crow14-Sep-11 10:09
David Crow14-Sep-11 10:09 
GeneralRe: Simple C to C++ Pin
Software200714-Sep-11 10:13
Software200714-Sep-11 10:13 
GeneralRe: Simple C to C++ Pin
Chris Losinger14-Sep-11 10:16
professionalChris Losinger14-Sep-11 10:16 
GeneralRe: Simple C to C++ [modified] Pin
Software200714-Sep-11 10:21
Software200714-Sep-11 10:21 
GeneralRe: Simple C to C++ Pin
Chris Losinger14-Sep-11 10:38
professionalChris Losinger14-Sep-11 10:38 
GeneralRe: Simple C to C++ Pin
Chuck O'Toole14-Sep-11 14:54
Chuck O'Toole14-Sep-11 14:54 
GeneralRe: Simple C to C++ Pin
Chuck O'Toole14-Sep-11 14:13
Chuck O'Toole14-Sep-11 14:13 
AnswerRe: Simple C to C++ Pin
Chuck O'Toole14-Sep-11 14:08
Chuck O'Toole14-Sep-11 14:08 
AnswerRe: Simple C to C++ Pin
Chuck O'Toole14-Sep-11 14:32
Chuck O'Toole14-Sep-11 14:32 
AnswerRe: Simple C to C++ Pin
enhzflep14-Sep-11 16:21
enhzflep14-Sep-11 16:21 
GeneralRe: Simple C to C++ Pin
Chuck O'Toole14-Sep-11 17:18
Chuck O'Toole14-Sep-11 17:18 
AnswerRe: Simple C to C++ Pin
Erudite_Eric14-Sep-11 20:41
Erudite_Eric14-Sep-11 20:41 
AnswerRe: Simple C to C++ Pin
Richard MacCutchan14-Sep-11 22:14
mveRichard MacCutchan14-Sep-11 22:14 
GeneralRe: Simple C to C++ Pin
Orjan Westin15-Sep-11 1:38
professionalOrjan Westin15-Sep-11 1:38 
GeneralRe: Simple C to C++ Pin
Richard MacCutchan15-Sep-11 1:43
mveRichard MacCutchan15-Sep-11 1:43 
AnswerRe: Simple C to C++ Pin
Orjan Westin15-Sep-11 1:32
professionalOrjan Westin15-Sep-11 1:32 
If you want to replace the character '<' in a string with, "&lt;", it could be done quite simply in C++ like this:
C++
void replace_html_delimiters(std::string& str)
{
  std::string::size_type pos = str.find("<");
  while (std::string::npos != pos)
  {
    str.replace(pos, 1, "&lt;");
    pos = str.find("<", pos + 4);
  } 
}

Or if you want to cover the closing '>' as well:
C++
void replace_html_delimiters(std::string& str)
{
  std::string::size_type pos = str.find_first_of("<>");
  while (std::string::npos != pos)
  {
    if ('<' == str[pos])
      str.replace(pos, 1, "&lt;");
    else
      str.replace(pos, 1, "&gt;");
    pos = str.find_first_of("<>", pos + 4);
  } 
}


By the way, I assume that you had &lt; in your code example, and that CodeProject converted it to < when you posted it? This can be avoided by escaping out the leading ampersand (& is also a reserved character in HTML, like < and >) like this: &amp;lt;.

Otherwise, your C code would simply replace the character '<' with the character '<', with lots of copying back and forth.
C++
void replace_html_delimiters(char *msg)
{
  for(i=0; ; i++)
  {
    if(msg[i]== NUL)
      break;                 // End condition, so not needed in for statement
    if(msg[i]=='<')          
    {
      msg[i] = NUL;          // Replace found '<' with 0 to mark end of string
      strcpy(z_buf,msg);     // Copy string (up to the new end) to buffer
      strcat(z_buf,"<");     // Add string "<" to end of buffer 
      strcat(z_buf,msg+i+1); // Add remaining string to end of buffer)
      strcpy(msg,z_buf);     // Copy back to string. 
    }
  }
}

And that could be rewritten very effectively like this:
C++
void replace_html_delimiters(char *msg)
{
  // No need to do anything
}

Smile | :)
AnswerRe: Simple C to C++ Pin
Stefan_Lang15-Sep-11 1:43
Stefan_Lang15-Sep-11 1:43 
GeneralRe: Simple C to C++ Pin
Richard MacCutchan15-Sep-11 1:47
mveRichard MacCutchan15-Sep-11 1:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.