473,508 Members | 2,088 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

struggling with strings

Hi guys!
I am trying to learn C programming but my java background blocks my
brains.
I have a book with a lot of functions and some of them don't work
correctly, so I have opportunity to exercise in C by correction
(strange, huh?).
For example, i want to write function that removes all occurences of
given substring from string.
I have two functions so far:

1. removes first substring from the string (i made it after
corrections of the function that i had in the horrible book:

char *strstr_rem_first(char *string, char *substring) {

int i, j, k, loc=-1;

for(i=0; string[i] && (loc==-1); i++) {
for(j=i, k=0; string[j] ==substring[k];j++, k++)
if(!substring[k+1])
loc=i;
if(loc != -1) { //substring was found
for(k=0; substring[k];k++); {
for(j=loc, i=loc+k; string[i]; j++, i++) {
string[j] = string[i];
string[i]='\0';
}
}
}
}
return (string);
}
2. finds rightmost index of the substring (returns -1 if not found):

int substring_index(const char *string, const char *substring) {

int i, j, k;
for(i=0; string[i];i++)
for(j=i,k=0; string[j] == substring[k]; j++, k++)
if(!substring[k+1])
return(i);

return(-1);
}

Now, armed with these I wanted to construct function that removes all
substrings.

I though about somthing like this:

while(substring_index() > 0)
string_original becomes substring_rem_first

return string_original

Unfortunately, because strings are treated as arrays and i can't
transfer references as easily as i got used to, i can't figure out how
to accomplish it in C.
I shuffled some pointers and some strcpy() etc in vain.
Especially this 'string_original becomes substring_rem_first' bothers
me.
I always end up with 'undefined' reference to 'substring_rem_first'.

Could you help me out? :-)
Nov 13 '05 #1
13 3753
Hi tuchka,

"tuchka" <ia***@hotmail.com> schrieb im Newsbeitrag
news:5f**************************@posting.google.c om...
1. removes first substring from the string (i made it after
corrections of the function that i had in the horrible book:
2. finds rightmost index of the substring (returns -1 if not found):


Check out the functions declared in "string.h". :-)

Here's a function to remove all occurrences of "substring" from "string":

#include <string.h>

char* strstr_rem_all( char* string, const char* substring ) {
char* occurrence; int substr_len = strlen( substring ); int move_len =
strlen( string ) - substr_len + 1; /* +1 b/c of terminating '\0' */
while ( occurrence = strstr( string, substring ) ) {
memmove( occurrence, string + substr_len, move_len );
}
return string;
}

memmove() is used instead of memcpy(), because it handles overlapping
copies. :-)

I hope that helps! :-)

Regards,
Ekkehard Morgenstern.
Nov 13 '05 #2
Thank you, Ekkehard,
I see how that function could've work but i still cannot get it to
work.
The function as written by you gives the following error:

C:\Sveta\C_PROG~1\STRING~1>str_test
123fart456fart789fart

Reverse traf987traf654traf321 is traf987traf654traf321
Filled with x became xxxxxxxxxxxxxxxxxxxxx
After removing first fart became 123456fart789fart
After removing all fart became
123art456rt456rt456rt456rt456rt456rt456rt?

Exiting due to signal SIGSEGV
General Protection Fault at eip=0000772e
eax=00000000 ebx=00000000 ecx=ffffffff edx=ffffffff esi=ffffffff
edi=ffffffff
ebp=00090390 esp=0008ef6c
program=C:\SVETA\C_PROG~1\STRING~1\STR_TEST.EXE
cs: sel=01a7 base=029a0000 limit=0009ffff
ds: sel=01af base=029a0000 limit=0009ffff
es: sel=01af base=029a0000 limit=0009ffff
fs: sel=017f base=00005a50 limit=0000ffff
gs: sel=01bf base=00000000 limit=0010ffff
ss: sel=01af base=029a0000 limit=0009ffff
App stack: [00090560..00010560] Exceptn stack: [000104c0..0000e580]

Call frame traceback EIPs:
0x0000772e
0x00003946
0x00001b82
0x000033d8

I re-wrote it like this but again i've got 'undefined reference' for
str_rem_first(string,substring):

char *strstr_rem_all(char *string, const char *substring) {
//char occurence[strlen(string)];
char *ptr;
int substr_len=strlen(substring);
int move_len=strlen(string)-substr_len+1;

while(ptr=strstr(string, substring)) {
char occurence[strlen(string)];
memmove(occurence, str_rem_first(string,substring),
strlen(string)-substr_len+1);
}
return string;
}

Sigh, sigh :-(
Why the lang. is so hard?
"Ekkehard Morgenstern" <ek******************@onlinehome.de> wrote in message news:<bp**********@online.de>...
Hi tuchka,

"tuchka" <ia***@hotmail.com> schrieb im Newsbeitrag
news:5f**************************@posting.google.c om...
1. removes first substring from the string (i made it after
corrections of the function that i had in the horrible book:
2. finds rightmost index of the substring (returns -1 if not found):


Check out the functions declared in "string.h". :-)

Here's a function to remove all occurrences of "substring" from "string":

#include <string.h>

char* strstr_rem_all( char* string, const char* substring ) {
char* occurrence; int substr_len = strlen( substring ); int move_len =
strlen( string ) - substr_len + 1; /* +1 b/c of terminating '\0' */
while ( occurrence = strstr( string, substring ) ) {
memmove( occurrence, string + substr_len, move_len );
}
return string;
}

memmove() is used instead of memcpy(), because it handles overlapping
copies. :-)

I hope that helps! :-)

Regards,
Ekkehard Morgenstern.

Nov 13 '05 #3
"Ekkehard Morgenstern" <ek******************@onlinehome.de> wrote in message news:<bp**********@online.de>...
Hi tuchka,

"tuchka" <ia***@hotmail.com> schrieb im Newsbeitrag
news:5f**************************@posting.google.c om...
1. removes first substring from the string (i made it after
corrections of the function that i had in the horrible book:
2. finds rightmost index of the substring (returns -1 if not found):
Check out the functions declared in "string.h". :-)

Here's a function to remove all occurrences of "substring" from "string":

#include <string.h>

char* strstr_rem_all( char* string, const char* substring ) {
char* occurrence; int substr_len = strlen( substring ); int move_len =
strlen( string ) - substr_len + 1; /* +1 b/c of terminating '\0' */
while ( occurrence = strstr( string, substring ) ) {
memmove( occurrence, string + substr_len, move_len );
}
return string;
}

memmove() is used instead of memcpy(), because it handles overlapping
copies. :-)


Isn't there something a bit weird here.
Be careful abt how much you are moving ... the length of the initial
string - that of the substring +1 . This does not change inside the
loop but each time the string gets shorter.

What about this ? It should work.

char* strstr_rem_all( char* string, const char* substring ) {
char* occurrence;
int substr_len = strlen( substring );
int move_len;
while ( occurrence = strstr( string, substring ) ) {
move_len = strlen( occurrence ) - substr_len + 1;
memmove( occurrence, occurrence + substr_len, move_len );
}
return string;
}

I hope that helps! :-)

Regards,
Ekkehard Morgenstern.

Nov 13 '05 #4
Hi tuchka,

I didn't test my code before posting it, because I assumed you'd be having
some thought about it before using it, but yeah, there's an obvious bug in
it:

"tuchka" <ia***@hotmail.com> schrieb im Newsbeitrag
news:5f**************************@posting.google.c om...
#include <string.h>

char* strstr_rem_all( char* string, const char* substring ) {
char* occurrence; int substr_len = strlen( substring ); int move_len = strlen( string ) - substr_len + 1; /* +1 b/c of terminating '\0' */
while ( occurrence = strstr( string, substring ) ) {
memmove( occurrence, string + substr_len, move_len );
}
return string;
}


Instead of "memmove( occurrence, string + substr_len, move_len )", it should
read "memmove( occurrence, occurrence + substr_len, move_len )", of course.
:-)

Regards,
Ekkehard Morgenstern.
Nov 13 '05 #5
Hi tuchka,

Sorry this didn't work either. ;-)

"Ekkehard Morgenstern" <ek******************@onlinehome.de> schrieb im
Newsbeitrag news:bp**********@online.de...
Instead of "memmove( occurrence, string + substr_len, move_len )", it should read "memmove( occurrence, occurrence + substr_len, move_len )", of

course.

Anupam was right, I moved too much.

Here's a complete test program that works:

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

char* strstr_rem_all( char* string, const char* substring ) {
char* occurrence; int substr_len = strlen( substring );
while ( occurrence = strstr( string, substring ) ) {
int move_len = strlen( occurrence ) - substr_len + 1; /*
+1 b/c of terminating '\0' */
memmove( occurrence, occurrence + substr_len, move_len );
}
return string;
}

int main( int argc, char** argv ) {
if ( argc >= 3 ) {
printf( "string = '%s', substr = '%s'\n", argv[1],
argv[2] );
strstr_rem_all( argv[1], argv[2] );
printf( "result = '%s'\n", argv[1] );
}
return 0;
}

Notice that the first argument in strstr_rem_all() must point to modifyable
memory, like a string buffer like argv[1]. If you let it point to a constant
(string literal), you will get an access violation error.

I hope this helped! :-)

Regards,
Ekkehard Morgenstern.
Nov 13 '05 #6
Hi Anupam,

"Anupam" <an**************@persistent.co.in> schrieb im Newsbeitrag
news:aa**************************@posting.google.c om...
char* strstr_rem_all( char* string, const char* substring ) {
char* occurrence;
int substr_len = strlen( substring );
int move_len;
while ( occurrence = strstr( string, substring ) ) {
move_len = strlen( occurrence ) - substr_len + 1;
memmove( occurrence, occurrence + substr_len, move_len );
}
return string;
}


Yup, you're right! :-)

I didn't test the code beforehand, and it was late! I posted a complete test
program now for tuchka in the other branch of the message tree. :-)

Coincidentially we have the same solution, I guess there's not many ways to
do it right! ;-)

Regards,
Ekkehard Morgenstern.
Nov 13 '05 #7
Thank you very much Ekkehard and Anupam! It works!
I see that I need to get better book. When I tried to re-write
'memmove'
function (i looked it up in internet) for some reason i did not notice
that type of
arguments were 'void'. I thought it arrays. 'void' does not make much
sense to me. I am lacking
essential information, i see that much. One last small question.

When i place the function directly into the program and call it from
'main' it works like a charm,
but when i put it into 'library' file (that is with asossiated header
file) and call it from test
program it gives the following:

////////////////////////////////////////////////////

After removing first fart became 123456fart678fart
After removing all fart became 123456678
Exiting due to signal SIGSEGV
General Protection Fault at eip=0000767e
eax=00000000 ebx=00000000 ecx=ffffffff edx=ffffffff esi=ffffffff
edi=ffffffff
ebp=00090390 esp=0008ef6c
program=C:\SVETA\C_PROG~1\STRING~1\STR_TEST.EXE
cs: sel=01a7 base=029a0000 limit=0009ffff
ds: sel=01af base=029a0000 limit=0009ffff
es: sel=01af base=029a0000 limit=0009ffff
fs: sel=017f base=00005a50 limit=0000ffff
gs: sel=01bf base=00000000 limit=0010ffff
ss: sel=01af base=029a0000 limit=0009ffff
App stack: [00090560..00010560] Exceptn stack: [000104c0..0000e580]

Call frame traceback EIPs:
0x0000767e
0x00003896
0x00001942
0x000033d8
////////////////////////////////////////////////////

I'm using gcc compiler and link like this:
gcc -c -g strutil.c

gcc -c -g str_test.c

gcc -o str_test.exe str_test.o strutil.o
What it can be? Maybe my compiler or my computer?
Do you have any ideas?

Thanks once again for your help. I'm going for a new book right away.
:-)
Nov 13 '05 #8

Hi tuchka,

"tuchka" <ia***@hotmail.com> schrieb im Newsbeitrag
news:5f**************************@posting.google.c om...
When i place the function directly into the program and call it from
'main' it works like a charm,
but when i put it into 'library' file (that is with asossiated header
file) and call it from test
program it gives the following:


I don't know what you're doing, but it could be that you used a string
literal for both parameters like:

strstr_rem_all( "123123abc123abc", "abc" );

This will cause an access violation, since the function assumes that the
first parameter is a buffer, not a constant.

It will be better to declare the function such that it returns no pointer.
This way you'll have to provide a buffer to get the result:

void strstr_rem_all( char* string, const char* substring ) {
char* occurrence; int substr_len = strlen( substring );
while ( occurrence = strstr( string, substring ) ) {
int move_len = strlen( occurrence ) - substr_len + 1; /* +1 b/c
of terminating '\0' */
memmove( occurrence, occurrence + substr_len, move_len );
}
}

btw, the "void*" in memmove() and memcpy() mean that these parameters can be
assigned any pointer value. Note that "void" is not the same as "void*". :-)

I hope this helps.

Regards,
Ekkehard Morgenstern.
Nov 13 '05 #9
Thank you, Ekkehard,
i see that i need to read regular text book not a 'bible' that i have,
to understand many concepts clearly. All my troubles are originating
from the fact that i don't know what i'm doing :-) I just bought two
books for beginners and going to study it.
Your input was most helpful.

Cheers,
tuchka
"Ekkehard Morgenstern" <ek******************@onlinehome.de> wrote in message news:<bp**********@online.de>...
Hi tuchka,

"tuchka" <ia***@hotmail.com> schrieb im Newsbeitrag
news:5f**************************@posting.google.c om...
When i place the function directly into the program and call it from
'main' it works like a charm,
but when i put it into 'library' file (that is with asossiated header
file) and call it from test
program it gives the following:


I don't know what you're doing, but it could be that you used a string
literal for both parameters like:

strstr_rem_all( "123123abc123abc", "abc" );

This will cause an access violation, since the function assumes that the
first parameter is a buffer, not a constant.

It will be better to declare the function such that it returns no pointer.
This way you'll have to provide a buffer to get the result:

void strstr_rem_all( char* string, const char* substring ) {
char* occurrence; int substr_len = strlen( substring );
while ( occurrence = strstr( string, substring ) ) {
int move_len = strlen( occurrence ) - substr_len + 1; /* +1 b/c
of terminating '\0' */
memmove( occurrence, occurrence + substr_len, move_len );
}
}

btw, the "void*" in memmove() and memcpy() mean that these parameters can be
assigned any pointer value. Note that "void" is not the same as "void*". :-)

I hope this helps.

Regards,
Ekkehard Morgenstern.

Nov 13 '05 #10
ia***@hotmail.com (tuchka) wrote in message news:<5f**************************@posting.google. com>...
Thank you very much Ekkehard and Anupam! It works!
I see that I need to get better book. When I tried to re-write
'memmove'
function (i looked it up in internet) for some reason i did not notice
that type of
arguments were 'void'. I thought it arrays. 'void' does not make much
sense to me. I am lacking
essential information, i see that much. One last small question.

When i place the function directly into the program and call it from
'main' it works like a charm,
but when i put it into 'library' file (that is with asossiated header
file) and call it from test
program it gives the following:

////////////////////////////////////////////////////

After removing first fart became 123456fart678fart
After removing all fart became 123456678
Exiting due to signal SIGSEGV
General Protection Fault at eip=0000767e
eax=00000000 ebx=00000000 ecx=ffffffff edx=ffffffff esi=ffffffff
edi=ffffffff
ebp=00090390 esp=0008ef6c
program=C:\SVETA\C_PROG~1\STRING~1\STR_TEST.EXE
cs: sel=01a7 base=029a0000 limit=0009ffff
ds: sel=01af base=029a0000 limit=0009ffff
es: sel=01af base=029a0000 limit=0009ffff
fs: sel=017f base=00005a50 limit=0000ffff
gs: sel=01bf base=00000000 limit=0010ffff
ss: sel=01af base=029a0000 limit=0009ffff
App stack: [00090560..00010560] Exceptn stack: [000104c0..0000e580]

Call frame traceback EIPs:
0x0000767e
0x00003896
0x00001942
0x000033d8
////////////////////////////////////////////////////

I'm using gcc compiler and link like this:
gcc -c -g strutil.c

gcc -c -g str_test.c

gcc -o str_test.exe str_test.o strutil.o
What it can be? Maybe my compiler or my computer?
There is *always* only one problem and that's incorrect coding. It is
tempting to think that we have discovered a major bug... :) but in
99.967% (there's a nice signature floating around to corroborate this)
of the cases, the fault lies with us.

Interesting . Could you give us the code for both the files. It would
be helpful if we had solid code at hand. I would not like to comment
before seeing that.
Do you have any ideas?

Thanks once again for your help. I'm going for a new book right away.
:-)


Glad to be of help. Please do use the services of this newsgroup
for clearing up your doubts. I would also like to suggest that you
could try using the Turbo C IDE. I had grown up with it and have
always loved the excellent help system. It is very simple , not hi-fi
, but it gives one a beautiful test bed to program. Of course I was
initially trapped into thinking that it was C and only later realised
that C is much bigger. No doubt there'll be many people with wider
experience who can suggest better ones. As far as books go I'll just
keep mum. I have learnt that it can be very dicey to suggest a book...
;) It is a matter of much sensitivity. K&R would be recommended though
at least at some point in the learning curve.

Regards,
Anupam
Nov 13 '05 #11
an**************@persistent.co.in (Anupam) wrote in message news:<aa**************************@posting.google. com>...
ia***@hotmail.com (tuchka) wrote in message news:<5f**************************@posting.google. com>...
Thank you very much Ekkehard and Anupam! It works!
I see that I need to get better book.

[snip]
Here's a great list of books mainatined by Richard Heathfield.
http://users.powernet.co.uk/eton/clc/cbooks.html
While you are at it, one of the good places to look for information is
right at your fingertips...literally... look at the signatures on clc.
You are invariably led to the *good* places.
Best of luck,
Anupam
Nov 13 '05 #12
Hi Anupam:
Please take a look at my files. I wonder if you find the cause of this
exception. It would be very interesting. I checked once again
everything (i even found out that i forgot to include strstr_rem_all()
into header!) but the message still appears after execution of
str_test.exe that i am getting by the following commands:
C:\C_PROG~1\STRING~1>gcc -c -g strutil.c

C:\C_PROG~1\STRING~1>gcc -c -g str_test.c

C:\C_PROG~1\STRING~1>gcc -o str_test.exe str_test.o strutil.o

C:\C_PROG~1\STRING~1>str_test
123fart456fart789fart

The following are my files:
--------------------------------------------------------
str_test2.c (as you guys suggested is working very well)
--------------------------------------------------------

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

char* strstr_rem_all( char* string, const char* substring ) {
char* occurrence; int substr_len = strlen( substring );
while ( occurrence = strstr( string, substring ) ) {
int move_len = strlen( occurrence ) - substr_len + 1;
/*
+1 b/c of terminating '\0' */
memmove( occurrence, occurrence + substr_len, move_len
);
}
return string;
}

int main( int argc, char** argv ) {
if ( argc >= 3 ) {
printf( "string = '%s', substr = '%s'\n", argv[1],
argv[2] );
strstr_rem_all( argv[1], argv[2] );
printf( "result = '%s'\n", argv[1] );
}
return 0;
}
------------------------------------------------------------------------
strutil.h (i am making to create my library)
----------------------------------------------------------------------
/*strutil.h*/

extern char *strrev(char *string); //return reversed string
extern char *strset(char *string, int letter);
extern char *strstr_rem_first(char *string, char *substring);
extern int substring_index(const char *string, const char *substring);
extern char *strstr_rem_all( char *string, const char *substring );
-----------------------------------------------------------------------
strutil.c (contains my functions)
-----------------------------------------------------------------------
#include <string.h>
#include "strutil.h"

//reverse
char *strrev(char *string) {
char *backward = string;
char *forward = string;
char temp;
//set 'backward' to the null byte terminating the string
while(*backward){
backward++;
}
while(forward<backward) {
--backward;
temp=*backward;
*backward=*forward;
*forward=temp;
++forward;
}
return (string);
}
//setting to char
char *strset(char *string, int letter) {
char *original=string;

while(*string) {
*string++ = letter;
}
return (original);
}
//finds rightmost index of substr
int substring_index(const char *string, const char *substring) {
int i, j, k;

for(i=0; string[i];i++)
for(j=i,k=0; string[j] == substring[k]; j++, k++)
if(!substring[k+1])
return(i);
return(-1);
}
//removes first sbstr
char *strstr_rem_first(char *string, char *substring) {
int i, j, k, loc=-1;

for(i=0; string[i] && (loc==-1); i++) {
for(j=i, k=0; string[j] ==substring[k];j++, k++)
if(!substring[k+1])
loc=i;
if(loc != -1) { //substring was found
for(k=0; substring[k];k++); {
for(j=loc, i=loc+k; string[i]; j++, i++) {
string[j] = string[i];
string[i]='\0';
}
}
}
}
return (string);
}
//removes all substr
char *strstr_rem_all( char *string, const char *substring ) {
char *occurrence; int substr_len = strlen( substring );
while (occurrence = strstr( string, substring ) ) {
int move_len = strlen( occurrence ) - substr_len + 1;
memmove( occurrence, occurrence + substr_len, move_len
);
}
return string;
}
-----------------------------------------------------------------------
str_test.c (which after compiling and linking with strutil.c gives the
above message)
-------------------------------------------------------------------------
#include <stdio.h>
#include <string.h>
#include "strutil.h"

int main() {

char *str_ptr;
char *substr_ptr;

char letter ='x';
char s[100];
char s_cache1[100];
char s_cache2[100];
char substr[5];

gets(s);

str_ptr=s;
strcpy(substr, "fart");

strcpy(s_cache1, s);
strcpy(s_cache2, s);

printf("\nReverse %s is %s", strdup(s), strrev(s));

printf("\nFilled with %c became %s", letter, strset(s, letter));

printf("\nAfter removing first %s became %s", substr,
strstr_rem_first(s_cache1, "fart"));
printf("\nAfter removing all %s became %s", substr,
strstr_rem_all(s_cache2, "fart"));

printf("\nIndex of first substring in %s is %d",
substring_index("hello", "fart"));

return 0;

}
--------------------------------------------------------------------------

Regards,
tuchka:-)
Nov 13 '05 #13
ia***@hotmail.com (tuchka) wrote in message news:<5f**************************@posting.google. com>...
Hi Anupam:
Please take a look at my files. I wonder if you find the cause of this
exception. It would be very interesting. I checked once again
everything (i even found out that i forgot to include strstr_rem_all()
into header!) but the message still appears after execution of
str_test.exe that i am getting by the following commands:
C:\C_PROG~1\STRING~1>gcc -c -g strutil.c

C:\C_PROG~1\STRING~1>gcc -c -g str_test.c

C:\C_PROG~1\STRING~1>gcc -o str_test.exe str_test.o strutil.o

C:\C_PROG~1\STRING~1>str_test
123fart456fart789fart

Wow didn't you supply the command line parameters here? This I cant
get. And still it is outputting stuff. Check it once . Maybe
something's wrong in what u gave us.

The following are my files:
--------------------------------------------------------
str_test2.c (as you guys suggested is working very well)
?? str_test.c I think from the compilation. Is this the code u
compiled?

int main( int argc, char** argv ) {
if ( argc >= 3 ) {
printf( "string = '%s', substr = '%s'\n", argv[1],
argv[2] );
strstr_rem_all( argv[1], argv[2] );
printf( "result = '%s'\n", argv[1] );
}
return 0;
Consider return EXIT_SUCCESS (or FAILURE) by including stdlib.h
}
------------------------------------------------------------------------
strutil.h (i am making to create my library)
----------------------------------------------------------------------
/*strutil.h*/

extern char *strrev(char *string); //return reversed string
extern char *strset(char *string, int letter);
Whoa, check out your names, they are common to functions in string.h
which also u r including.
extern char *strstr_rem_first(char *string, char *substring);
extern int substring_index(const char *string, const char *substring);
extern char *strstr_rem_all( char *string, const char *substring );
-----------------------------------------------------------------------
strutil.c (contains my functions)
-----------------------------------------------------------------------
#include <string.h>
#include "strutil.h"

//reverse
char *strrev(char *string) {
char *backward = string;
char *forward = string;
char temp;
//set 'backward' to the null byte terminating the string
while(*backward){
backward++;
}
while(forward<backward) {
--backward;
temp=*backward;
*backward=*forward;
*forward=temp;
++forward;
}
return (string);
}
Compiler is thinking : Is this the real strrev or is the one in the
standard library the one u want to use? BIG problem.
//setting to char
char *strset(char *string, int letter) {
char *original=string;

while(*string) {
*string++ = letter;
}
return (original);
}
//finds rightmost index of substr
int substring_index(const char *string, const char *substring) {
int i, j, k;

for(i=0; string[i];i++)
for(j=i,k=0; string[j] == substring[k]; j++, k++)
if(!substring[k+1])
return(i);
return(-1);
}
//removes first sbstr
char *strstr_rem_first(char *string, char *substring) {
int i, j, k, loc=-1;

for(i=0; string[i] && (loc==-1); i++) {
for(j=i, k=0; string[j] ==substring[k];j++, k++)
if(!substring[k+1])
loc=i;
if(loc != -1) { //substring was found
for(k=0; substring[k];k++); {
for(j=loc, i=loc+k; string[i]; j++, i++) {
string[j] = string[i];
string[i]='\0';
}
}
}
}
return (string);
}
//removes all substr
char *strstr_rem_all( char *string, const char *substring ) {
char *occurrence; int substr_len = strlen( substring );
while (occurrence = strstr( string, substring ) ) {
int move_len = strlen( occurrence ) - substr_len + 1;
memmove( occurrence, occurrence + substr_len, move_len
);
}
return string;
}
-----------------------------------------------------------------------
str_test.c (which after compiling and linking with strutil.c gives the
above message)
-------------------------------------------------------------------------
#include <stdio.h>
#include <string.h>
#include "strutil.h"

int main() {

char *str_ptr;
char *substr_ptr;

char letter ='x';
char s[100];
char s_cache1[100];
char s_cache2[100];
char substr[5];

gets(s); Warning ! Warning ! gets is dangerous . Refer to Richard Heathfield'
s page for more info...(The URL was already given earlier)

str_ptr=s;
strcpy(substr, "fart");

strcpy(s_cache1, s);
strcpy(s_cache2, s);

printf("\nReverse %s is %s", strdup(s), strrev(s));

printf("\nFilled with %c became %s", letter, strset(s, letter));

printf("\nAfter removing first %s became %s", substr,
strstr_rem_first(s_cache1, "fart"));
printf("\nAfter removing all %s became %s", substr,
strstr_rem_all(s_cache2, "fart"));

printf("\nIndex of first substring in %s is %d",
substring_index("hello", "fart"));

return 0;

}
--------------------------------------------------------------------------

Regards,
tuchka:-)


Try changing the names of those functions. Your function names are
colliding with the standard ones in the global namespace.
Nov 13 '05 #14

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

Similar topics

5
13763
by: Geoffrey | last post by:
Hope someone can help. I am trying to read data from a file binary file and then unpack the data into python variables. Some of the data is store like this; xbuffer:...
22
2055
by: Stan | last post by:
Hey everyone, I've got a computer science class and we're working on C++. I am struggling with nested loops and have a really simple assignment, but I can't get it to run the loop. I need to run...
4
1996
by: Rowan | last post by:
Hi there, it's me again. I am having trouble with a view again. I am trying to do a calculation, but there are some checks that need to be taken into consideration. Maybe a view is not the right...
8
1402
by: Jason | last post by:
A week ago I posted a simple little hi-score routine that I was using to learn Python. I've only just managed to examine the code, and the responses that people gave, and I'm now seriously...
2
5528
by: Riaan C | last post by:
Hi I'm new to C. Here's the problem. I understand the whole concept of pointers and can easily use it with normal non-array variables. I want to declare a array of strings, thus...
26
6731
by: dagger | last post by:
Hi there. I'm using C under FreeBSD with the gcc compiler and am having a bit of trouble using the calloc and realloc calls. As an example the code snippet: #include <stdio.h> int main() {...
4
5577
by: Angus Comber | last post by:
Hello I have received a lot of help on my little project here. Many thanks. I have a struct with a string and a long member. I have worked out how to qsort the struct on both members. I can...
18
1882
by: Tony Burrows | last post by:
I'm just learning C as another language, and I'm trying to build some utilities into a library. I have this (crude I know) function: void insert(char* insrt, char* source, int place){ char...
11
22761
by: briankind | last post by:
Hello i have these radio buttons and i wish to have the corresponding textboxes to be visible when the radiobutton is selected. any Help? snippets. thanks thanks in adv Bry
0
7225
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
7124
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
7326
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
7385
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
7498
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
5629
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,...
0
4707
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...
0
3182
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
418
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...

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.