Re: sentence
- From: av <av@xxxxx>
- Date: Mon, 19 Jun 2006 07:07:44 +0200
On Sun, 18 Jun 2006 08:23:35 +0100, "Malcolm"
<regniztar@xxxxxxxxxxxxxx> wrote:
"Sidhu" <sidhu4u.90@xxxxxxxxx> wrote
Hoe to relplace the word in sentence? Can any one send program?Write a function like this
/*
function to replace all instances of a word in a string
Params: out - the output buffer (may not be the same as any input)
original - the original text
search - the sub string to seach for
replacement - the string to replace it with
returns: the number of replacements made
*/
int replace(char *out, char *original, char *search, char *replacement)
#include <stdio.h>
#define F for
#define R return
#define W while
#define RE realloc
#define B break
#define G goto
#define uns unsigned
int init_kmn(int* next, int snext, char* p)
{int i,j, k;
if(--snext<=0) R -1; // snext==1023
F(i=0, j=-1, next[0]=-1; p[i]&&i<snext; )
{W(j>=0 && p[i]!=p[j] ) j=next[j]; // i==1022
++i; ++j; // i==1023
next[i]=(p[i]==p[j]? next[j]: j);
}
if(i>=snext) R -1;
R i; // ritorna la lunghezza di p
}
char* kmn(char* p, char* a, int* next, int i)
{int j, k;
F(k=0, j=0; j<i&&a[k]; ++j, ++k)
W(j>=0 && a[k]!=p[j]) j=next[j];
R (j==i? a+(k-i): a+k);
}
int repl_m(char* dst, int sdst, char* a,
char* p, int plen, int* next, char *b)
{char *pl, *aa, *ddst;
int con;
// ---------------------
// *p!='\0'
aa=a; ddst=dst; con=0;
W(1){pl=kmn(p, aa, next, plen); // 4+1
if(*pl==0) break; // sz=5
F( ; sdst>0 && aa<pl; --sdst)
*ddst++=*aa++;
F( pl=b; sdst>0 && (*ddst=*pl); --sdst, ++ddst, ++pl);
aa+=plen;
if(sdst<=0) break;
else ++con;
}
F(;sdst>0 && (*ddst++=*aa++); --sdst);
if(sdst<=0) *ddst=0;
// nel caso di errore
// ritorna la lunghezza della nuova linea in negativo
// altrimenti quante volte ha sostituito
R sdst>=0 ? con: (dst-ddst);
}
/*
function to replace all instances of a word in a string
Params: out - the output buffer (may not be the same as any input)
original - the original text
search - the sub string to seach for
replacement - the string to replace it with
returns: the number of replacements made
*/
// ritorna le volte che ha fatto la sostituzione
// se ritorna un numero negativo => errore
int replace_m(char* dst, int sdst, char* a, char* p, char *b)
{int next[1024], i;
if(dst==0||a==0||p==0||b==0||sdst<=1||a==dst||*p==0)
{m0:;
if(dst&&sdst>=1) *dst=0;
R -1;
}
if( ( i=init_kmn(next, 1024, p) )==-1 ) G m0;
R repl_m(dst, sdst, a, p, i, next, b);
}
int main(void)
{
char result[1024]; /* make the result buffer big */
char *sentence = "The cat sat on the mat.";
int N;
/* can we repalce cat with kitten ? */
N = replace_m(result, 1024, sentence, "cat", "kitten");
printf("%d instances %s\n", N, result);
/* can we repalce several ats with a longer string? */
N =replace_m(result, 1024, sentence, "at", "attywatwat");
printf("%d instances %s\n", N, result);
/* can we replace the spaces with the empty
string to run the words together ? */
N = replace_m(result, 1024, sentence, " ", "");
printf("%d instances, %s\n", N, result);
return 0;
}
-------------------------------
C:>replace1
1 instances The kitten sat on the mat.
3 instances The cattywatwat sattywatwat on the mattywatwat.
5 instances, Thecatsatonthemat.
.
- Follow-Ups:
- Re: sentence
- From: av
- Re: sentence
- References:
- sentence
- From: Sidhu
- Re: sentence
- From: Malcolm
- sentence
- Prev by Date: Re: sizeof
- Next by Date: Re: a little mistake
- Previous by thread: Re: sentence
- Next by thread: Re: sentence
- Index(es):
Relevant Pages
|