如何判断一个字符串里是否全是数字

guan323333 2009-10-23 03:34:08
RT,C里面应该有函数可以直接判断吗 ,用for循环的就不用说了,或者是正则表达式? 谁知道的说下啊

比如 char temp[20] = {"1245645"};

如何判断知道temp里全是数字
...全文
973 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
lzh3ng 2009-10-23
  • 打赏
  • 举报
回复
循环遍历一遍,与数字做比较。
zwzo_wen 2009-10-23
  • 打赏
  • 举报
回复
我也不会正则,使用循环可以解决问题。
astupidman 2009-10-23
  • 打赏
  • 举报
回复
用循环还是最简单的
小菜_默 2009-10-23
  • 打赏
  • 举报
回复
赞同12L和14L、、、
CatRoll 2009-10-23
  • 打赏
  • 举报
回复
对啊,上面的代码都可以啊。
tiangtangcao 2009-10-23
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 pcboyxhy 的回复:]
while(*s && *s>='0' && *s <='9') s++;

if (*s)
    //不是纯数字
else
    //纯数字
[/Quote]
这个好像是最简单的
liangyonglou 2009-10-23
  • 打赏
  • 举报
回复
int main()
{
char a[100];
scanf("%[123456789]",a);
printf("%s\n",a);

return 0;
}
用这种方法可以,只要是你输入的不是数字,就给你截取掉,你首先去长度,处理完毕后在去长度,如果长度一样,那就是全是数字了,不然就不是。
walkersfaint 2009-10-23
  • 打赏
  • 举报
回复
正则最简单,搜索下
pcboyxhy 2009-10-23
  • 打赏
  • 举报
回复
while(*s && *s>='0' && *s<='9') s++;

if (*s)
//不是纯数字
else
//纯数字
guan323333 2009-10-23
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 fandelei1982 的回复:]
不知道在c中如何用正则
C/C++ code
#include<stdio.h>int main(int argc,char*argv[])
{char temp[]="123232";int len= strlen(temp);char*buf= (char*)malloc(len*sizeof(char));int i= atoi(temp);if (strcmp(temp,itoa(i,buf,10)))
{
printf("not all numbers");
}else {
printf("all numbers");
}
free(buf);return0;
}
[/Quote]


5L这个不错,但为什么我在运行的时候会有点小问题,MS出在free(buf);这话上,图传不上来,就说是Debug ERRor
DAMAGE:AFTER Normal block 什么的
zbing0203 2009-10-23
  • 打赏
  • 举报
回复
C里用正则表达式得用其他库了 你搜下应该有
何牧晗 2009-10-23
  • 打赏
  • 举报
回复
C++里是:

string abc="12340573129asdf";
if(abc.find_first_not_of("1234567890")!=string::npos){cout<<"不全是数字";}
fx397993401 2009-10-23
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 fandelei1982 的回复:]
不知道在c中如何用正则
C/C++ code
#include<stdio.h>int main(int argc,char*argv[])
{char temp[]="123232";int len= strlen(temp);char*buf= (char*)malloc(len*sizeof(char));int i= atoi(temp);if (strcmp(temp,itoa(i,buf,10)))
{
printf("not all numbers");
}else {
printf("all numbers");
}
free(buf);return0;
}
[/Quote]

这个思路 好
ArmStronger 2009-10-23
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 fandelei1982 的回复:]
不知道在c中如何用正则
C/C++ code
#include<stdio.h>int main(int argc,char*argv[])
{char temp[]="123232";int len= strlen(temp);char*buf= (char*)malloc(len*sizeof(char));int i= atoi(temp);if (strcmp(temp,itoa(i,buf,10)))
{
printf("not all numbers");
}else {
printf("all numbers");
}
free(buf);return0;
}
[/Quote]
想法不错,就是当字符串长度超过int大小的时候不起作用了
butwang 2009-10-23
  • 打赏
  • 举报
回复

void main()
{
char temp[20] = {"1245645"};
char temp2[20] = {"124dd5"};

char *p = 0;
int s = strtol(temp, &p, 10);
if (*p == 0)
printf("%s 是数字\n", temp);
else
printf("%s 不是数字\n", temp2);

char *p2 = 0;
int s2 = strtol(temp2, &p2, 10);
if (*p2 == 0)
printf("%s 是数字\n", temp2);
else
printf("%s 不是数字\n", temp2);
}
friendly_ 2009-10-23
  • 打赏
  • 举报
回复
不知道在c中如何用正则

#include <stdio.h>

int main(int argc, char *argv[])
{
char temp[] = "123232";
int len = strlen(temp);
char *buf = (char*)malloc(len*sizeof(char));
int i = atoi(temp);
if (strcmp(temp,itoa(i,buf,10)))
{
printf("not all numbers");
} else {
printf("all numbers");
}
free(buf);
return 0;
}

guan323333 2009-10-23
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 thy38 的回复:]
因为你的字符串长度未知,恐怕只能用正则了:
用reg = "[^0-9]+"来匹配就可以了。

匹配成功说明不全是数字。
[/Quote]

正则这个东西我完全不懂啊,3L能不能直接给段代码看下
thy38 2009-10-23
  • 打赏
  • 举报
回复
因为你的字符串长度未知,恐怕只能用正则了:
用reg = "[^0-9]+"来匹配就可以了。

匹配成功说明不全是数字。
kouwenlong 2009-10-23
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 starcat 的回复:]
isdigit()
[/Quote]
它是用来判断字符的。
starcat 2009-10-23
  • 打赏
  • 举报
回复
isdigit()

69,364

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧