在字符串末尾获取数字 [英] get number at end of string

查看:74
本文介绍了在字符串末尾获取数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个函数,我必须在字符串的末尾得到一个数字。我发布了这封信,以防它对某人有帮助。评论是

欢迎。


int getnum(char * str)

{

char buffer [BUFSIZE];

char * buf = buffer;

int i;


for(i = 0; i< BUFSIZE-1; i ++){

如果(str [i] ==''\''')中断;

if(!isdigit(str [i] ))继续;

*(buf ++)= str [i];

}

* buf =''\''';


if(buffer [0]!=''\'''){

返回atoi(缓冲区);

}否则{

返回-1;

}

}

Here is a function I have to get a number at the end of a string. I''m
posting this in case it proves helpful to someone. Comments are
welcome.

int getnum(char *str)
{
char buffer[BUFSIZE];
char *buf = buffer;
int i;

for (i = 0; i < BUFSIZE-1; i++) {
if (str[i] == ''\0'') break;
if (!isdigit(str[i])) continue;
*(buf++) = str[i];
}
*buf = ''\0'';

if (buffer[0] != ''\0'') {
return atoi(buffer);
} else {
return -1;
}
}

推荐答案

jake1138写道:
jake1138 wrote:
这是一个函数我必须在字符串的末尾得到一个数字。我发布这个以防万一它对某人有帮助。评论是欢迎的。

int getnum(char * str)
{char buffer [BUFSIZE];
char * buf = buffer;
int i;

for(i = 0; i< BUFSIZE-1; i ++){
if(str [i] ==''\''')break ;
if(!isdigit(str [i]))继续;
*(buf ++)= str [i];
}
* buf =''\'0' ';

if(buffer [0]!=''\'''){
返回atoi(缓冲区);
}否则{
返回 - 1;
}
}
Here is a function I have to get a number at the end of a string. I''m
posting this in case it proves helpful to someone. Comments are
welcome.

int getnum(char *str)
{
char buffer[BUFSIZE];
char *buf = buffer;
int i;

for (i = 0; i < BUFSIZE-1; i++) {
if (str[i] == ''\0'') break;
if (!isdigit(str[i])) continue;
*(buf++) = str[i];
}
*buf = ''\0'';

if (buffer[0] != ''\0'') {
return atoi(buffer);
} else {
return -1;
}
}




首先,我不知道它与最后得到一个数字有什么关系

字符串。此功能并非专门针对字符串结尾字母b
以任何方式。


其次,这个函数最多检查输入序列的'BUFSIZE''第一个字符

,这不仅仅是奇怪的。什么输入字符串

(它应该是一个字符串,对吧?)比BUFSIZE更长?


-

祝你好运,

Andrey Tarasevich



Firstly, I don''t see what it has to do with "getting a number at the end
of a string". This function is not targeted specifically at the "end of
a string" in any way.

Secondly, this function inspects at most ''BUFSIZE'' first character of
the input sequence, which is more than strange. What it the input string
(it is supposed to be a string, right?) is longer than ''BUFSIZE''?

--
Best regards,
Andrey Tarasevich


jake1138写道:
jake1138 wrote:
这是一个函数我必须在字符串的末尾得到一个数字。我发布这个以防万一它对某人有帮助。评论是欢迎的。

int getnum(char * str)
{char buffer [BUFSIZE];
char * buf = buffer;
int i;

for(i = 0; i< BUFSIZE-1; i ++){
if(str [i] ==''\''')break ;
if(!isdigit(str [i]))继续;
*(buf ++)= str [i];
}
* buf =''\'0' ';

if(buffer [0]!=''\'''){
返回atoi(缓冲区);
}否则{
返回 - 1;
}
}
Here is a function I have to get a number at the end of a string. I''m
posting this in case it proves helpful to someone. Comments are
welcome.

int getnum(char *str)
{
char buffer[BUFSIZE];
char *buf = buffer;
int i;

for (i = 0; i < BUFSIZE-1; i++) {
if (str[i] == ''\0'') break;
if (!isdigit(str[i])) continue;
*(buf++) = str[i];
}
*buf = ''\0'';

if (buffer[0] != ''\0'') {
return atoi(buffer);
} else {
return -1;
}
}




评论(你要求''他们):

- 未记载。这确实需要记录它的语义,特别是

输入值的有效类型和解析失败时的行为。

- 它没有修改''str'',使用const。

- 我将''buf ++''从循环体中放入标题中,就像

检查''if(是) str [i] ==''\''''''。这是我个人的偏好。

- 由于''缓冲''的大小而容易失败。你检查一下你没有b $ b溢出它,但如果数字不合适,你就会返回错误的结果。

- 太复杂了。您(尝试)将''str''末尾的数字复制到

临时缓冲区,然后将该缓冲区提供给atoi()。相反,搜索该数字的起始点并直接将该指针提供给atoi()。

- 您的复制有问题,它会对所有数字进行汇总,然后解析结果

作为数字。


您需要根据您希望函数的工作方式来执行测试工具

(即根据无证语义):


断言(getnum(NULL)== - 1);

断言(getnum("")== - 1) ;

断言(getnum(" aeu123htn456)== 456);

断言(getnum(" x:-1")== - 1);

....


Uli



Comments (you asked for ''em):
- Undocumented. This really needs its semantics documented, in particular
the valid types of inputvalues and the behaviour on parsing failure.
- It doesn''t modify ''str'', use const.
- I''d put the ''buf++'' from the loop''s body into the header, just as the
check ''if (str[i] == ''\0'')''. This is my personal preference though.
- Prone to failure due to size of ''buffer''. You check that you don''t
overflow it, but if the number doesn''t fit, you return faulty results.
- Too complicated. You (try to) copy the number at the end of ''str'' to a
temporary buffer and then feed that buffer to atoi(). Instead, search the
beginning of that number and feed that pointer to atoi() directly.
- Your copying is faulty, it concats all digits and then parses the result
as number.

You need to do testcases according to how you want the function to work
(i.e. according to the yet undocumented semantics):

assert( getnum(NULL)==-1);
assert( getnum("")==-1);
assert( getnum("aeu123htn456)==456);
assert( getnum("x:-1")==-1);
....

Uli


jake1138< co **** **@gmail.com>写道:
jake1138 <co******@gmail.com> wrote:
这是一个函数我必须在字符串的末尾得到一个数字。我发布这个以防万一它对某人有帮助。欢迎评论。


有点挑剔:

int getnum(char * str)


正弦你不要修改'str''指向你可能会做什么


int getnum(const char * str)

{
char buffer [ BUFSIZE];
char * buf = buffer;
int i;


有些人认为尽可能检查

a函数的论据是个好习惯,你可以测试''str''是一个NULL

指针并在这种情况下报告错误。

for(i = 0; i< BUFSIZE-1; i ++){
if(/ str [i] ==''\''')break;
if(!isdigit(str [i]))继续;


负数有什么问题?你的函数会为它们返回一个正数
的数字......而且,与你给出的描述相反,你的

函数将返回它在字符串中找到的第一个数字,其中

不一定是字符串末尾的那个,想想例如

" blabla12blabla42blabla"。

*(buf ++)= STR [1];


您可以在buf ++周围留下括号。但是你为什么要b $ b认为你需要复制字符串呢?您可以轻松地使用''str''

本身作为指向字符串中当前位置的指针,然后

将其传递给atoi()或其他任何内容打算使用。

}
* buf =''\''';
if(buffer [0]!=''\'''){
return atoi(buffer);


虽然你可以放心地假设缓冲区中的内容以数字开头,

你仍然不知道是否数字不是太大而无法存储

整数 - 而atoi()让你没有机会解决这个问题。对于

,因为使用strtol()通常更为谨慎。

}否则{
返回-1;


如果您修改处理负数的功能,那么你需要使用不同的方式报告错误......

}
}
Here is a function I have to get a number at the end of a string. I''m
posting this in case it proves helpful to someone. Comments are
welcome.
A bit of nitpicking:
int getnum(char *str)
Sine you don''t modify what ''str'' is pointing to you might make that

int getnum( const char *str )
{
char buffer[BUFSIZE];
char *buf = buffer;
int i;
Some people think that it is a good habit to check the arguments of
a function as far as possible, and you could test is ''str'' is a NULL
pointer and report an error in this case.
for (i = 0; i < BUFSIZE-1; i++) {
if (str[i] == ''\0'') break;
if (!isdigit(str[i])) continue;
What''s wrong with negative numbers? Your function will return a positive
number for them... And, contrary to the description you gave, your
function will return the first number it finds in the string, which
is not necessarily the one at the end of the string, think of e.g.
"blabla12blabla42blabla".
*(buf++) = str[i];
You can leave off the parentheses around the ''buf++''. But why do you
think you need to copy the string at all? You can easily use ''str''
itself as a pointer to the current position in the string and later
pass that to atoi() or whatever you''re going to use.
}
*buf = ''\0''; if (buffer[0] != ''\0'') {
return atoi(buffer);
While you can safely assume that what''s in ''buffer'' starts with a digit,
you still don''t know if the number there isn''t too large to be stored
in an integer - and atoi() gives you no chance to figure that out. For
that reason it is usually more prudent to use strtol().
} else {
return -1;
If you modify the function to deal with negative numbers also you
will have to use a different way to report errors...
}
}




这是你的函数版本,负责处理一些

问题提到(但仍然没有正确处理负值

!):


#include< stdio.h>

#include< ctype.h>

#include< stdlib.h>

#include< errno.h>

#include < limits.h>


int getnum(const char * s)

{

long val;


if(s == NULL)

返回-1;


while(* s&&!isdigit( * s))

s ++;

if(!* s)

返回-1;


val = strtol(s,NULL,10);

if(errno == ERANGE || val> INT_MAX)

返回-1;

返回val;

}

问候, Jens

-

\ Jens Thoms Toerring ___ Je *********** @ physik.fu-berlin.de

\ __________________________ http://www.toerring.de


这篇关于在字符串末尾获取数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆