Pascal Infinite while loop
I'm implementing a program in FreePascal in Win10(64-bit). The problem state:
'Given a string, replace all substring 'child'
with 'childhood'
'
or
'Replace all 'child'
with 'childhood'
'
I try this program
uses crt;
var s, newS : string;
tmp, tmp2, tmp3 : int64;
tmpstr, tmpstr2 : string;
step, step2, step3 : longint;
position : longint;
begin
clrscr;
write('Nhap xau : '); readln(s);
//main mechanism
while pos('child',s) <> 0 do begin
position := pos('child', s);
delete(s, pos('child',1), 5);
insert('childhood',s,position);
inc(position, 9);
newS := '';
for step:=position to length(s) do begin
newS := newS + s[step];
end;
s := newS;
end;
writeln(s);
readkey;
end.
You can see that this part:
inc(position, 9);
newS := '';
for step:=position to length(s) do begin
newS := newS + s[step];
end;
s := newS;
was used to try to cut off the while loop, but it doesn't work. Any idea?
Thanks a lot and have a good day! Thanks for reading this question thread! =)
infinite-loop freepascal
|
show 3 more comments
I'm implementing a program in FreePascal in Win10(64-bit). The problem state:
'Given a string, replace all substring 'child'
with 'childhood'
'
or
'Replace all 'child'
with 'childhood'
'
I try this program
uses crt;
var s, newS : string;
tmp, tmp2, tmp3 : int64;
tmpstr, tmpstr2 : string;
step, step2, step3 : longint;
position : longint;
begin
clrscr;
write('Nhap xau : '); readln(s);
//main mechanism
while pos('child',s) <> 0 do begin
position := pos('child', s);
delete(s, pos('child',1), 5);
insert('childhood',s,position);
inc(position, 9);
newS := '';
for step:=position to length(s) do begin
newS := newS + s[step];
end;
s := newS;
end;
writeln(s);
readkey;
end.
You can see that this part:
inc(position, 9);
newS := '';
for step:=position to length(s) do begin
newS := newS + s[step];
end;
s := newS;
was used to try to cut off the while loop, but it doesn't work. Any idea?
Thanks a lot and have a good day! Thanks for reading this question thread! =)
infinite-loop freepascal
1
Given the requirement is as you've stated, I suggest replacing your program with a call to StringReplace.
– Sertac Akyuz
Nov 23 '18 at 15:17
This is homework I guess. What your instructor is hoping is that you will debug your program to find out what is going wrong. Do you know how to debug? If not, there is no time like the present.
– David Heffernan
Nov 23 '18 at 15:20
1
I hope you are aware of the fact that the replacement'childhood'
contains the search text,'child'
and that this should have consequences for how and where you try to find your search text after you replaced it. Otherwise'child'
will become'childhood'
,'childhoodhood'
,'childhoodhoodhood'
, etc. ad infinitum. So do as @David says, and debug your program. It is an important skill and easy to learn.
– Rudy Velthuis
Nov 23 '18 at 19:29
FWIW, since the linedelete(s, pos('child',1), 5);
probably doesn't compile at all, it looks as if this is not exactly your code. Please always post the exact code (copy and paste) that gives you problems. Do not type it from memory or from view. Use copy and paste. How can we tell what is wrong with your code if we don't even see your exact code? The error could just as well be in the in the part you typed wrongly.
– Rudy Velthuis
Nov 23 '18 at 19:33
@Rudy_Velthius this is my exact code. I copy paste from notepad
– hungnguyen
Nov 24 '18 at 2:54
|
show 3 more comments
I'm implementing a program in FreePascal in Win10(64-bit). The problem state:
'Given a string, replace all substring 'child'
with 'childhood'
'
or
'Replace all 'child'
with 'childhood'
'
I try this program
uses crt;
var s, newS : string;
tmp, tmp2, tmp3 : int64;
tmpstr, tmpstr2 : string;
step, step2, step3 : longint;
position : longint;
begin
clrscr;
write('Nhap xau : '); readln(s);
//main mechanism
while pos('child',s) <> 0 do begin
position := pos('child', s);
delete(s, pos('child',1), 5);
insert('childhood',s,position);
inc(position, 9);
newS := '';
for step:=position to length(s) do begin
newS := newS + s[step];
end;
s := newS;
end;
writeln(s);
readkey;
end.
You can see that this part:
inc(position, 9);
newS := '';
for step:=position to length(s) do begin
newS := newS + s[step];
end;
s := newS;
was used to try to cut off the while loop, but it doesn't work. Any idea?
Thanks a lot and have a good day! Thanks for reading this question thread! =)
infinite-loop freepascal
I'm implementing a program in FreePascal in Win10(64-bit). The problem state:
'Given a string, replace all substring 'child'
with 'childhood'
'
or
'Replace all 'child'
with 'childhood'
'
I try this program
uses crt;
var s, newS : string;
tmp, tmp2, tmp3 : int64;
tmpstr, tmpstr2 : string;
step, step2, step3 : longint;
position : longint;
begin
clrscr;
write('Nhap xau : '); readln(s);
//main mechanism
while pos('child',s) <> 0 do begin
position := pos('child', s);
delete(s, pos('child',1), 5);
insert('childhood',s,position);
inc(position, 9);
newS := '';
for step:=position to length(s) do begin
newS := newS + s[step];
end;
s := newS;
end;
writeln(s);
readkey;
end.
You can see that this part:
inc(position, 9);
newS := '';
for step:=position to length(s) do begin
newS := newS + s[step];
end;
s := newS;
was used to try to cut off the while loop, but it doesn't work. Any idea?
Thanks a lot and have a good day! Thanks for reading this question thread! =)
infinite-loop freepascal
infinite-loop freepascal
asked Nov 23 '18 at 14:55
hungnguyenhungnguyen
378
378
1
Given the requirement is as you've stated, I suggest replacing your program with a call to StringReplace.
– Sertac Akyuz
Nov 23 '18 at 15:17
This is homework I guess. What your instructor is hoping is that you will debug your program to find out what is going wrong. Do you know how to debug? If not, there is no time like the present.
– David Heffernan
Nov 23 '18 at 15:20
1
I hope you are aware of the fact that the replacement'childhood'
contains the search text,'child'
and that this should have consequences for how and where you try to find your search text after you replaced it. Otherwise'child'
will become'childhood'
,'childhoodhood'
,'childhoodhoodhood'
, etc. ad infinitum. So do as @David says, and debug your program. It is an important skill and easy to learn.
– Rudy Velthuis
Nov 23 '18 at 19:29
FWIW, since the linedelete(s, pos('child',1), 5);
probably doesn't compile at all, it looks as if this is not exactly your code. Please always post the exact code (copy and paste) that gives you problems. Do not type it from memory or from view. Use copy and paste. How can we tell what is wrong with your code if we don't even see your exact code? The error could just as well be in the in the part you typed wrongly.
– Rudy Velthuis
Nov 23 '18 at 19:33
@Rudy_Velthius this is my exact code. I copy paste from notepad
– hungnguyen
Nov 24 '18 at 2:54
|
show 3 more comments
1
Given the requirement is as you've stated, I suggest replacing your program with a call to StringReplace.
– Sertac Akyuz
Nov 23 '18 at 15:17
This is homework I guess. What your instructor is hoping is that you will debug your program to find out what is going wrong. Do you know how to debug? If not, there is no time like the present.
– David Heffernan
Nov 23 '18 at 15:20
1
I hope you are aware of the fact that the replacement'childhood'
contains the search text,'child'
and that this should have consequences for how and where you try to find your search text after you replaced it. Otherwise'child'
will become'childhood'
,'childhoodhood'
,'childhoodhoodhood'
, etc. ad infinitum. So do as @David says, and debug your program. It is an important skill and easy to learn.
– Rudy Velthuis
Nov 23 '18 at 19:29
FWIW, since the linedelete(s, pos('child',1), 5);
probably doesn't compile at all, it looks as if this is not exactly your code. Please always post the exact code (copy and paste) that gives you problems. Do not type it from memory or from view. Use copy and paste. How can we tell what is wrong with your code if we don't even see your exact code? The error could just as well be in the in the part you typed wrongly.
– Rudy Velthuis
Nov 23 '18 at 19:33
@Rudy_Velthius this is my exact code. I copy paste from notepad
– hungnguyen
Nov 24 '18 at 2:54
1
1
Given the requirement is as you've stated, I suggest replacing your program with a call to StringReplace.
– Sertac Akyuz
Nov 23 '18 at 15:17
Given the requirement is as you've stated, I suggest replacing your program with a call to StringReplace.
– Sertac Akyuz
Nov 23 '18 at 15:17
This is homework I guess. What your instructor is hoping is that you will debug your program to find out what is going wrong. Do you know how to debug? If not, there is no time like the present.
– David Heffernan
Nov 23 '18 at 15:20
This is homework I guess. What your instructor is hoping is that you will debug your program to find out what is going wrong. Do you know how to debug? If not, there is no time like the present.
– David Heffernan
Nov 23 '18 at 15:20
1
1
I hope you are aware of the fact that the replacement
'childhood'
contains the search text, 'child'
and that this should have consequences for how and where you try to find your search text after you replaced it. Otherwise 'child'
will become 'childhood'
, 'childhoodhood'
, 'childhoodhoodhood'
, etc. ad infinitum. So do as @David says, and debug your program. It is an important skill and easy to learn.– Rudy Velthuis
Nov 23 '18 at 19:29
I hope you are aware of the fact that the replacement
'childhood'
contains the search text, 'child'
and that this should have consequences for how and where you try to find your search text after you replaced it. Otherwise 'child'
will become 'childhood'
, 'childhoodhood'
, 'childhoodhoodhood'
, etc. ad infinitum. So do as @David says, and debug your program. It is an important skill and easy to learn.– Rudy Velthuis
Nov 23 '18 at 19:29
FWIW, since the line
delete(s, pos('child',1), 5);
probably doesn't compile at all, it looks as if this is not exactly your code. Please always post the exact code (copy and paste) that gives you problems. Do not type it from memory or from view. Use copy and paste. How can we tell what is wrong with your code if we don't even see your exact code? The error could just as well be in the in the part you typed wrongly.– Rudy Velthuis
Nov 23 '18 at 19:33
FWIW, since the line
delete(s, pos('child',1), 5);
probably doesn't compile at all, it looks as if this is not exactly your code. Please always post the exact code (copy and paste) that gives you problems. Do not type it from memory or from view. Use copy and paste. How can we tell what is wrong with your code if we don't even see your exact code? The error could just as well be in the in the part you typed wrongly.– Rudy Velthuis
Nov 23 '18 at 19:33
@Rudy_Velthius this is my exact code. I copy paste from notepad
– hungnguyen
Nov 24 '18 at 2:54
@Rudy_Velthius this is my exact code. I copy paste from notepad
– hungnguyen
Nov 24 '18 at 2:54
|
show 3 more comments
1 Answer
1
active
oldest
votes
This is just one of the possibilities, not optimized but perhaps the easiest to understand:
oldstr := 'original child string';
newstr := '';
while oldstr<>'' do begin
// analyze the string from left to right
if copy(oldstr, 1 5)='child' then begin
// match found, perform substitution
newstr := newstr+'childhood';
delete(oldstr, 1, 5)
end else begin
// does not match, go ahead to the next possibility
newstr := newstr+oldstr[1];
delete(oldstr, 1, 1)
end
end
// now newstr is the desired result
The gotcha here was to not analyze again what was added in a previous step (child->childhood). The algorithm above (warning, I've not tested it) ensures that any single char from the original string is checked only once.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53448902%2fpascal-infinite-while-loop%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
This is just one of the possibilities, not optimized but perhaps the easiest to understand:
oldstr := 'original child string';
newstr := '';
while oldstr<>'' do begin
// analyze the string from left to right
if copy(oldstr, 1 5)='child' then begin
// match found, perform substitution
newstr := newstr+'childhood';
delete(oldstr, 1, 5)
end else begin
// does not match, go ahead to the next possibility
newstr := newstr+oldstr[1];
delete(oldstr, 1, 1)
end
end
// now newstr is the desired result
The gotcha here was to not analyze again what was added in a previous step (child->childhood). The algorithm above (warning, I've not tested it) ensures that any single char from the original string is checked only once.
add a comment |
This is just one of the possibilities, not optimized but perhaps the easiest to understand:
oldstr := 'original child string';
newstr := '';
while oldstr<>'' do begin
// analyze the string from left to right
if copy(oldstr, 1 5)='child' then begin
// match found, perform substitution
newstr := newstr+'childhood';
delete(oldstr, 1, 5)
end else begin
// does not match, go ahead to the next possibility
newstr := newstr+oldstr[1];
delete(oldstr, 1, 1)
end
end
// now newstr is the desired result
The gotcha here was to not analyze again what was added in a previous step (child->childhood). The algorithm above (warning, I've not tested it) ensures that any single char from the original string is checked only once.
add a comment |
This is just one of the possibilities, not optimized but perhaps the easiest to understand:
oldstr := 'original child string';
newstr := '';
while oldstr<>'' do begin
// analyze the string from left to right
if copy(oldstr, 1 5)='child' then begin
// match found, perform substitution
newstr := newstr+'childhood';
delete(oldstr, 1, 5)
end else begin
// does not match, go ahead to the next possibility
newstr := newstr+oldstr[1];
delete(oldstr, 1, 1)
end
end
// now newstr is the desired result
The gotcha here was to not analyze again what was added in a previous step (child->childhood). The algorithm above (warning, I've not tested it) ensures that any single char from the original string is checked only once.
This is just one of the possibilities, not optimized but perhaps the easiest to understand:
oldstr := 'original child string';
newstr := '';
while oldstr<>'' do begin
// analyze the string from left to right
if copy(oldstr, 1 5)='child' then begin
// match found, perform substitution
newstr := newstr+'childhood';
delete(oldstr, 1, 5)
end else begin
// does not match, go ahead to the next possibility
newstr := newstr+oldstr[1];
delete(oldstr, 1, 1)
end
end
// now newstr is the desired result
The gotcha here was to not analyze again what was added in a previous step (child->childhood). The algorithm above (warning, I've not tested it) ensures that any single char from the original string is checked only once.
answered Dec 23 '18 at 10:43
linuxfanlinuxfan
2,0982720
2,0982720
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53448902%2fpascal-infinite-while-loop%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Given the requirement is as you've stated, I suggest replacing your program with a call to StringReplace.
– Sertac Akyuz
Nov 23 '18 at 15:17
This is homework I guess. What your instructor is hoping is that you will debug your program to find out what is going wrong. Do you know how to debug? If not, there is no time like the present.
– David Heffernan
Nov 23 '18 at 15:20
1
I hope you are aware of the fact that the replacement
'childhood'
contains the search text,'child'
and that this should have consequences for how and where you try to find your search text after you replaced it. Otherwise'child'
will become'childhood'
,'childhoodhood'
,'childhoodhoodhood'
, etc. ad infinitum. So do as @David says, and debug your program. It is an important skill and easy to learn.– Rudy Velthuis
Nov 23 '18 at 19:29
FWIW, since the line
delete(s, pos('child',1), 5);
probably doesn't compile at all, it looks as if this is not exactly your code. Please always post the exact code (copy and paste) that gives you problems. Do not type it from memory or from view. Use copy and paste. How can we tell what is wrong with your code if we don't even see your exact code? The error could just as well be in the in the part you typed wrongly.– Rudy Velthuis
Nov 23 '18 at 19:33
@Rudy_Velthius this is my exact code. I copy paste from notepad
– hungnguyen
Nov 24 '18 at 2:54