sentence

S

spibou

Sidhu said:
Hoe to relplace the word in sentence? Can any one send program?

Let me guess. You are so busy doing the rest of your homework that
you couldn't even find time to give a proper explanation.

But anyway if you provide the email of this Hoe chap then someone
might be willing to send him a programme.
 
C

CBFalconer

Sidhu said:
Hoe to relplace the word in sentence? Can any one send program?

s/relplace/replace/
s/hoe/how/

Use as a script to sed. However this is OT, and not a C question.
 
K

Keith Thompson

Sidhu said:
Hoe to relplace the word in sentence? Can any one send program?

Let me guess. You want to use it to replace occurrences of "relplace"
by "replace". Sounds like a good idea.
 
M

Malcolm

Sidhu said:
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)

The way to do is is to call strstr() to find where search appears in
original.
ststr returns a pointer. So copy characters from original to the output,
until
you reach that pointer. Then copy the replacement text. Then increment the
pointer returned by strstr by the length of the search string, to jump over
the text you want to replace. Repeat until you find no more occurrences of
the search string. Finally, copy the remaining characters over to the output
buffer, not forgetting the terminating NUL.

You'll need to test it.

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(result, sentence, "cat", "kitten");
printf("%d instances %s\n", N, result);

/* can we repalce several ats with a longer string? *?
N = replace(result, sentence, "at", "attywatwat");
printf("%d instances %s\n", result);

/* can we replace the spaces with the empty string to run the words
together ? */
N = replace(result, sentence, " ", "");
printf("%d instances, "%s\n", N, result);

return 0;
}

If you have any problems getting the function to work, post your attempt.
 
A

av

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<snext; )
{W(j>=0 && p!=p[j] ) j=next[j]; // i==1022
++i; ++j; // i==1023
next=(p==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;
}
 
A

av

#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 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);

R sdst>0 ? con: (dst-ddst);
 
?

=?ISO-8859-1?Q?Martin_J=F8rgensen?=

-snip-

That's too easy... Next time try this:

#define Q for
#define Z return
#define J while
#define DF realloc
#define I break
#define ER goto
#define BVS unsigned

And then try to read the code, LOL :)


Best regards
Martin Jørgensen
 
C

CBFalconer

Martin said:
-snip-

That's too easy... Next time try this:

#define Q for
.... snip ...

--

+-------------------+ .:\:\:/:/:.
| PLEASE DO NOT F :.:\:\:/:/:.:
| FEED THE TROLLS | :=.' - - '.=:
| | '=(\ 9 9 /)='
| Thank you, | ( (_) )
| Management | /`-vvv-'\
+-------------------+ / \
| | @@@ / /|,,,,,|\ \
| | @@@ /_// /^\ \\_\
@x@@x@ | | |/ WW( ( ) )WW
\||||/ | | \| __\,,\ /,,/__
\||/ | | | jgs (______Y______)
/\/\/\/\/\/\/\/\//\/\\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
==============================================================

fix (vb.): 1. to paper over, obscure, hide from public view; 2.
to work around, in a way that produces unintended consequences
that are worse than the original problem. Usage: "Windows ME
fixes many of the shortcomings of Windows 98 SE". - Hutchison
 
A

av

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
*/

#include <stdio.h>

#define F for
#define P printf
#define R return
#define W while
#define G goto
#define uns unsigned

int init_kmn(int* next, int snext, char* p);

char* kmn(char* p, char* a, int* next, int i);

int repl(char* dst, int sdst, char* a,
char* p, int plen, int* next, char *b);

/*
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||a==dst||*p==0||sdst<=1)
{m0:;
if(dst && sdst>=1) *dst=0;
R -1;
}
if( ( i=init_kmn(next, 1024, p) )==-1 ) G m0;
R repl(dst, sdst, a, p, i, next, b);
}

/* C:> prog
1 instances The kitten sat on the mat.
3 instances The cattywatwat sattywatwat on the mattywatwat.
5 instances, Thecatsatonthemat. */

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;
}

-------------------------------------
; nasmw -f obj this_file.asm
; bcc32 file.c this_file.obj

section _DATA public align=4 class=DATA use32
global _init_kmn , _kmn , _repl
section _TEXT public align=1 class=CODE use32

; int init_kmn(int* next, int snext, char* p)
_init_kmn:
push ecx
push edx
push esi
push edi
%define @next [esp+20]
%define @snext [esp+24]
%define @p [esp+28]
mov edx, @next
mov ecx, @p
mov esi, 0
mov edi, -1
mov dword[edx], -1
mov al, [ecx]
jmp short .c5
..ee:
mov eax, -1
jmp short .cf
..c0:
cmp al, [ecx+edi]
je .c2
mov edi, [edx+4*edi]
..c1:
cmp edi, 0
jge .c0
..c2:
inc esi
inc edi
mov al, [ecx+esi]
cmp al, [ecx+edi]
jne .c3
push dword[edx+4*edi]
jmp short .c4
..c3:
push edi
..c4:
pop dword[edx+4*esi]
..c5:
dec dword @snext
jle .ee
cmp al, 0
jne .c1
..c9:
mov eax, esi
..cf:
%undef @next
%undef @snext
%undef @p
pop edi
pop esi
pop edx
pop ecx
ret

; char* kmn(char* p, char* a, int* next, int i)
_kmn:
push ecx
push edx
push edi
push ebp
%define @p [esp+20]
%define @a [esp+24]
%define @next [esp+28]
%define @i [esp+32]
mov ecx, @p
mov ebp, @a
mov edx, @next
mov edi, 0
jmp short .c9
..c0:
cmp al, [ecx+edi]
je .c2
mov edi, [edx+4*edi]
..c1:
cmp edi, 0
jge .c0
..c2:
inc edi
inc ebp
..c9:
cmp edi, @i
jge .ca
mov al, [ebp]
cmp al, 0
jne .c1
..ca:
mov eax, ebp
cmp edi, @i
jne .cf
sub eax, @i
..cf:
%undef @p
%undef @a
%undef @next
%undef @i
pop ebp
pop edi
pop edx
pop ecx
ret

; int repl(char* dst, int sdst, char* a,
; char* p, int plen, int* next, char * b)
_repl:
push ebx
push ecx
push edx
push esi
push edi
push ebp
push esi
%define @dst [esp+32]
%define @sdst [esp+36]
%define @a [esp+40]
%define @p [esp+44]
%define @plen [esp+48]
%define @next [esp+52]
%define @b [esp+56]
mov dword[esp], 0
mov esi, @a
mov edx, @next
mov ebp, 0
mov ecx, @p
mov edi, @dst
jmp short .a3
..a:
cmp ebp, @plen
jl .a3
mov eax, @plen
sub edi, @plen
add dword @sdst, eax
mov eax, @b
cmp dword @sdst, 0
jmp short .a1
..a0:
inc eax
inc edi
dec dword @sdst
..a1:
jle .a2
mov bl, [eax]
mov [edi], bl
cmp bl, 0
jne .a0
..a2:
mov ebp, 0
inc dword[esp]
..a3:
mov al, [esi]
mov [edi], al
cmp al, 0
jne .c1
jmp short .ca
..c0:
cmp al, [ecx+ebp]
je .c2
mov ebp, [edx+4*ebp]
..c1:
cmp ebp, 0
jge .c0
..c2:
inc esi
inc edi
inc ebp
dec dword @sdst
jge .a
..ca:
mov eax, [esp]
cmp dword @sdst, 0
jg .cf
mov byte[edi], 0
mov eax, -1
..cf:
%undef @dst
%undef @sdst
%undef @a
%undef @p
%undef @plen
%undef @next
%undef @b
pop esi
pop ebp
pop edi
pop esi
pop edx
pop ecx
pop ebx
ret
 
P

pete

Martin said:
av wrote:
-snip-

That's too easy...

Not even av really likes those macros.
He's never been able to post code that uses them consistently
and he's been posting those macros for years.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,184
Messages
2,570,973
Members
47,529
Latest member
JaclynShum

Latest Threads

Top