怎么取字符串中的部分字符?

来源:百度知道 编辑:UC知道 时间:2024/06/04 07:58:07
"type01.ini"
我想取01,存入int num;里
有函数能做吗?

循环、
查到0-9就存入一个char里面、
之后再用atoi( const char *str );这个函数

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX 10

int NumberCome(const char *str)
{
int count = strlen(str);
int tempCount = 0;
char tempStr[MAX] = "";
int strPos = 0;

for( ; count > tempCount ; tempCount++)
{
if(str[tempCount] > '0' && str[tempCount] < '9')
{
tempStr[strPos] = str[tempCount];
strPos++;
}
}
tempStr[strPos] = '\0';

return atoi(tempStr);
}

int main()
{
printf("%d", NumberCome("type01.ini"));
return 0;
}

GCC编译成功(只保证功能、不保证效率)

自己写个函数呗

可以自己编写一个函数

你能确保数字一定是在"type"和"."之间吗?