c写一个函数,检查一个字符串是否是数字字符串

lyswwr 2014-05-12 11:50:55
怎样实现的,如isdigitstr(“1234”);
...全文
579 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
mujiok2003 2014-05-12
  • 打赏
  • 举报
回复
http://www.cplusplus.com/reference/cctype/isdigit/
FightForProgrammer 2014-05-12
  • 打赏
  • 举报
回复
感觉遍历这个字符串,只要全都是数字字符就行了吧?
rendason 2014-05-12
  • 打赏
  • 举报
回复
刚刚疏忽了
引用 11 楼 u013595256 的回复:
int isdigitstr(char* str)
{
    if(strspn(str, "0123456789")==strlen(str))
        return 1;
    return 0;
}
rendason 2014-05-12
  • 打赏
  • 举报
回复
int isdigitstr(char* str)
{
    if(strspn(str)==strlen(str))
        return 1;
    return 0;
}
没事人 2014-05-12
  • 打赏
  • 举报
回复
#inclde<iostream>..........
gz_qmc 2014-05-12
  • 打赏
  • 举报
回复
BOOL isdigitstr(char *x); { fif(NULL==x) return FALSE; char *p=x; while(*p) { if(*p<0x30&&*p>0x39&&*p!='.') return FALSE; p++; } return TRUE; }
漂浮一生 2014-05-12
  • 打赏
  • 举报
回复
有没有可能是小数呢??? 有没有可能是负数呢??? 这也是个问题。。。。
707wk 2014-05-12
  • 打赏
  • 举报
回复
比较简单的实现:
#include <stdio.h>

int isdigitstr(char* str)
{
	while(*str)
	{
		if(*str<'0'||*str>'9')return 0;
		str++;
	}
	return 1;
}

int main()
{
	printf("%d",isdigitstr("12asd3123"));
	return 0;
}
u013984264 2014-05-12
  • 打赏
  • 举报
回复
#include<stdio.h> int main() { void isdigitstr(char *); char str[30]; int k; gets(str); isdigitstr(str);//检查一个字符串是否是数字字符串 getch(); } void isdigitstr(char *p) { int length; length=strlen(p);//统计输入字符串的有效字符长度 while((*p)!=0) //逐个字符检查 { if((*p)>='0' && (*p)<='9')//如果出现数字,则字符串长度减1 length--; p++;//指向下一个字符 } if(length==0) printf("这是个数字字符串\n"); else printf("不是数字字符串\n"); } 亲测,你真懒
LouisScola 2014-05-12
  • 打赏
  • 举报
回复
#include <string.h>

int isDigitalStr(char *str)
{	
	int len = strlen(str);
	char *s = str;
	int i = 0;

	while( '0' <= *s && *s <= '9' && i < len){
		s++;
		i++;
	}

	if(i == len)
		return 1;
	else 
		return 0;		
}
arbboter 2014-05-12
  • 打赏
  • 举报
回复
楼主真懒。
赵4老师 2014-05-12
  • 打赏
  • 举报
回复
isdigit, iswdigit int isdigit( int c ); int iswdigit( wint_t c ); Each of these routines returns true if c is a particular representation of a decimal-digit character. Routine Required Header Compatibility isdigit <ctype.h> ANSI, Win 95, Win NT iswdigit <ctype.h> or <wchar.h> ANSI, Win 95, Win NT For additional compatibility information, see Compatibility in the Introduction. Libraries LIBC.LIB Single thread static library, retail version LIBCMT.LIB Multithread static library, retail version MSVCRT.LIB Import library for MSVCRT.DLL, retail version Return Value isdigit returns a non-zero value if c is a decimal digit (0 – 9). iswdigit returns a non-zero value if c is a wide character corresponding to a decimal-digit character. Each of these routines returns 0 if c does not satisfy the test condition. The result of the test condition for the isdigit function depends on the LC_CTYPE category setting of the current locale; see setlocale for more information. For iswdigit, the result of the test condition is independent of locale. Parameter c Integer to test Generic-Text Routine Mappings TCHAR.H Routine _UNICODE & _MBCS Not Defined _MBCS Defined _UNICODE Defined _istdigit isdigit _ismbcdigit iswdigit Character Classification Routines | Locale Routines | is, isw Function Overview

69,369

社区成员

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

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