473,397 Members | 1,949 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,397 software developers and data experts.

How to count occurencies in a Structure

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
9 2012
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
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
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
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
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
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
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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Anand | last post by:
Hi all Please help me to find out table size in MS-SQL how can I count or identify, this specific table is using some xyz kb of space of my hdd. thanks
1
by: Phil Kershaw | last post by:
Hi All, Can anyone help me with a problem that has been driving me mad. I have a table with 10 fields in it for staff to enter their initials to register a vote. I need to count the fields that...
8
by: Invalidlastname | last post by:
Hi, We are developing an asp.net application, and we dynamically created certain literal controls to represent some read-only text for certain editable controls. However, recently we found an issue...
1
by: sunilkeswani | last post by:
Hi I am still new to access. I want to know how i can build a query which can display results from 4 different columns/fields Like. Field1 Field2 Field3 Field4 1 2 1 ...
0
by: lkrubner | last post by:
The idea I'm trying to get at is that I want the tag info for the tag "photography", and I want the date, and I want a count of any comments a tag may have. This following query gets back all the ...
68
by: Martin Joergensen | last post by:
Hi, I have some files which has the following content: 0 0 0 0 0 0 0 1 1 1 1 0 0 1 1 1 1 0 0 1 1 1 1 0 0 1 1 1 1 0 0 0 0 0 0 0
22
by: MP | last post by:
vb6,ado,mdb,win2k i pass the sql string to the .Execute method on the open connection to Table_Name(const) db table fwiw (the connection opened via class wrapper:) msConnString = "Data Source="...
3
by: acorn71 | last post by:
Hello, I need to write a SELECT statement that will display the most popular categories. This means I need a 'category count' for each of the messages in the messages table, and I don't know...
9
by: smarttechie | last post by:
Hii all please help its urgent i have developed an application in xsl. what i want to do is i m searching the xml file based on a search.and based on that search im creating one tree like...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.