/*
#include<stdio.h>
#include<ctype.h>
//int fun(char x)
//{
// if(x-'0'>=0&&x-'0'<=9)
// return 1;
// return 0;
//}
int main()
{
char str[101];
fgets(str,sizeof(str),stdin);
char num[101];
int i=0,j=0;
for(i;str[i]!='\0';i++)
{
if(isdigit(str[i]))
{
num[j++]=str[i];
}
}
for(int c=0;c<j;c++)
{
printf("%c",num[c]);
}
return 0;
}*/
#include <stdio.h>
#include <string.h>
void fun(char s[])
{
char t[101];
int i,j=0;
for(i=0;s[i]!='\0';i++)
{
if(s[i]>='0'&&s[i]<='9')
{
t[j++]=s[i];
}
}
t[j]='\0';
strcpy(s,t);
}
int main()
{
char s[101];
gets(s);
fun(s);
printf("%s",s);
return 0;
}
在新增代码中 我们定义了一个返回值类型为void的自定义函数用于提取str中的字符类型数字,它的形式参数是指向传入数组首字符的指针,在这个自定义函数内,我们定义了一个char类型的temporary数组,从传入数组的首字符s[0]读起读到终止符'\0'为止,如果 s[i]是字符类型数字,将其存入t字符串中,并将下一位变成终止符'\0' 防止越界访问,这点很重要,最后把t字符串copy道s字符串,实际上就完成了s字符串中字符数字的提取。