您的位置:首页 > 其它

从字符串中提取数字

2009-11-17 16:10 155 查看
下面程序将字符串中的连续数字提取出来,并存放到一维数组中。比如说一个字符串:"a284twx234 je3432",把“284“,“234”,“3432”这3个连续数字提取出来,分别存放到a[0],a[1].a[2]中。

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

int main()

//实现了把一个字符串中的第一次出现的有效数字
//提取出来比如:abge_+*123.456dfsdf
//程序运行的结果应该是:123.456

#include<iostream>
#include<cstring>
#include<cstdio>

#define MAXLEN 50 //定义字符数组的上届
using namespace std;

int main()
//将string类对象中的数字字符提取出来
int CFileIndexDialog::string2int(CString str)
int length;
int i;
TCHAR temp2[15];
lstrcpy(temp2,str); //copies a string to a buffer

TCHAR temp[4];
int count;
count=0;
length=strlen(temp2); //length保存了字符数组temp2的长度
for(i=0;i<length;i++)
if ((temp2[i]<='9')&&(temp2[i]>='0'))
temp[count]=temp2[i];
count++;
}
}
return (::atoi(temp));
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: