c语言 11-8
1、
#include <stdio.h> void del_num(char *s) //函数的形参为指针,也就是字符串数组的数组名,相当于指向数组第一个元素的指针。 { char *tmp = s; // 将指针tmp赋值为指针s,即指向传入的字符串数组的第一个元素的指针 while(*tmp) //循环条件为指针tmp指向的元素,如果tmp指针指向的元素不为null则执行循环体 { if(*tmp >= '0' && *tmp <= '9') tmp++; //当指针tmp指向的元素为数字时,指针tmp后移 else *s++ = *tmp++; //当指针tmp指向的元素不是数字时,将tmp指向的元素赋值给指针s指向的元素,同时指针s和指针tmp后移 } *s = '\0'; //指针tmp指向null是循环终止,如果*tmp最后一个是非数字字符或者数字时,并没有将最后的null赋值给*s,因此需要将null赋值给*s,使其形成字符串。此时指针s指向字符串的最后 //一个字符 } int main(void) { char str[128]; printf("str: "); scanf("%s", str); del_num(str); printf("result after deletion: %s\n", str); return 0; }
2、
#include <stdio.h> void del_num(char *s) { char *tmp = s; while(*tmp) { if(*tmp < '0' || *tmp > '9') *s++ = *tmp++; else tmp++; } *s = '\0'; } int main(void) { char str[128]; printf("str: "); scanf("%s", str); del_num(str); printf("result after deletion: %s\n", str); return 0; }
3、
#include <stdio.h> void del_num(char *s) { int j = 0; char *tmp = s; while(*tmp) { if(*tmp >= '0' && *tmp <= '9') tmp++; else { *s++ = *tmp++; j++; } } *s = '\0'; while(j--) s--; printf("result: after deletion: %s\n", s); } int main(void) { char str[128]; printf("str: "); scanf("%s", str); del_num(str); return 0; }
4、
#include <stdio.h> char *del(char *s1, char *s2) { char *tmp = s2; while(*s1) { if(*s1 < '0' || *s1 > '9') *s2++ = *s1++; else s1++; } *s2 = '\0'; return tmp; } int main(void) { char str1[128]; char str2[128]; printf("str1: "); scanf("%s", str1); printf("result: %s\n", del(str1, str2)); return 0; }
5、
#include <stdio.h> char *del(char *s) { char *t = s; char *tmp = s; while(*tmp) { if(*tmp < '0' || *tmp > '9') *s++ = *tmp++; else tmp++; } *s = '\0'; return t; } int main(void) { char str[128]; printf("str: "); scanf("%s", str); printf("result after deleting numbers: %s\n", del(str)); return 0; }
6、
#include <stdio.h> char *del(char *s1, char *s2) { char *t = s2; while(*s1) { if(*s1 < '0' || *s1 > '9') *s2++ = *s1; s1++; } return t; } int main(void) { char str1[128]; char str2[128]; printf("str1: "); scanf("%s", str1); printf("result after deleting numbers: %s\n", del(str1, str2)); return 0; }
7、
#include <stdio.h> char *del(char *s1) { char tmp[128]; char *t = tmp; char *z = tmp; while(*s1) { if(*s1 < '0' || *s1 > '9') *t++ = *s1; s1++; } *t = '\0'; return z; } int main(void) { char str[128]; printf("str: "); scanf("%s", str); printf("result after deleting numbers: %s\n", del(str)); return 0; }