日期:2014-05-16  浏览次数:20796 次

c语言中怎样从字母和数字组成的字符串中提取数字?
问题如题,比如a123b11c22,在遇到字母时就把它后面的数字提取出来,并且使后面的数字(如a后面的123)可以和其他int数据比较大小,多谢指点!!

------解决方案--------------------
用strtok,加atoi
------解决方案--------------------
int型数据跟字符串转换用itoa就可以了。
而比较应该不难吧,遍历数组,比较ASC码,是数字则输出到新数组里,然后对新数组做atoi的操作就可以和int数据比较了。
------解决方案--------------------
探讨
#include "stdio.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#define MAX 13
void main()
{
//printf("hello\n");
int i,fd,j,ff;
//FILE *fl="/root/文档/项目相关/test/file1";
fd=open("/root/文档/项目相关/test/file1",O_RDWR|O_NDELAY);



char *buff;


buff=malloc(sizeof(buff));
memset(buff,0,sizeof(buff));


------解决方案--------------------
探讨
#include "stdio.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#define MAX 13
void main()
{
//printf("hello\n");
int i,fd,j,ff;
//FILE *fl="/root/文档/项目相关/test/file1";
fd=open("/root/文档/项目相关/test/file1",O_RDWR|O_NDELAY);



char *buff;


buff=malloc(sizeof(buff));
memset(buff,0,sizeof(buff));


------解决方案--------------------
[zhanghua@localhost TEST]$ cat str-num-test.c
#include <stdio.h>

int main()
{
char buf[] = "aaaBBB123addd";
int num = 0;
char *p;

p = buf;
while (*p != '\0')
{
if (*p > '0' && *p < '9')
break;
p++;
}

num = atoi(p);
printf("num = %d\n", num);

return 0;
}
[zhanghua@localhost TEST]$ ./str-num-test
num = 123

------解决方案--------------------
咋对我1楼的回复视而不见呢?strtok()函数不是非常的方便帮你一个个的找出数字子串来吗?
探讨
引用:
[zhanghua@localhost TEST]$ cat str-num-test.c
#include <stdio.h>

int main()
{
char buf[] = "aaaBBB123addd";
int num = 0;
char *p;

p = buf;
while (*p != '\0')
{
if (*p > '0' && *p < '9')
break;
p++;
}

num = atoi(p);
printf("num = %d\n", num);

return 0;
}
[zhanghua@localhost TEST]$ ./str-num-test
num = 123



谢谢,但…