By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,483 Members | 1,052 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,483 IT Pros & Developers. It's quick & easy.

How to count occurencies in a Structure

P: n/a
Hi to All,

I need to search the string occurencies in a structure like this:

struct node
{
char info[BUFLEN];
struct node *next;
};

/* the word to search */
char myWord[10];


Can You suggest to me a sample, in order to resolve my problem ?

Best Regards
Gaetano

Apr 7 '07 #1
Share this Question
Share on Google+
9 Replies


P: n/a
nick048 wrote:
Hi to All,

I need to search the string occurencies in a structure like this:

[i]struct node
Please drop the silly forum-style format marker like [i] and [b]. They
don't work for most usenet newsreaders, in particular they don't work
for Google Groups, which is the one you use. You should have noticed
that by now.

It makes your code harder to read, and makes it impossible to cut and
past into our compiler to test.


Brian
Apr 7 '07 #2

P: n/a
nick048 <ni*************@moonsoft.itwrote:
I need to search the string occurencies in a structure like this:
struct node
{
char info[BUFLEN];
struct node *next;
};

/* the word to search */
char myWord[10];
Can You suggest to me a sample, in order to resolve my problem ?
There are lot of informations missing and the question is rather
unclear. First of all, what means "search string occurrencies in
a structure"? I guess you mean search in the 'info' member of the
structure. But it's still unclear if you want only matches of the
whole string (in wich case strcmp() would probably be useful) or
if you look for all the substrings that matches 'myWord' (strstr()
would look like a good candidate for a function to use) etc. And
then the structure looks a lot like an element of a linked list.
Are you perhaps looking for searching in the 'info' members of
all the structures that make up the linked list? Please specify
more clearly what the problem is, it hardly makes sense to answer
a question you didn't intended to ask.

And could you please refrain from putting this '' and ''
stuff into the code? It only makes it harder to read and one
can't simply past the code into a program without having to
remove them before trying to compile it.

Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
Apr 7 '07 #3

P: n/a
On 7 Apr, 18:45, j...@toerring.de (Jens Thoms Toerring) wrote:
nick048 <nicosia.gaet...@moonsoft.itwrote:
I need to search the string occurencies in a structure like this:
struct node
{
char info[BUFLEN];
struct node *next;
};
/* the word to search */
char myWord[10];

Can You suggest to me a sample, in order to resolve my problem ?

There are lot of informations missing and the question is rather
unclear. First of all, what means "search string occurrencies in
a structure"? I guess you mean search in the 'info' member of the
structure. But it's still unclear if you want only matches of the
whole string (in wich case strcmp() would probably be useful) or
if you look for all the substrings that matches 'myWord' (strstr()
would look like a good candidate for a function to use) etc. And
then the structure looks a lot like an element of a linked list.
Are you perhaps looking for searching in the 'info' members of
all the structures that make up the linked list? Please specify
more clearly what the problem is, it hardly makes sense to answer
a question you didn't intended to ask.

And could you please refrain from putting this '' and ''
stuff into the code? It only makes it harder to read and one
can't simply past the code into a program without having to
remove them before trying to compile it.

Regards, Jens
--
\ Jens Thoms Toerring ___ j...@toerring.de
\__________________________ http://toerring.de
Ok, sorry.
I post an example.

in the struct I insert 3 or more string in node->info like these:
1) "this is the first string and after is the second string"
2) "you are insert the second string"
3) "after this string, are more string"

myString = "string" appear 2 times in 1), 1 time in 2) and 2 times in
3). The total of searched occurrencies is 5.

Exist a method in order to count the occurrencies of myWord ?

Best Regards
Gaetano

struct node
{
char info[BUFLEN];
struct node *next;
};
/* the word to search */
char myWord[10];

Apr 7 '07 #4

P: n/a
nick048 <ni*************@moonsoft.itwrote:
I post an example.
in the struct I insert 3 or more string in node->info like these:
1) "this is the first string and after is the second string"
2) "you are insert the second string"
3) "after this string, are more string"
myString = "string" appear 2 times in 1), 1 time in 2) and 2 times in
3). The total of searched occurrencies is 5.
Exist a method in order to count the occurrencies of myWord ?
To find the number of occurr in a single string there's not a single
function but it's easy using strstr():

#include <stdio.h>
#include <string.h>

int main( void )
{
char a[ ] = "this is the first string and after is the second string";
char b[ ] = "string";
int cnt = 0;
char *sptr = a;

while ( *sptr != '\0' && ( sptr = strstr( sptr, b ) ) != NULL )
{
cnt++;
sptr += strlen( b );
}

printf( "%d\n", cnt );
return 0;
}

But please note that the problem is still not completely specified:
what should be the result if you search in for "AA" in the string
"AAAAA"? The above method would return 2, but one also could argue
that it should be 4. To get that result you would have to change
the line

sptr += strlen( b );
to
sptr++;

in the while loop.

If you have a number of strings just convert the above into a
function that takes two strings (the string to search in and
the string to search for) as its arguments and returns the
number of occurrences. Then call that function for each of
the strings and add up the results.

Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
Apr 7 '07 #5

P: n/a
On 7 Apr, 21:11, j...@toerring.de (Jens Thoms Toerring) wrote:
nick048 <nicosia.gaet...@moonsoft.itwrote:
I post an example.
in the struct I insert 3 or more string in node->info like these:
1) "this is the first string and after is the second string"
2) "you are insert the second string"
3) "after this string, are more string"
myString = "string" appear 2 times in 1), 1 time in 2) and 2 times in
3). The total of searched occurrencies is 5.
Exist a method in order to count the occurrencies of myWord ?

To find the number of occurr in a single string there's not a single
function but it's easy using strstr():

#include <stdio.h>
#include <string.h>

int main( void )
{
char a[ ] = "this is the first string and after is the second string";
char b[ ] = "string";
int cnt = 0;
char *sptr = a;

while ( *sptr != '\0' && ( sptr = strstr( sptr, b ) ) != NULL )
{
cnt++;
sptr += strlen( b );
}

printf( "%d\n", cnt );
return 0;

}

But please note that the problem is still not completely specified:
what should be the result if you search in for "AA" in the string
"AAAAA"? The above method would return 2, but one also could argue
that it should be 4. To get that result you would have to change
the line

sptr += strlen( b );
to
sptr++;

in the while loop.

If you have a number of strings just convert the above into a
function that takes two strings (the string to search in and
the string to search for) as its arguments and returns the
number of occurrences. Then call that function for each of
the strings and add up the results.

Regards, Jens
--
\ Jens Thoms Toerring ___ j...@toerring.de
\__________________________ http://toerring.de
Ok and thank You,
I have used your suggestion and I have modified my code in this way:

/*** HERE the various #define ***/

#define BUFLEN 100

struct node
{
char info[BUFLEN] ;
struct node *pun ;
} ;

typedef struct node* mylist ;

/*** HERE the various Prototype ***/

int main(int argc, char *argv[])
{
int occur, n;
mylist testa = NULL ;
char myWord[20];
char mystring[BUFLEN];

/**** HERE the Input code forf mystring and the code inserting each
mystring in the struct****/

/* I search the occurrence number in the entire list */
occur = word_counter(testa, myword, n) ;

printf("The occurrences are: %d\n",occur);
}

int word_counter(mylist p, const char *buf, int n)
{
int occ;
char temp[BUFLEN];

occ = 0;
n = 0;
while (p != NULL)
{
strcpy(temp, p->info);
occ = eachline_counter(temp, buf, j);
n = n + occ;
printf("Occurrences for this string are: %d\n", n ) ;
p = p->pun ;
}
return (n) ;
}

int eachline_counter(char *a, char *b, int rip)
{
int cnt = 0;
char *sptr = a;

while ( *sptr != '\0' && ( sptr = strstr( sptr, b ) ) != NULL )
{
cnt++;
sptr += strlen( b );
}

printf( "%d\n", cnt );
return (cnt);
};

But when I compile the program, the GCC compiler return to me this
error:
>In function 'word_counter'
Passing arg 2 of eachline_counter discards qualifier from pointer target type
Sure, I mistake something; but I don't see the error. Can You help
me ?

Thank You and Best Regards
Gaetano

Apr 8 '07 #6

P: n/a
nick048 <ni*************@moonsoft.itwrote:
I have used your suggestion and I have modified my code in this way:
int word_counter(mylist p, const char *buf, int n)
{
int occ;
char temp[BUFLEN];
occ = 0;
n = 0;
while (p != NULL)
{
strcpy(temp, p->info);
Why do you strcpy() the string to a tempoary string? The only
function you use the string in does not modify it, so making
a copy seems to be a waste of time and memory.
occ = eachline_counter(temp, buf, j);
n = n + occ;
printf("Occurrences for this string are: %d\n", n ) ;
p = p->pun ;
}
return (n) ;
Since 'return' isn't a function you don't need parentheses here.
It's a bit like writing

a = ( ( b ) + ( 1 ) );

which also isn't wrong but looks a bit strange (at least to me;-)
}
int eachline_counter(char *a, char *b, int rip)
{
int cnt = 0;
char *sptr = a;
while ( *sptr != '\0' && ( sptr = strstr( sptr, b ) ) != NULL )
{
cnt++;
sptr += strlen( b );
}
printf( "%d\n", cnt );
return (cnt);
};
But when I compile the program, the GCC compiler return to me this
error:
In function 'word_counter'
Passing arg 2 of eachline_counter discards qualifier from pointer target type
Sure, I mistake something; but I don't see the error. Can You help
me ?
Your only "mistake" is to believe that this is an error while it's
only a gentle warning;-) You get it because 'buf' is a const char
pointer but the function you pass it to takes just a char pointer.
The 'const' part means a promise that the content of the memory the
pointer points to won't get changed. So the compiler can assume that
you won't change what 'buf' points to in word_counter(), but then you
pass it there to a function which doesn't make this promise. And
thus the compiler dutifully warns you that there would be a problem
if the function eachline_counter() should change the memory the
second argument points to. But since this function doesn't do this
it's not really a problem, but the compiler can't figure that out.

Since eachline_counter() doesn't modify any of the strings it gets
passed pointers to you could get rid of the warning by defining it
instead as taking two const char pointers:

int eachline_counter(const char *a, const char *b, int rip)

I think it' a good idea to generally qualify all pointer arguments
to functions as const if the memory they point to doesn't get changed
within the function. That way you can immediately see also half a
year later when you have to modify the program that you don't have
to worry about what happens to the arguments without taking a closer
look at the function and it also may help the compiler spot some of
my more stupid mistakes. But you can also simply disregard the
warning after once you understood what it's about and found that
it's nothing to worry about.
Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
Apr 8 '07 #7

P: n/a
On Apr 8, 3:09 pm, "nick048" <nicosia.gaet...@moonsoft.itwrote:
On 7 Apr, 21:11, j...@toerring.de (Jens Thoms Toerring) wrote:
nick048 <nicosia.gaet...@moonsoft.itwrote:
I post an example.
in the struct I insert 3 or more string in node->info like these:
1) "this is the first string and after is the second string"
2) "you are insert the second string"
3) "after this string, are more string"
myString = "string" appear 2 times in 1), 1 time in 2) and 2 times in
3). The total of searched occurrencies is 5.
Exist a method in order to count the occurrencies of myWord ?
To find the number of occurr in a single string there's not a single
function but it's easy using strstr():
#include <stdio.h>
#include <string.h>
int main( void )
{
char a[ ] = "this is the first string and after is the second string";
char b[ ] = "string";
int cnt = 0;
char *sptr = a;
while ( *sptr != '\0' && ( sptr = strstr( sptr, b ) ) != NULL )
{
cnt++;
sptr += strlen( b );
}
printf( "%d\n", cnt );
return 0;
}
But please note that the problem is still not completely specified:
what should be the result if you search in for "AA" in the string
"AAAAA"? The above method would return 2, but one also could argue
that it should be 4. To get that result you would have to change
the line
sptr += strlen( b );
to
sptr++;
in the while loop.
If you have a number of strings just convert the above into a
function that takes two strings (the string to search in and
the string to search for) as its arguments and returns the
number of occurrences. Then call that function for each of
the strings and add up the results.
Regards, Jens
--
\ Jens Thoms Toerring ___ j...@toerring.de
\__________________________ http://toerring.de

Ok and thank You,
I have used your suggestion and I have modified my code in this way:

/*** HERE the various #define ***/

#define BUFLEN 100

struct node
{
char info[BUFLEN] ;
struct node *pun ;

} ;

typedef struct node* mylist ;

/*** HERE the various Prototype ***/

int main(int argc, char *argv[])
{
int occur, n;
mylist testa = NULL ;
char myWord[20];
char mystring[BUFLEN];

/**** HERE the Input code forf mystring and the code inserting each
mystring in the struct****/

/* I search the occurrence number in the entire list */
occur = word_counter(testa, myword, n) ;

printf("The occurrences are: %d\n",occur);

}

int word_counter(mylist p, const char *buf, int n)
{
int occ;
char temp[BUFLEN];

occ = 0;
n = 0;
while (p != NULL)
{
strcpy(temp, p->info);
occ = eachline_counter(temp, buf, j);
n = n + occ;
printf("Occurrences for this string are: %d\n", n ) ;
p = p->pun ;
}
return (n) ;

}

int eachline_counter(char *a, char *b, int rip)
{
int cnt = 0;
char *sptr = a;

while ( *sptr != '\0' && ( sptr = strstr( sptr, b ) ) != NULL )
{
cnt++;
sptr += strlen( b );
}

printf( "%d\n", cnt );
return (cnt);

};

But when I compile the program, the GCC compiler return to me this
error:
In function 'word_counter'
Passing arg 2 of eachline_counter discards qualifier from pointer target type

Sure, I mistake something; but I don't see the error. Can You help
me ?

Thank You and Best Regards
Gaetano
here is what my gcc compiler tells:

str.c: In function `main':
str.c:18: error: `NULL' undeclared (first use in this function)
str.c:18: error: (Each undeclared identifier is reported only once
str.c:18: error: for each function it appears in.)
str.c:26: error: `myword' undeclared (first use in this function)
str.c: In function `word_counter':
str.c:39: error: `NULL' undeclared (first use in this function)
str.c:42: error: `j' undeclared (first use in this function)
str.c: In function `eachline_counter':
str.c:55: error: `NULL' undeclared (first use in this function)
>It seems that we are encountering different errors, thinking different things, even the compiler behaving in different ways...
Apr 8 '07 #8

P: n/a
"Thomas.Chang" <Mi***********@gmail.comha scritto nel messaggio
news:11**********************@b75g2000hsg.googlegr oups.com...
>/*** HERE the various #define ***/
It's likely to contain #include <stdlib.h>, too.
here is what my gcc compiler tells:

str.c: In function `main':
str.c:18: error: `NULL' undeclared (first use in this function)
str.c:18: error: (Each undeclared identifier is reported only once
str.c:18: error: for each function it appears in.)
str.c:26: error: `myword' undeclared (first use in this function)
str.c: In function `word_counter':
str.c:39: error: `NULL' undeclared (first use in this function)
str.c:42: error: `j' undeclared (first use in this function)
str.c: In function `eachline_counter':
str.c:55: error: `NULL' undeclared (first use in this function)
>>It seems that we are encountering different errors, thinking different
things, even the compiler behaving in different ways...
See above
Apr 8 '07 #9

P: n/a
On 8 Apr 2007 00:09:13 -0700, "nick048" <ni*************@moonsoft.it>
wrote:

snip almost 1/2 of post

Please trim your quotes. We don't need to see the obsolete code you
are replacing.
>Ok and thank You,
I have used your suggestion and I have modified my code in this way:
Here missing the various #include.
>/*** HERE the various #define ***/

#define BUFLEN 100

struct node
{
char info[BUFLEN] ;
struct node *pun ;
} ;

typedef struct node* mylist ;

/*** HERE the various Prototype ***/

int main(int argc, char *argv[])
{
int occur, n;
mylist testa = NULL ;
char myWord[20];
char mystring[BUFLEN];

/**** HERE the Input code forf mystring and the code inserting each
mystring in the struct****/

/* I search the occurrence number in the entire list */
occur = word_counter(testa, myword, n) ;

printf("The occurrences are: %d\n",occur);
}

int word_counter(mylist p, const char *buf, int n)
{
int occ;
char temp[BUFLEN];

occ = 0;
n = 0;
What purpose is served by passing n into this function if you do not
use the value passed?
while (p != NULL)
{
strcpy(temp, p->info);
occ = eachline_counter(temp, buf, j);
Look up above and see the type of buf in the function definition. Look
down below and see the type of the second parameter in the definition
of eachline_counter. What is the difference between the two? Why did
you make them different?

What is j?
n = n + occ;
printf("Occurrences for this string are: %d\n", n ) ;
p = p->pun ;
}
return (n) ;
return is not a function; it is a statement. The parentheses are
unnecessary.
>}

int eachline_counter(char *a, char *b, int rip)
What is rip? You don't use it in this function.
>{
int cnt = 0;
char *sptr = a;

while ( *sptr != '\0' && ( sptr = strstr( sptr, b ) ) != NULL )
{
cnt++;
sptr += strlen( b );
}

printf( "%d\n", cnt );
return (cnt);
};

But when I compile the program, the GCC compiler return to me this
error:
>>In function 'word_counter'
Passing arg 2 of eachline_counter discards qualifier from pointer target type

Sure, I mistake something; but I don't see the error. Can You help
me ?

Thank You and Best Regards
Gaetano

Remove del for email
Apr 8 '07 #10

This discussion thread is closed

Replies have been disabled for this discussion.