测试字符串以查看是否存在数字并将该值分配给变量,同时跳过所有非数字值?
给定一个字符串“a 19 bcd 20”,我如何测试字符串上的特定位置是否有一个数字? (不仅仅是字符“1”,而是整个数字“19”和“20”)。
char s[80];
strcpy(s,"a 19 b c d 20");
int i=0;
int num=0;
int digit=0;
for (i =0;i<strlen(s);i++){
if ((s[i] <= '9') && (s[i] >= '0')){ //how do i test for the whole integer value not just a digit
//if number then convert to integer
digit = s[i]-48;
num = num*10+digit;
}
if (s[i] == ' '){
break; //is this correct here? do nothing
}
if (s[i] == 'a'){
//copy into a temp char
}
}
given a string say " a 19 b c d 20", how do I test to see if at that particular position on the string there is a number? (not just the character '1' but the whole number '19' and '20').
char s[80];
strcpy(s,"a 19 b c d 20");
int i=0;
int num=0;
int digit=0;
for (i =0;i<strlen(s);i++){
if ((s[i] <= '9') && (s[i] >= '0')){ //how do i test for the whole integer value not just a digit
//if number then convert to integer
digit = s[i]-48;
num = num*10+digit;
}
if (s[i] == ' '){
break; //is this correct here? do nothing
}
if (s[i] == 'a'){
//copy into a temp char
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这些是 C 解决方案:
您只是想从字符串中解析数字吗?然后您可以使用 strtol() 遍历字符串。
如果您有具体位置和号码需要检查,您仍然可以执行类似的操作。
例如:“19”在位置 10 吗?
您是否尝试在不使用任何库例程的情况下解析这些数字?
注意:我没有测试过这个......
These are C solutions:
Are you just trying to parse the numbers out of the string? Then you can just walk the string using
strtol()
.If you have a specific location and number to check for you can still do something similar.
For example: Is '19' at position 10?
Are you trying to parse out the numbers without using any library routines?
Note: I haven't tested this...
看起来你想解析字符串并从中提取所有数字;如果是这样,这里有一个更“C++”的方法:
这应该打印出以下内容:
流提取运算符自动提取空格分隔的标记,因此您不必执行任何混乱的字符级操作或手动操作整数转换。您需要为 stringstream 类添加
#include
。It seems like you want to parse the string and extract all the numbers from it; if so, here's a more "C++" way to do it:
This should print out the following:
The stream extraction operator pulls out space-delimited tokens automatically, so you're saved from having to do any messy character-level operations or manual integer conversion. You'll need to
#include <sstream>
for the stringstream class.您可以使用
atoi()
。You can use
atoi()
.在
if
之后,您需要转移到while
来收集后续数字,直到遇到非数字。但是,更重要的是,您是否明确定义了您的要求?数字之间允许有空格吗?如果有两个数字(例如 abc123def456gh)怎么办?
after your
if
you need to shift towhile
to collect subqsequent digits until you hit a non-digit.BUT, more inportantly, have you clearly defined your requirements? Will you allow whitespace between the digits? What if there are two numbers, like
abc123def456gh
?它不是很清楚你在寻找什么。假设你想从字符串中提取所有数字,然后从找到的数字中提取整个数字,你可以尝试以下操作:
假设字符串中的所有数字连接时从整数到整数不会溢出长数据类型。
Its not very clear what you are looking for.. Assuming you want to extract all the digits from a string and then from a whole number from the found digits you can try the following:
It is assumed that all the digits in your string when concatenated to from the whole number will not overflow the long data type.
没有办法测试整数。正如您所做的那样,编写词法分析器是一种方法。另一种方法是尝试使用 C 标准库的 strtoul 函数(或一些类似的函数,具体取决于字符串是否具有浮点数等)。
您的代码需要允许空格,并且您可以使用 C 库的
isdigit
来测试当前字符是否是数字:另一种方法是使用
boost::lexical_cast
>:There's no way to test for a whole number. Writing a lexer, as you've done is one way to go. Another would be to try and use the C standard library's
strtoul
function (or some similar function depending on whether the string has floating point numbers etc).Your code needs to allow for whitespaces and you can use the C library's
isdigit
to test if the current character is a digit or not:Another approach will be to use
boost::lexical_cast
: