求解啊~~字符串插入下划线~~~
比如 输入: hu wen32 jiha 43j输出: hu wen_32_ jiha _43_j
程序怎么写啊???
if ('0' <= s[i] && s[i] <= '9') { if ((s[i - 1] < '0' || s[i - 1] > '9') && s[i - 1] != '_') { strcpy(&s[i + 1], &s[i]); s[i] = '_'; } if ((s[i + 1] < '0' || s[i + 1] > '9') && s[i + 1] != '_') { strcpy(&s[i + 2], &s[i + 1]); s[i + 1] = '_'; } i++; }
#include <stdio.h> #include <string.h> char capitalize(char c) { return c >= 'a' && c <= 'z' ? c - 32 : c; } char is_numeric(char c) { return c >= '0' && c <= '9'; } int main() { char s[100] = {0}; int i = 0, j = 0; printf("Give me a string: "); gets(s); for (; s[j] == ' '; j++); for (; s[j]; i++, j++) { s[i] = s[j]; if (s[j] == ' ') { for (; s[j] == ' '; j++); j--; } } s[i] = 0; s[0] = capitalize(s[0]); for (i = 1; s[i]; i++) { if (s[i - 1] == ' ' && !is_numeric(s[i])) { s[i] = capitalize(s[i]); } if (is_numeric(s[i])) { if (!is_numeric(s[i - 1]) && s[i - 1] != '_') { for (j = strlen(s); j >= i; j--) { s[j + 1] = s[j]; } s[i++] = '_'; } if (!is_numeric(s[i + 1]) && s[i + 1] != '_') { for (j = strlen(s); j >= i; j--) { s[j + 1] = s[j]; } s[++i] = '_'; } } } puts(s); }