c - How to switch characters in a string two characters at a time -
i working on first attempt @ learning c. want know if possible change characters of string two. meaning if have word: "hello", how set function change string to: "hlelo". have method called
void word_reverse(char* str) { strrev(str);//changes whole string in reverse } how make change 2 characters in string @ time? many in advance.
i'm not working out in front of compiler you'll have work make run, should started @ least.
char *resersi(char *string) { char *start = string; char *end = string; // move end pointer end (c-style strings end in '\0') while (*end != 0) end ++; // move end pointer 1 away end (we don't want swap '\0') end--; // slide start pointer , end pointer inwards until overlap or cross. while (start < end) { // swap values char temp = *end; *end = *start; *start = temp; // slide pointers start++; end--; } return string; } here basic outline of you'll need do.
Comments
Post a Comment