上个星期老师给我们一个题目让我们把字符串中的数字字符找出来然后在把他变成整型的
这个程序分成两个部分,一是在字符串中找出数字字符,二是将数字字符转换成整型数字。
下面附上我的代码,如有不妥请指正。
#include <stdio.h>
mygets(char str[])
{
int i;
i = 0;
while((str[i] = getchar()) != '\n')
{
i++;
}
str[i] = '\0';
}
int atoi(char *p,char *p1)
{
int temp = 0;
while(*p != '\0')
{
if('0' <= *p && *p <= '9')
{
*p1 = *p;
//printf("%c",*p1);
temp = temp * 10 + (*p1 - '0');
p1++;
}
p++;
}
return temp;
//printf("%s\n",*p1);
}
int main()
{
char str[30];
char b[30];
int m;
mygets(str);
printf("%s\n",str);
m = atoi(str,b);
printf("%d\n",m);
return 0;
}
下面附上我的运行代码图