473,385 Members | 1,347 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

No occurrence founded

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

Apr 11 '07 #1
14 1248
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
Apr 11 '07 #2
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.

Apr 11 '07 #3

"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
Apr 11 '07 #4
Fred Kleinschmidt wrote:
"nick048" <ni*************@moonsoft.itwrote in message
[snip]
> while ( sptr != '\0' && ( sptr = strstr( sptr, b ) ) != NULL )

sptr is a pointer, you cannot compare it to a char.
'\0' is an integer, not a char. '\0' == 0 == NULL (in many/most cases).
So you're absolutely right, the OP wants to compare *sptr to '\0', but
comparing sptr to '\0' is legal on most platforms.
Bjørn
[snip]
--
Looking for an embeddable web server?
http://www.metasystems.no/products/h...der/index.html
Apr 11 '07 #5
"B. Augestad" <bo*@metasystems.nowrites:
Fred Kleinschmidt wrote:
>"nick048" <ni*************@moonsoft.itwrote in message
[snip]
>> while ( sptr != '\0' && ( sptr = strstr( sptr, b ) ) != NULL )
sptr is a pointer, you cannot compare it to a char.

'\0' is an integer, not a char. '\0' == 0 == NULL (in many/most
cases). So you're absolutely right, the OP wants to compare *sptr to
'\0', but comparing sptr to '\0' is legal on most platforms.
'\0' is an integer constant expression with value 0, which means it is
by definition a null pointer constant. Assuming sptr is a pointer,
comparing it to '\0' is legal (but horribly bad style) on *all*
platforms.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Apr 11 '07 #6
B. Augestad wrote, On 11/04/07 20:56:
Fred Kleinschmidt wrote:
>"nick048" <ni*************@moonsoft.itwrote in message
[snip]
>> while ( sptr != '\0' && ( sptr = strstr( sptr, b ) ) != NULL )

sptr is a pointer, you cannot compare it to a char.

'\0' is an integer, not a char. '\0' == 0 == NULL (in many/most cases).
NULL could be ((void*)0) which is a rather different type. In fact, on
some common implementations it is.
So you're absolutely right, the OP wants to compare *sptr to '\0', but
comparing sptr to '\0' is legal on most platforms.
It is legal on all implementations since '\0' is an integer constant
with a value of 0 and all such constants are null pointer constants.
Note that other integer values are not compatible with pointers.
--
Flash Gordon
Apr 11 '07 #7
On Apr 12, 7:56 am, "B. Augestad" <b...@metasystems.nowrote:
Fred Kleinschmidt wrote:
"nick048" <nicosia.gaet...@moonsoft.itwrote in message
while ( sptr != '\0' && ( sptr = strstr( sptr, b ) ) != NULL )
sptr is a pointer, you cannot compare it to a char.

'\0' is an integer, not a char. '\0' == 0 == NULL (in many/most cases).
So you're absolutely right, the OP wants to compare *sptr to '\0', but
comparing sptr to '\0' is legal on most platforms.
Are you sure he wants to compare *sptr to '\0' ? It would
be a redundant test, since the strstr() function would
return NULL in that case anyway.

As written, the code will prevent calling strstr with a null
pointer (in case the code prior to this loop is later
modified, for example) which would cause undefined behaviour.

Apr 11 '07 #8
Keith Thompson wrote:
"B. Augestad" <bo*@metasystems.nowrites:
>>Fred Kleinschmidt wrote:
>>>"nick048" <ni*************@moonsoft.itwrote in message

[snip]

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

sptr is a pointer, you cannot compare it to a char.

'\0' is an integer, not a char. '\0' == 0 == NULL (in many/most
cases). So you're absolutely right, the OP wants to compare *sptr to
'\0', but comparing sptr to '\0' is legal on most platforms.


'\0' is an integer constant expression with value 0, which means it is
by definition a null pointer constant. Assuming sptr is a pointer,
comparing it to '\0' is legal (but horribly bad style) on *all*
platforms.
A dash of c.l.c paranoia got the better of me, as I wanted to stress
that NULL not necessarily is equal to 0. I guess I overdid it.

Thanks for clarifying.

Bjørn
--
Looking for an embeddable web server?
http://www.metasystems.no/products/h...der/index.html
Apr 12 '07 #9
Eric Sosman wrote:
nick048 wrote On 04/11/07 13:32,:
>Hi,

This is a function that count the number of occurrences of word in a
string:
(fx:snip)
I am not sure what is wrong, but here are three observations:
(fx:snip)
- The function will loop indefinitely if `b' is "".
That's plausibly correct behaviour: the empty string occurs infinitely
often in the test string, C can't represent infinite values, the
answer is the "undefined" of denotational semantics, ie bottom,
representing a non-terminating computation ...
--
Chris "not entirely serious" Dollin
The second Jena user conference! http://hpl.hp.com/conferences/juc2007/
"A facility for quotation covers the absence of original thought."/Gaudy Night/

Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN

Apr 12 '07 #10
"nick048" <ni*************@moonsoft.itha scritto nel messaggio
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 )
{
cnt++;
sptr += strlen( b );
}
return(cnt);
}
To me, this function seems valid, even if it has horrible style. I would
write:
int word_counter(char *str, char *word)
{
int cnt = 0;
size_t length;
if (word == NULL || *word == '\0')
return -1;
length = strlen(word);
if (str != NULL) {
while ((str = strstr(str, word)) != NULL) {
cnt++;
word += length;
}
}
return cnt;
}

which is perfectly equivalent (except for style, and for checking that word
isn't a null pointer or an empty string.) I think the error is in the
main...
Apr 13 '07 #11

"Army1987" <pl********@for.itha scritto nel messaggio
news:ev**********@tdi.cu.mi.it...
"nick048" <ni*************@moonsoft.itha scritto nel messaggio
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 )
{
cnt++;
sptr += strlen( b );
}
return(cnt);
}

To me, this function seems valid [snip] I think the error is in the
main...
I wrote:
#include <string.h>
#include <stdio.h>
int wordCounter(char *a, char *b)
{
/* copied and pasted from yours */
}
int main(void)
{
printf("%d\n", wordCounter("test1 test2 test3 test4", "test"));
return 0;
}
and it writes 4. Please show us the rest of the program.
Apr 13 '07 #12

"Army1987" <pl********@for.itwrote in message
news:ev**********@tdi.cu.mi.it...
"nick048" <ni*************@moonsoft.itha scritto nel messaggio
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 )
{
cnt++;
sptr += strlen( b );
}
return(cnt);
}

To me, this function seems valid, even if it has horrible style. I would
write:
int word_counter(char *str, char *word)
{
int cnt = 0;
size_t length;
if (word == NULL || *word == '\0')
return -1;
length = strlen(word);
if (str != NULL) {
while ((str = strstr(str, word)) != NULL) {
cnt++;
word += length;
}
}
return cnt;
}

which is perfectly equivalent (except for style, and for checking that
word isn't a null pointer or an empty string.) I think the error is in the
main...
Your reply isn't clear. Did you just make a mistake in your code
or are you implying the OP had the parameters reversed?

Either way this only returns some of the matches and the OPs
question needs to be refined. I am sure someone pointed out,
if haystack is "abababababab" and needle is "abab" what is the
expected return value?
Apr 13 '07 #13

"Barry" <ba****@nullhighstream.netha scritto nel messaggio
news:13*************@corp.supernews.com...
>
"Army1987" <pl********@for.itwrote in message
news:ev**********@tdi.cu.mi.it...
>"nick048" <ni*************@moonsoft.itha scritto nel messaggio
news:11**********************@q75g2000hsh.googleg roups.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 )
{
cnt++;
sptr += strlen( b );
}
return(cnt);
}

To me, this function seems valid, even if it has horrible style. I would
write:
int word_counter(char *str, char *word)
{
int cnt = 0;
size_t length;
if (word == NULL || *word == '\0')
return -1;
length = strlen(word);
if (str != NULL) {
while ((str = strstr(str, word)) != NULL) {
cnt++;
word += length;
}
}
return cnt;
}

which is perfectly equivalent (except for style, and for checking that
word isn't a null pointer or an empty string.) I think the error is in
the main...
Your reply isn't clear. Did you just make a mistake in your code
or are you implying the OP had the parameters reversed?
Which mistake? I'm implying that the OP's error is not in the function
wordCounter, as it is correct (except when b is a null pointer or an empty
string, which is not the case in the OP's example).
Either way this only returns some of the matches and the OPs
question needs to be refined. I am sure someone pointed out,
if haystack is "abababababab" and needle is "abab" what is the
expected return value?
The OP's statement
>> sptr += strlen( b );
makes it clear that (s)he wants to return 3 in that case.
Apr 14 '07 #14

"Army1987" <pl********@for.itha scritto nel messaggio
news:ev**********@tdi.cu.mi.it...
>
"Barry" <ba****@nullhighstream.netha scritto nel messaggio
news:13*************@corp.supernews.com...
>>
"Army1987" <pl********@for.itwrote in message
news:ev**********@tdi.cu.mi.it...
>>int word_counter(char *str, char *word)
{
int cnt = 0;
size_t length;
if (word == NULL || *word == '\0')
return -1;
length = strlen(word);
if (str != NULL) {
while ((str = strstr(str, word)) != NULL) {
cnt++;
word += length;
I meant str += length;
>> }
}
return cnt;
}
>Your reply isn't clear. Did you just make a mistake in your code
or are you implying the OP had the parameters reversed?
Which mistake?
Sorry. Replied too soon.
Apr 14 '07 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: thenetflyer | last post by:
<?php /* Using a function to perform an operation to an occurrence before returning it to the replacement string does not work in preg_replace. Consider the following code in which I want to...
1
by: pulu | last post by:
Hi, In the datawarehouse DB (under MS commerce server 2002) a table stores the referer domain name. Table structure is like refererdomainid <binary>,domainInternalFlag...
2
by: Eric W. Sirko | last post by:
Hi, Eric van der Vlist's book XML Schema says that we can use xs:key to declare co-occurrence constraints. The example he uses (boiled down a bit) is a "book" element that must have either an...
1
by: Robert | last post by:
How can I query an existing table and update a field in each record in the table with the occurrence count of each record e.g. update field to 1 (= first record occurrence), update field to 2 for...
5
by: comp.lang.php | last post by:
$orderBy = 's.app_date desc, s.last_name asc, s.first_name asc, s.mi asc'; if ($_REQUEST) { $ascArray = array('asc' => 'desc', 'desc' => 'asc'); // ARRAY OF ALL ORDERING POSSIBILITIES $junk =...
2
by: mrc-1 | last post by:
Hi, i'd like to validate my XML file (see below) using XML Schema. Now my question: is it possible to limit the occurrence of the element b in the data element. I mean: data can contain up to...
6
by: Registered User | last post by:
Hi experts, I'm trying to write a program to solve the following exercise: Accept three strings from the user. Find the first occurrence of the first string in the third string. If it is present...
8
by: sherifffruitfly | last post by:
Hi, I've been searching as best I can for this - coming up with little. I have a file that is full of lines fitting this pattern: (?<year>\d{4}),(?<amount>\d{6,7}) I'm likely to get a...
10
by: tkpmep | last post by:
For any list x, x.index(item) returns the index of the FIRST occurrence of the item in x. Is there a simple way to identify the LAST occurrence of an item in a list? My solution feels complex -...
6
by: Xernoth | last post by:
Hi, I have an exercise that requests the following: Write a function that reads words from an input stream and stores them in a vector. Use that function both to write programs that count the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.