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 | |
by mr.nick (Chaplain) on Mar 01, 2012 at 20:07 UTC | |
by davido (Cardinal) on Mar 01, 2012 at 16:59 UTC |