#include<stdio.h> #include<stdlib.h> #include<math.h> #include<string.h> int main(){ /* 输入一个字符串str1, 把其中的连续非数字的字符子串换成一个‘*’, 存入字符数组str2 中, 所有数字字符也必须依次存入 str2 中。输出str2。 输入为一行字符串str1,其中可能包含空格。 字符串长度不超过80个字符。 <pre>$Ts!47&*s456 a23* +B9k</pre> *47*456*23*9* */ char str1[80]; char str2[80]; gets(str1); int i = 0 , j = 0; while( i < strlen(str1) - 1 ){ if( str1[i] >= '0' && str1[i] <= '9' ){ str2[j++] = str1[i]; } else if( (str1[i] < '0' || str1[i] > '9') && (str1[i+1] >= '0' && str1[i+1] <= '9') ){ str2[j++] = '*'; } i++; } if( str1[ strlen(str1) - 1 ] < '0' || str1[ strlen(str1) - 1 ] > '9') str2[j++] = '*'; else str2[j++] = str1[i]; str2[j] = '\0'; puts(str2); return 0; } |
Double click to view unformatted code.