Programming Tips - How can I delete non-numeric characters from a string?

Date: 2009mar28 Language: C/C++ Q. How can I delete non-numeric characters from a string? A. This is handy if user-entered strings that are supposed to be numbers. Use the function DeleteNonNumericCharacters() below:
// helper function inline char *Shuffle(char *dest, const char *src) { return (char *) memmove(dest, src, strlen(src) + sizeof(char)); } // helper function inline bool IsNumeric(const char c) { return isdigit(c) || c == '-' || c == '.'; } void DeleteNonNumericCharacters(char *s) { for (char *p = s + strlen(s) - 1; p >= s; p--) { if (!IsNumeric(*p)) { Shuffle(p, p+1); } } } void ExampleUse() { char buf[100]; lstrcpyn(buf, "EDT5EST", sizeof(buf)); DeleteNonNumericCharacters(buf); printf("buf=%s<\n", buf); // Prints out "5" // Now you can safely do atoi() on it: int hours = atoi(buf); }