473,385 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

sentence

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

Jun 17 '06 #1
10 2057

Sidhu wrote:
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.

Jun 17 '06 #2
Sidhu wrote:

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.

--
"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews
Jun 17 '06 #3
"Sidhu" <si********@gmail.com> writes:
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jun 17 '06 #4
"Sidhu" <si********@gmail.com> 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)

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.

--
Buy my book 12 Common Atheist Arguments (refuted)
$1.25 download or $7.20 paper, available www.lulu.com/bgy1mm
Jun 18 '06 #5
av
On Sun, 18 Jun 2006 08:23:35 +0100, "Malcolm"
<re*******@btinternet.com> wrote:
"Sidhu" <si********@gmail.com> 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.
Jun 19 '06 #6
av
On Mon, 19 Jun 2006 07:07:44 +0200, av <av@ala.a> wrote:
On Sun, 18 Jun 2006 08:23:35 +0100, "Malcolm"
<re*******@btinternet.com> wrote:
"Sidhu" <si********@gmail.com> 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 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);
}


Jun 19 '06 #7
av wrote:
On Mon, 19 Jun 2006 07:07:44 +0200, av <av@ala.a> wrote:

On Sun, 18 Jun 2006 08:23:35 +0100, "Malcolm"
<re*******@btinternet.com> wrote:

"Sidhu" <si********@gmail.com> 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

-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

--
---------------------------------------------------------------------------
Home of Martin Jørgensen - http://www.martinjoergensen.dk
Jun 19 '06 #8
Martin Jørgensen wrote:
av wrote:
On Mon, 19 Jun 2006 07:07:44 +0200, av <av@ala.a> wrote:
.... snip ...

#include <stdio.h>

#define F for


-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
Jun 19 '06 #9
av
On Sun, 18 Jun 2006 08:23:35 +0100, "Malcolm"
<re*******@btinternet.com> wrote:
"Sidhu" <si********@gmail.com> 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
*/


#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
Jun 19 '06 #10
Martin Jørgensen wrote:

av wrote:

#include <stdio.h>

#define F for
#define R return
#define W while
#define RE realloc
#define B break
#define G goto
#define uns unsigned -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.
if(*pl==0) break; if(sdst<=0) break;


--
pete
Jun 20 '06 #11

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

Similar topics

1
by: Christian Buck | last post by:
Hi, i'm writing a regexp that matches complete sentences in a german text, and correctly ignores abbrevations. Here is a very simplified version of it, as soon as it works i could post the...
6
by: mike | last post by:
Hello, I am trying to write some code to parse a sentence and hyperlink just the words in it. I used Aaron's code from an earlier question as a start. So far, all the code does below is...
8
by: Scott | last post by:
I would like to automatically change the first letter to upper case and keep the rest intact of each sentence on a control of a form, i.e., i am going to school. see you in the afternoon. -I am...
3
by: dalearyous | last post by:
ok basically i need to write a program that will replace normal words in a sentence with pirate words. the trick is it needs to be able to take two word phrases. i went about this two different ways:...
4
by: wohast | last post by:
Hi, I'm stuck at the very beginning of my current assignment. I just need a little help getting started, and then I know how to do everything else. ask user: "Please enter your name followed by...
12
by: jackson.rayne | last post by:
Hello, I am a javascript newbie and I'm stick at one place. I have a requirement where I will get a sentence in a variable example var v1 ="This is a sentence"
3
by: dmalhotr2001 | last post by:
Hi, For string extraction function in vb, if I feed in a paragraph, how do I extract the first sentence of that paragraph. Thanks :D
19
by: fellya | last post by:
Hi, i don't have enough experience in writing codes in Python but now i'm trying to see how i can start using Python. I've tried to write a simple program that can display a sentence. now my...
1
by: fellya | last post by:
Hi, i don't have enough experience in writing codes in Python but now i'm trying to see how i can start using Python. I've tried to write a simple program that can display a sentence. now my...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.