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

Simple string matching exercise

Dear group,

I want to implement a solution to the following link:

http://acmicpc-live-archive.uva.es/n...data/p2006.pdf
As a beginning I am trying to implement this little sample program which
will
delete the repeated strings in two set of strings.

The code:

#include<stdio.h>
#include<stdlib.h>

enum str_match{UN_MATCHED=-1,MATCHED};

unsigned my_strmatch(const char *pattern1,char *pattern2)
{
while(pattern1 != '\0' && pattern2++ != '\0')
if( pattern1 == pattern2)
return MATCHED;
else
return UN_MATCHED;
}
int main(void)
{
char *str1 = "grammer is a important for a language isn't it";
char *str2 = "grammer is a dufficult in a language isn't it";
while(str1 != '\0' && str2 != '\0')
{
if(my_strmatch(str1,str2) == MATCHED)
free(str1);
}
printf("the remaining string are %s",str1);
return 0;
}
I know that I am making a very basic error in pointer memory allocation
which
I am not sure. Can any one help me out in doing this. Don't just write the
program
since that will be too simple for you people. Just give me a hint. Thanks.


Mar 25 '06 #1
9 3942

you can't free string allocated with no malloc.
so char *c = "........";
free(c);

is not allowed.

hth
Ruchir

c_beginner wrote:
Dear group,

I want to implement a solution to the following link:

http://acmicpc-live-archive.uva.es/n...data/p2006.pdf
As a beginning I am trying to implement this little sample program which
will
delete the repeated strings in two set of strings.

The code:

#include<stdio.h>
#include<stdlib.h>

enum str_match{UN_MATCHED=-1,MATCHED};

unsigned my_strmatch(const char *pattern1,char *pattern2)
{
while(pattern1 != '\0' && pattern2++ != '\0')
if( pattern1 == pattern2)
return MATCHED;
else
return UN_MATCHED;
}
int main(void)
{
char *str1 = "grammer is a important for a language isn't it";
char *str2 = "grammer is a dufficult in a language isn't it";
while(str1 != '\0' && str2 != '\0')
{
if(my_strmatch(str1,str2) == MATCHED)
free(str1);
}
printf("the remaining string are %s",str1);
return 0;
}
I know that I am making a very basic error in pointer memory allocation
which
I am not sure. Can any one help me out in doing this. Don't just write the
program
since that will be too simple for you people. Just give me a hint. Thanks.


Mar 25 '06 #2

"c_beginner" <sa***********@REMOVETHISgmail.com>
wrote in message
news:44***********************@news.sunsite.dk...
Dear group,

I want to implement a solution to the following link:
http://acmicpc-live-archive.uva.es/nuevoportal/dat
a/p2006.pdf

As a beginning I am trying to implement this little sample program which will
delete the repeated strings in two set of strings.
The code:

#include<stdio.h>
#include<stdlib.h>

enum str_match{UN_MATCHED=-1,MATCHED};

unsigned my_strmatch(const char *pattern1,char *pattern2) {
while(pattern1 != '\0' && pattern2++ != '\0') if( pattern1 == pattern2)
return MATCHED;
else
return UN_MATCHED;
}
int main(void)
{
char *str1 = "grammer is a important for a language isn't it"; char *str2 = "grammer is a dufficult in a language isn't it"; while(str1 != '\0' && str2 != '\0')
{
if(my_strmatch(str1,str2) == MATCHED) free(str1);
}
printf("the remaining string are %s",str1);
return 0;
}
I know that I am making a very basic error in pointer memory allocation which
I am not sure. Can any one help me out in doing this. Don't just write the program
since that will be too simple for you people. Just give me a hint. Thanks.


<replay made in attachment>

you can't free string allocated with no malloc.
so char *c = "........";
free(c);

is not allowed.

hth
Ruchir

</replay made in attachment>

Ok, I just made the following change.

code:

#include<stdio.h>
#include<stdlib.h>

enum str_match{UN_MATCHED=-1,MATCHED};


unsigned my_strmatch(const char *pattern1,char
*pattern2)
{
while(pattern1 != '\0' && pattern2++ !=
'\0')
if( pattern1 == pattern2)
return MATCHED;
else
return UN_MATCHED;
}
int main(void)
{
char *str1 = malloc(sizeof *str1);
char *str2 = malloc(sizeof *str2);
if(!str1 || !str2)
{
printf("malloc error\n");
exit(1);
}
str1="grammer is a important for a language
isn't it";
str2 = "grammer is a dufficult in a language
isn't it";
while(*(str1++) != '\0' && *(str2++) != '\0')
{
if(my_strmatch(str1,str2) ==
MATCHED)
free(str1);
}
printf("the remaining string are %s",str1);
return 0;
}

end code:

I post incremented the str1 and str2 and checked
the value of the two variable.
(operator preceding rules)

If I make the following change:

while((*str1)++ != '\0' && (*str2)++ != '\0')

that is , first de-refer the pointer and apply
the post increment operator
the program shows a run time error. Can any one
explain me why?

And though the above program is correct it does
not lead to the core issue which
is deleting the matched string. Instead it frees
the whole strings. I need to work
over it.The program does not show any error but
simply does not print anything.

Mar 25 '06 #3
c_beginner wrote:
.... snip ...
Ok, I just made the following change.

.... snip ...

Please quote properly to the message to which you're replying. Read the
material at the following URLs:

<http://cfaj.freeshell.org/google/>
<http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>
<http://www.safalra.com/special/googlegroupsreply/>

Mar 25 '06 #4
Ruchir Bindal wrote:
c_beginner wrote:
Dear group,

I want to implement a solution to the following link:

http://acmicpc-live-archive.uva.es/n...data/p2006.pdf
As a beginning I am trying to implement this little sample program which
will
delete the repeated strings in two set of strings.

The code:

#include<stdio.h>
#include<stdlib.h>

enum str_match{UN_MATCHED=-1,MATCHED};

unsigned my_strmatch(const char *pattern1,char *pattern2)
{
while(pattern1 != '\0' && pattern2++ != '\0')
if( pattern1 == pattern2)
return MATCHED;
else
return UN_MATCHED;
}
int main(void)
{
char *str1 = "grammer is a important for a language isn't it";
char *str2 = "grammer is a dufficult in a language isn't it";
while(str1 != '\0' && str2 != '\0')
{
if(my_strmatch(str1,str2) == MATCHED)
free(str1);
}
printf("the remaining string are %s",str1);
return 0;
}
I know that I am making a very basic error in pointer memory allocation
which
I am not sure. Can any one help me out in doing this. Don't just write the
program
since that will be too simple for you people. Just give me a hint. Thanks.

you can't free string allocated with no malloc.
so char *c = "........";
free(c);

is not allowed.

hth
Ruchir


Please do *not* top post to this group. Please read the following,
thanks.

<http://cfaj.freeshell.org/google/>
<http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>
<http://www.safalra.com/special/googlegroupsreply/>

Mar 25 '06 #5
c_beginner wrote:
.... snip ...
Ok, I just made the following change.

#include<stdio.h>
#include<stdlib.h>

enum str_match{UN_MATCHED=-1,MATCHED};
You could have left the enumerations with their default values. As it
stands now, MATCHED will evaluate to false in a logical expression.
unsigned my_strmatch(const char *pattern1,char *pattern2)
{
while(pattern1 != '\0' && pattern2++ != '\0')
if( pattern1 == pattern2)
return MATCHED;
else
return UN_MATCHED;
}
You've got the code above totally wrong. Read a good book on basic C.
You're making several fundamental errors above.
int main(void)
{
char *str1 = malloc(sizeof *str1);
char *str2 = malloc(sizeof *str2);
if(!str1 || !str2)
{
printf("malloc error\n");
exit(1);
}
str1="grammer is a important for a language
isn't it";
str2 = "grammer is a dufficult in a language
isn't it";
while(*(str1++) != '\0' && *(str2++) != '\0')
{
if(my_strmatch(str1,str2) ==
MATCHED)
free(str1);
}
printf("the remaining string are %s",str1);
return 0;
}

end code:

I post incremented the str1 and str2 and checked
the value of the two variable.
(operator preceding rules)

If I make the following change:

while((*str1)++ != '\0' && (*str2)++ != '\0')

that is , first de-refer the pointer and apply
the post increment operator
the program shows a run time error. Can any one
explain me why?

And though the above program is correct it does
not lead to the core issue which
is deleting the matched string. Instead it frees
the whole strings. I need to work
over it.The program does not show any error but
simply does not print anything.


You've made too many fundamental mistakes in the code above to bother
explaining here. Start with a basic text on C and simple programs.

Mar 25 '06 #6
On Sat, 25 Mar 2006 18:24:16 +0530, "c_beginner"
<sa***********@REMOVETHISgmail.com> wrote:

snip 50 lines of obsolete code

Please include only what is relevant.
</replay made in attachment>
There are no attachments in this newsgroup.

Ok, I just made the following change.

code:

#include<stdio.h>
#include<stdlib.h>

enum str_match{UN_MATCHED=-1,MATCHED};


unsigned my_strmatch(const char *pattern1,char
*pattern2)
{
while(pattern1 != '\0' && pattern2++ !=
'\0')
if( pattern1 == pattern2)
return MATCHED;
else
return UN_MATCHED;
This function returns an unsigned int. Why are you attempting to
return a negative value.
}
int main(void)
{
char *str1 = malloc(sizeof *str1);
str1 has type pointer to char. Therefore *str will always have type
char. sizeof of char is always 1. You need to know how much space
you want to allocate prior to calling malloc. If you don't know this
value when writing the code, you have to include code that will
calculate the value at the time the program executes.
char *str2 = malloc(sizeof *str2);
if(!str1 || !str2)
Good. Many forget to check for success.
{
printf("malloc error\n");
exit(1);
Use EXIT_FAILURE instead of 1 for portability.
}
str1="grammer is a important for a language
isn't it";
You went to all the trouble to have str1 point to allocated memory and
here you throw away the address of that memory, thus creating a memory
leak. The code you have replaces the current value of str1 (the
address returned by malloc) with the address of the string literal.
What you really want is to initialize the memory pointed to by str1
with contents of the string literal. This is done with code like
strcpy(str1, "grammer...");
str2 = "grammer is a dufficult in a language
isn't it";
while(*(str1++) != '\0' && *(str2++) != '\0')
Each pointer is incremented during the evaluation of the while. By
the time you get to the next if statement, your pointers no longer
point to the first character of each string. Consequently, the first
character of each will not participate in your comparison.
{
if(my_strmatch(str1,str2) ==
MATCHED)
Here you are comparing signed and unsigned values.
free(str1);
}
printf("the remaining string are %s",str1);
You just freed str1 above. You can no longer use until you
reinitialize it to point somewhere.
return 0;
You never free str2.
}

end code:

I post incremented the str1 and str2 and checked
the value of the two variable.
What the expression *str1++ actually does is
1 - evaluate to the character str1 currently points to
2 - at some point before the next sequence point, increment
the current value of str1 (so that it points to the next character)
(operator preceding rules)

If I make the following change:

while((*str1)++ != '\0' && (*str2)++ != '\0')
What the expression (*str1)++ actually does is
1 - evaluate to the character str1 currently points to
2 - some time before the next sequence point, increment the
value just evaluated (the incremented value does not participate in
the comparison and the value of the pointer itself never changes)

that is , first de-refer the pointer and apply
the post increment operator
the program shows a run time error. Can any one
What run time error?
explain me why?

And though the above program is correct it does
No it is not correct. The fact that it compiles without error is
necessary but hardly sufficient. Your code invokes undefined behavior
at multiple places.
not lead to the core issue which
is deleting the matched string. Instead it frees
the whole strings. I need to work
over it.The program does not show any error but
simply does not print anything.

Remove del for email
Mar 25 '06 #7
"santosh" <sa*********@gmail.com> writes:
Ruchir Bindal wrote: [snip] Please do *not* top post to this group. Please read the following,
thanks.

<http://cfaj.freeshell.org/google/>
<http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>
<http://www.safalra.com/special/googlegroupsreply/>


And when quoting, trim anything not relevant to your reply. (Yes, I'm
talking to you too, santosh.)

--
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.
Mar 25 '06 #8
Keith Thompson wrote:
"santosh" <sa*********@gmail.com> writes:
Ruchir Bindal wrote:

[snip]
Please do *not* top post to this group. Please read the following,
thanks.

<http://cfaj.freeshell.org/google/>
<http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>
<http://www.safalra.com/special/googlegroupsreply/>


And when quoting, trim anything not relevant to your reply. (Yes, I'm
talking to you too, santosh.)


Thanks. Point noted.

Mar 25 '06 #9
c_beginner wrote:
Dear group,

I want to implement a solution to the following link:

http://acmicpc-live-archive.uva.es/n...data/p2006.pdf


If you follow the requirements at the link, you'll end up with a fairly
involved program. On the other hand, many of the mistakes in the code
you posted indicate you're a beginner. Why not start with simpler demos
and work your way up slowly? Acquire a good C text like K&R and start
with the sample programs and exercises.

Mar 25 '06 #10

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

Similar topics

1
by: Henry | last post by:
I have a table that stores a list of zip codes using a varchar column type, and I need to perform some string prefix pattern matching search. Let's say that I have the columns: 94000-1235 94001...
18
by: Andy Green | last post by:
Emphasis is on efficiancy and speed this is the excercise: The program should monitor a possibly infinite stream of characters from the keyboard (standard input). If it detects the sequence "aaa"...
10
by: Douglas G | last post by:
I've tried various ideas on this problem, but I don't see word wrapping. Can you point out what is wrong? It's a K&R exercise, and I'm still new to programming. Other pointers would be helpful...
19
by: Paul | last post by:
hi, there, for example, char *mystr="##this is##a examp#le"; I want to replace all the "##" in mystr with "****". How can I do this? I checked all the string functions in C, but did not...
6
by: sathyashrayan | last post by:
Dear group, Following is a exercise from a book called "Oreilly's practical C programming". I just wanted to do a couple of C programming exercise. I do have K and R book, but let me try some...
7
by: VUNETdotUS | last post by:
How can I get the text after matching string is found: var str = "1111>AAAA<2222>BBBB<3333>CCCC"; if(str.indexOf("2222>")){ //how to get "BBBB" value following my "2222>" but before "<3333"...
7
by: DirtyRasa | last post by:
Here is what my professor told me to to. Write a function that converts a string to a float and returns a float. Test your function by entering f4 and with 4f. Both should say Data entered was...
11
by: tech | last post by:
Hi, I need a function to specify a match pattern including using wildcard characters as below to find chars in a std::string. The match pattern can contain the wildcard characters "*" and "?",...
2
by: .nLL | last post by:
got below script, works fine with IE but fails on opera. my js knowledge is very limited and can't find whats wrong with it. ---------------------------------------------- function...
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
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.