473,769 Members | 5,742 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2037
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.itwr ote:
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.d e (Jens Thoms Toerring) wrote:
nick048 <nicosia.gaet.. .@moonsoft.itwr ote:
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.d e
\______________ ____________ 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.itwr ote:
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.d e (Jens Thoms Toerring) wrote:
nick048 <nicosia.gaet.. .@moonsoft.itwr ote:
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.d e
\______________ ____________ 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(te sta, myword, n) ;

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

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

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

int eachline_counte r(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_counte r 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.itwr ote:
I have used your suggestion and I have modified my code in this way:
int word_counter(my list 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_counte r(temp, buf, j);
n = n + occ;
printf("Occurre nces 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_counte r(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_counte r 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_counte r() 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_counte r() 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_counte r(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.itwr ote:
On 7 Apr, 21:11, j...@toerring.d e (Jens Thoms Toerring) wrote:
nick048 <nicosia.gaet.. .@moonsoft.itwr ote:
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.d e
\______________ ____________ 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(te sta, myword, n) ;

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

}

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

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

}

int eachline_counte r(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_counte r 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_count er':
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.Cha ng" <Mi***********@ gmail.comha scritto nel messaggio
news:11******** **************@ b75g2000hsg.goo glegroups.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_count er':
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(te sta, myword, n) ;

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

int word_counter(my list 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_counte r(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_counte r. What is the difference between the two? Why did
you make them different?

What is j?
n = n + occ;
printf("Occurre nces 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_counte r(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_counte r 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
4265
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
1698
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 contain text to tally the votes. At the moment I have a query that counts the votes for each record by using Abs(Not(IsNull(!)))+Abs(Not(IsNull(!)))...............Abs(Not(IsNull(!))) While this works I am sure there is a more elegant way of doing...
8
4279
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 which is related to the repeater. In the code shown below, if I call Repeater1.Controls.Count in the OnInit (the code fragment was highlighted in yellow) , the viewstate for the repeater will be lost during the postback. You can re-produce this...
1
3678
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 1 1 0 0
0
3228
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 info I want, provided the tag has at least one comment. If It doesn't have any comments, it doesn't show up in the return. But if it has a count of zero, I just want it to come back with a count of zero, I don't want it to disappear from the...
68
6826
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
12492
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=" & msDbFilename moConn.Properties("Persist Security Info") = False moConn.ConnectionString = msConnString moConn.CursorLocation = adUseClient moConn.Mode = adModeReadWrite' or using default...same result
3
2551
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 how to do this. Here is the structure of the 2 tables:
9
2982
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 structure. in this tree structure i need the count of siblings in each level. im not getting the count exactly the same the xslt file ----------------- <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0"...
0
9423
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10210
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10043
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8869
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7406
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5446
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3561
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2814
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.