T
tuchka
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 && (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; j++, i++) {
string[j] = string;
string='\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++)
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?
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 && (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; j++, i++) {
string[j] = string;
string='\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++)
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?