没有发生事件 [英] No occurrence founded

查看:80
本文介绍了没有发生事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




这是一个计算

字符串中单词出现次数的函数:

int wordCounter(char * a,char * b)

{

int cnt = 0;

char * sptr = a;

/ *仅供测试

int c = strlen(a);

int d = strlen(b);

printf (a =%deb =%d \ n,c,d);结束测试* /

while(sptr!=''\'''&&(sptr = strstr(sptr,b))!= NULL)

{

cnt ++;

sptr + = strlen(b);

}

return(cnt);

}


如果我通过,例如:

表示test1 test2 test3 test4

表示b测试

函数返回cnt = 0.

我已经验证a的长度是char + 1和b的数量

char + 1的数量。

如何修改我的功能,以便正确计算单词

数?


谢谢

加埃塔诺

解决方案

nick048写于04/11 / 07 13:32,:





这是一个计算单词出现次数的函数

string:

int wordCounter(char * a,char * b)

{

int cnt = 0 ;

char * sptr = a;

/ *仅供测试

int c = strlen(a);

int d = strlen( b);

printf(a =%deb =%d \ n,c,d);结束测试* /

while(sptr!=''\'''&&(sptr = strstr(sptr,b))!= NULL)

{

cnt ++;

sptr + = strlen(b);

}

return(cnt);

}


如果我通过,例如:

表示test1 test2 test3 test4

表示b测试

函数返回cnt = 0.

我已经验证a的长度是char + 1和b的数量

char + 1的数量。

如何修改我的功能,以便正确计算单词

的数量?



我不确定有什么问题,但这里有三个观察结果:


- 你还记得#include< ; string.h>?


- 'while''测试的第一部分可能是错误的。


- 该函数将循环无限期,如果'b''是"。


-
Er ********* @ sun.com


nick048写道:





这是一个计算

字符串中单词出现次数的函数:

int wordCounter(char * a,char * b)

{

int cnt = 0;

char * sptr = a;

/ *仅用于测试

int c = strlen(a);

int d = strlen(b);

printf(a =%deb =%d \ n,c,d);结束测试* /

while(sptr!=''\'''&&(sptr = strstr(sptr,b))!= NULL)

{

cnt ++;

sptr + = strlen(b);

}

return(cnt);

}


如果我通过,例如:

表示test1 test2 test3 test4

为b" test"

函数返回cnt = 0.



我得到了4.

我会在错误的顺序中输入参数,

或其他错误指定它们。既然你没有/显示

代码/来电,我们就看不出你做错了什么。


为什么,天堂'原来,调用一个参数`a`和

其他`b`?为什么不是更令人回味的东西,比如

`lookFor`和`lookIn`?为什么叫柜台`cnt`

而不是`count`或`result` - 我可以看到你的`o`

和`u`键有效吗?为什么用'\ 0''(字符文字)比较`sptr`(另一个可疑的

名称,既没有简洁的优点也没有

可理解性)

而不是`NULL`或`0`或我的首选,

什么都没有,只需使用`sptr`?


(我也会计算`strlen(lookFor)`一次并给它一个

漂亮的名字。)


-

Prickly Hedgehog

美好时光曾经好多了。




" nick048" < ni ************* @ moonsoft.itwrote in message

news:11 ***************** *****@q75g2000hsh.googlegr oups.com ...





这是一个函数计算

字符串中出现的单词数:

int wordCounter(char * a,char * b)

{

int cnt = 0;

char * sptr = a;

/ *仅供测试

int c = strlen( a);

int d = strlen(b);

printf(" a =%deb =%d \ n",c,d);结束测试* /

while(sptr!=''\'''&&(sptr = strstr(sptr,b))!= NULL)



sptr是一个指针,你无法将它与char进行比较。

你想要(* sptr&&(sptr = strstr(sptr,b))!= NULL)


{

cnt ++;

sptr + = strlen(b);

}

返回(cnt);

}


如果我通过,例如:

代表test1 test2 test3 test4

代表btest

函数返回cnt = 0.

I已经验证了a的长度是char + 1的数量,而b 的数量是char + 1.

如何修改我的函数,以便计算正确的单词

数字?



-

Fred L. Kleinschmidt

波音助理技术研究员

航空稳定性和控制计算


Hi,

This is a function that count the number of occurrences of word in a
string:
int wordCounter(char *a, char *b)
{
int cnt = 0;
char *sptr = a;
/* ONLY FOR TEST
int c = strlen(a);
int d = strlen(b);
printf("a = %d e b = %d\n",c,d); END TEST*/
while ( sptr != ''\0'' && ( sptr = strstr( sptr, b ) ) != NULL )
{
cnt++;
sptr += strlen( b );
}
return(cnt);
}

If I pass, for example:
for a "test1 test2 test3 test4"
for b "test"
the function return cnt = 0.
I have verified that the lenght of a is the number of char+1 and for b
the number of char + 1.
How can modify my function, in order to count correctly the word
number ?

Thank You
Gaetano

解决方案

nick048 wrote On 04/11/07 13:32,:

Hi,

This is a function that count the number of occurrences of word in a
string:
int wordCounter(char *a, char *b)
{
int cnt = 0;
char *sptr = a;
/* ONLY FOR TEST
int c = strlen(a);
int d = strlen(b);
printf("a = %d e b = %d\n",c,d); END TEST*/
while ( sptr != ''\0'' && ( sptr = strstr( sptr, b ) ) != NULL )
{
cnt++;
sptr += strlen( b );
}
return(cnt);
}

If I pass, for example:
for a "test1 test2 test3 test4"
for b "test"
the function return cnt = 0.
I have verified that the lenght of a is the number of char+1 and for b
the number of char + 1.
How can modify my function, in order to count correctly the word
number ?

I am not sure what is wrong, but here are three observations:

- Did you remember to #include <string.h>?

- The first part of the `while'' test is probably wrong.

- The function will loop indefinitely if `b'' is "".

--
Er*********@sun.com


nick048 wrote:

Hi,

This is a function that count the number of occurrences of word in a
string:
int wordCounter(char *a, char *b)
{
int cnt = 0;
char *sptr = a;
/* ONLY FOR TEST
int c = strlen(a);
int d = strlen(b);
printf("a = %d e b = %d\n",c,d); END TEST*/
while ( sptr != ''\0'' && ( sptr = strstr( sptr, b ) ) != NULL )
{
cnt++;
sptr += strlen( b );
}
return(cnt);
}

If I pass, for example:
for a "test1 test2 test3 test4"
for b "test"
the function return cnt = 0.

I got 4.

I''ll lay odds you got the parameters in the wrong order,
or otherwise mis-specified them. Since you didn''t /show
code/ for the call, we can''t see what you did wrong.

Why, for heaven''s sake, call one parameter `a` and the
other `b`? Why not something more evocative, like
`lookFor` and `lookIn`? Why call the counter `cnt`
and not `count` or `result` -- I can see that your `o`
and `u` keys work? Why compare `sptr` (another dubious
name, with neither the virtue of brevity nor that of
comprehensibility) with ''\0'' (a character literal)
rather than `NULL` or `0` or my preferred choice,
nothing at all, just using `sptr`?

(I''d also compute `strlen(lookFor)` once and give it a
nice name.)

--
Prickly Hedgehog
The "good old days" used to be much better.



"nick048" <ni*************@moonsoft.itwrote in message
news:11**********************@q75g2000hsh.googlegr oups.com...

Hi,

This is a function that count the number of occurrences of word in a
string:
int wordCounter(char *a, char *b)
{
int cnt = 0;
char *sptr = a;
/* ONLY FOR TEST
int c = strlen(a);
int d = strlen(b);
printf("a = %d e b = %d\n",c,d); END TEST*/
while ( sptr != ''\0'' && ( sptr = strstr( sptr, b ) ) != NULL )

sptr is a pointer, you cannot compare it to a char.
You want (*sptr && ( sptr = strstr( sptr, b ) ) != NULL )

{
cnt++;
sptr += strlen( b );
}
return(cnt);
}

If I pass, for example:
for a "test1 test2 test3 test4"
for b "test"
the function return cnt = 0.
I have verified that the lenght of a is the number of char+1 and for b
the number of char + 1.
How can modify my function, in order to count correctly the word
number ?

--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Aero Stability and Controls Computing


这篇关于没有发生事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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