in reply to Thinkin' Perl in C++

A C-String is an array of char's where the last element is a null character ( '\0' ) which evaluates to false in boolean context. Walk the array with a while loop and an index variable that you will increment++, looking for a '-'. When you find a '-', assign that element a '_'. When you reach a '\0', you're done.

size_t i = 0; while( cstring[i] ) { if( cstring[i] == '-' ) cstring[i] = '_'; i++; }

Or without an index, but with pointer arithmatic...

char * temp = cstring; while( *temp ) { if( *temp == '-' ) *temp = '_'; temp++; }

You did mention C strings. If you're using the string class, it's a little different.


Dave

Replies are listed 'Best First'.
Re^2: Thinkin' Perl in C++
by salva (Canon) on Mar 01, 2012 at 16:57 UTC
    I guess that when the OP talks about CString he is actually referring to the Windows C++ class CString, not to the C language strings (aka char *)
      If it's truly CString, then:
      CString sTest("This-has-dashes"); sTest.Replace('-','_');

      mr.nick ...

      Well, then he's on his own. ;) I don't know anything about the Windows CString class. :)


      Dave