Re: sentence



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.
.



Relevant Pages

  • Re: socket communication: send & receive doesnt work right
    ... So I don't want to send a string as bytes. ... public void send_doubles(double vals, int len) throws ... // send a short acknowledgement to the server ... char *result; ...
    (microsoft.public.win32.programmer.networks)
  • Re: Replace strings in a text file and get the number of replacementsmade
    ... It might be faster to use sb.Replace(search, replacement, i, search.Length) ... If you simply call StringBuilder.Replace(string, string, int, int) instead of having your own AreEqualmethod followed by a call to Removeand Insert, the performance should be practically identical, but you wouldn't get any information about how many replacements occurred. ... You could do a little hack by searching for the first character that differs between the search and replacement strings, and then bumping a counter after each call to StringBuilder.Replacebased on whether the character at the same offset within the current StringBuilder has changed. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: [PATCH] markers: modpost
    ... pointers to the name/format string pairs. ... The same can then be done with modules using the __markers section. ... +static void read_markers(const char *fname) ... int main ...
    (Linux-Kernel)
  • Re: [PATCH] markers: modpost
    ... This adds some new magic in the MODPOST phase for CONFIG_MARKERS. ... will be a neighbor of its format string. ... +static void read_markers(const char *fname) ... int main ...
    (Linux-Kernel)
  • Re: Is this code totaly a shit?
    ... | void UppStrg(char *Low, char *Upp, int cnt); ... whitespace-delimited string. ... You're also assuming that the representations of the characters ...
    (comp.lang.c)