将char *与整数和字符进行比较 [英] Comparing char* with integers and characters

查看:123
本文介绍了将char *与整数和字符进行比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将char *与str中包含的整数和字符进行比较,

整数可以是一位数还是更多?


void Access( char * str){b / b

char * pt = str;


while(pt!=''0''){

if(isalpha(* pt))

printf(找到一个字符\ n);

else if(* pt == ''='')

printf(等号找到了\ n);


//这就是问题

--1)否则如果(isdigit(* pt))

printf("只有一个数字在char * \ n"中加入); //它

如果整数是24则不会工作,它只是

//访问''2'

等等。


如何改进此代码以访问不仅限于1位数的整数,

比如说,

字符串由逗号,冒号和perios混合分隔。


char * str =" 24,5:300.123;


感谢您的建议

解决方案

" Nicholas" < NI ** @ yahoo.com>在消息中写道

新闻:Lp ******************* @ news-server.bigpond.net.au ...

....

| //这就是问题

| --1)否则if(isdigit(* pt))

| printf(只有一个数字在char * \ n中加入;); //它

|如果整数是24,它将无法工作,它只是

| //访问''2''

|等等。

|

|如何改进此代码以访问整数,而不仅限于1

位数,


嗯,这取决于你所说的访问的含义。 ...

你可以开始的一件事是:

else if(isdigit(* pt)){

char * numBegin = pt ;

do {++ pt; } while(isdigit(* pt));

/ * now [numBegin..pt [是包含数字的字符范围* /


现在到将数字字符串传递给大多数C函数,你将需要

零终止它。这可以通过以下任一方式完成:

- 将源字符串复制到本地缓冲区:

char buf [32] = {0};


//注意:这里需要检查溢出 - 这里有一个简单的方法

int len = pt-numBegin;

if(len> = sizeof(buf))len = sizeof(buf)-1;


memcpy(buf,numBegin,len);

printf("是%s",buf);


- 就地设置零终止(仅当输入

字符串可修改时才有效 - 将失败如果参数是一个字符串

literal):

char saved = * pt;

* pt =''\''';

printf(数字是%s,numBegin);

* pt =已保存;

hth - Ivan

-
http://www.post1.com/~ ivec <> Ivan Vecerina




" Ivan Vecerina" < 4 ** @ myrealbox.com>在消息中写道

news:3f ******** @ news.swissonline.ch ...

" Nicholas" < NI ** @ yahoo.com>在消息中写道
新闻:Lp ******************* @ news-server.bigpond.net.au ...
......
| //这是问题
| --1)否则if(isdigit(* pt))
| printf(只有一个数字在char * \ n中加入;); //它
|如果整数是24,它将无法工作,它只是
| //访问''2''
|等等
|
|如何改进此代码以访问整数,而不仅限于1 /

嗯,这取决于你所说的访问的含义。 ......
你可以开始的一件事是:
如果(isdigit(* pt)){
char * numBegin = pt;
do {++ pt; } while(isdigit(* pt));
/ * now [numBegin..pt [是包含数字的字符范围* /


感谢Ivan的回复。


" Access"表示如果char str [] =" 24,5:300.123" ;;在函数内部,我想b / b
想要将整数视为一个组,因为你在char * numBegin中提到了
,我想要处理''令牌'':24或300或123,

但不是2或4或5等等......上面的numBegin满足。


现在要将数字字符串传递给大多数C函数,您需要将其终止。这可以通过以下任一方式完成:
- 将源字符串复制到本地缓冲区:
char buf [32] = {0};


你是什么通过将数字串传递给大多数C函数来表示?


如果我有这个函数,请说:void Func1(char * str1)并调用函数

因为Func(numBegin)会给我错误吗?

是因为char * numBegin包含24。如果char * numBegin只是

" 5" ?


我可以使用strcpy来编号吗?


谢谢

//注意:需要检查在这里溢出 - 这里是一个简单的方法
int len = pt-numBegin;
if(len> = sizeof(buf))len = sizeof(buf)-1;
< brc> memcpy(buf,numBegin,len);
printf(数字是%s,buf);

- 就地设置零终止(仅适用于如果输入
字符串是可修改的 - 如果参数是一个字符串,那么将失败
literal):
char saved = * pt;
* pt =''\'0' ';
printf(数字是%s,numBegin);
* pt =已保存;

hth - Ivan
-
http://www.post1.com/~ivec <> Ivan Vecerina



2003年9月9日星期二04:11:23 GMT

" Nicholas" < NI ** @ yahoo.com>写道:

如何将char *与str中包含的整数和字符进行比较,
整数可以是一位数还是更多?

void Access(char * str){

char * pt = str;

而(pt!=''0''){


虽然地址不等于字符'0'的值?

我希望你想要(* pt!= 0)或(* pt!=''\ 0 )或(* pt),这些都意味着''而

字符pt指向的不是''。

if(isalpha(* pt))
printf(找到一个字符\ n);
如果(* pt ==''='')
printf(找到等号\\n) ;

//这是问题


C ++风格评论

- 1)否则if(isdigit(* pt) )
printf(只有一个数字在char * \ n中加入;); //如果整数是24,它就不会工作,只是
//访问''2'
等等。

如何改进此代码访问整数不仅限于1位数,
比如说,
字符串由逗号,冒号和perios混合分隔。

char *的示例str =" 24,5:300.123;




看看这个:


-

char * x(c,k,s)char * k,* s; {if(!k)return * s-36?x(0,0,s + 1):s; if(s)if(* s)c = 10 +(c?(x(

c,k,0),x(c,k + = * sc,s + 1),* k):( x(* s, k,s + 1),0));否则c = 10; printf(& x(~0,0,k)[c-〜 -

c +1[〜 ℃下-c],C);}主(){X(0," ^ [kXc6] dn_eaoh


How can I compare char* with integers and characters contained in the str,
where integers can be one digit or more?

void Access(char *str) {

char *pt = str;

while (pt != ''0'') {
if (isalpha(*pt))
printf("A character is found\n");
else if (*pt == ''='')
printf("Equal sign is found\n");

// This is the problem
--1) else if (isdigit(*pt))
printf("Only a single digit is accesed usin char*\n"); // It
wont work if the integer is 24, it is only
// accessing ''2''
etc. etc.

How can I improve this code to access integers not limited only to 1 digit,
say for an example,
the string is separated by mixed of commas, colons and perios.

example of char *str = "24,5:300.123;

Thank you for the advice

解决方案

"Nicholas" <ni**@yahoo.com> wrote in message
news:Lp*******************@news-server.bigpond.net.au...
....
| // This is the problem
| --1) else if (isdigit(*pt))
| printf("Only a single digit is accesed usin char*\n"); // It
| wont work if the integer is 24, it is only
| // accessing ''2''
| etc. etc.
|
| How can I improve this code to access integers not limited only to 1
digit,

Well, it depends what you mean by "access" ...
One thing you can start with is:
else if (isdigit(*pt)) {
char* numBegin = pt;
do { ++pt; } while( isdigit(*pt) );
/* now [numBegin..pt[ is the char range containing a number */

Now to pass the number-string to most C functions, you will need to
zero-terminate it. This can be done by either:
- copying the source string to a local buffer:
char buf[32] = {0};

//NB: need to check for overflow here -- here''s a simple way
int len = pt-numBegin;
if( len>=sizeof(buf) ) len = sizeof(buf)-1;

memcpy( buf, numBegin, len );
printf("The number is %s",buf);

- setting a zero-termination in-place (only works if the input
string is modifiable -- Will FAIL if the parameter is a string
literal):
char saved = *pt;
*pt = ''\0'';
printf("The number is %s",numBegin);
*pt = saved;
hth - Ivan
--
http://www.post1.com/~ivec <> Ivan Vecerina



"Ivan Vecerina" <iv**@myrealbox.com> wrote in message
news:3f********@news.swissonline.ch...

"Nicholas" <ni**@yahoo.com> wrote in message
news:Lp*******************@news-server.bigpond.net.au...
...
| // This is the problem
| --1) else if (isdigit(*pt))
| printf("Only a single digit is accesed usin char*\n"); // It
| wont work if the integer is 24, it is only
| // accessing ''2''
| etc. etc.
|
| How can I improve this code to access integers not limited only to 1
digit,

Well, it depends what you mean by "access" ...
One thing you can start with is:
else if (isdigit(*pt)) {
char* numBegin = pt;
do { ++pt; } while( isdigit(*pt) );
/* now [numBegin..pt[ is the char range containing a number */

Thanks Ivan for the reply.

"Access" means that if char str[] = "24,5:300.123"; inside the funtion, I
would like to treat the integers as a group as you mentioned
in the char* numBegin, I''d like to process the ''token'': 24 or 300 or 123,
but not 2 or 4 or 5 etc etc...which is satisfied by the numBegin above.

Now to pass the number-string to most C functions, you will need to
zero-terminate it. This can be done by either:
- copying the source string to a local buffer:
char buf[32] = {0};

What do you mean by "to pass the number-string to most C functions"?

If I have this function, say: void Func1(char *str1) and call the function
as Func(numBegin) will give me error ?
Is it because char* numBegin contains "24". What if char* numBegin is only
"5" ?

Can I just use strcpy to numBegin?

Thank you
//NB: need to check for overflow here -- here''s a simple way
int len = pt-numBegin;
if( len>=sizeof(buf) ) len = sizeof(buf)-1;

memcpy( buf, numBegin, len );
printf("The number is %s",buf);

- setting a zero-termination in-place (only works if the input
string is modifiable -- Will FAIL if the parameter is a string
literal):
char saved = *pt;
*pt = ''\0'';
printf("The number is %s",numBegin);
*pt = saved;
hth - Ivan
--
http://www.post1.com/~ivec <> Ivan Vecerina



On Tue, 09 Sep 2003 04:11:23 GMT
"Nicholas" <ni**@yahoo.com> wrote:

How can I compare char* with integers and characters contained in the str,
where integers can be one digit or more?

void Access(char *str) {

char *pt = str;

while (pt != ''0'') {
while an address doesn''t equal the value of the character ''0''?
I hope you want while (*pt != 0) or (*pt != ''\0) or (*pt), which all mean ''while
the character pt points to is not zero''.
if (isalpha(*pt))
printf("A character is found\n");
else if (*pt == ''='')
printf("Equal sign is found\n");

// This is the problem
C++ style comments
--1) else if (isdigit(*pt))
printf("Only a single digit is accesed usin char*\n"); // It
wont work if the integer is 24, it is only
// accessing ''2''
etc. etc.

How can I improve this code to access integers not limited only to 1 digit,
say for an example,
the string is separated by mixed of commas, colons and perios.

example of char *str = "24,5:300.123;



Look at this:

-
char*x(c,k,s)char*k,*s;{if(!k)return*s-36?x(0,0,s+1):s;if(s)if(*s)c=10+(c?(x(
c,k,0),x(c,k+=*s-c,s+1),*k):(x(*s,k,s+1),0));else c=10;printf(&x(~0,0,k)[c-~-
c+"1"[~c<-c]],c);}main(){x(0,"^[kXc6]dn_eaoh


这篇关于将char *与整数和字符进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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