I am writing a program that reads a string then searches for certain keywords, like “cat”, and replaces it with “dog”. I am just unsure as how to start it. What code will I have to use?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
For 8-bit characters it’s broadly like this, there are many ways to implement it:
Set
sito point to the first character of the string.mov al,[si]repnz scasbto find the first match of the first character.Store the address somewhere.
Set
dito point to the first character of the replacement string ('dog'in this case).Set
cx/ecx/rcxto string length.repz cmpsbCheck that
cx/ecx/rcxis zero and last characters match.If yes, it’s a match, so copy
'dog'to the address stored withrep movsb(set pointerssianddifirst). Do note that this approach only works if the replace string is no longer than the original string. If it’s longer, you may need to reserve a new block of memory to avoid a buffer overflow. If it’s not a match, backtracksito the stored address, incrementsiby 1 (by 2 for 16-bit characters), and jump to 2. (mov al,[si]). You need to also check here when you have reached the end of the string.Ready. Or, if you want to replace all, as in sed
s/cat/dog/g, loop from 1, set pointer (si) first (depending on how you want your regex engine to work).For UTF-8 (16-bit characters) replace the following:
scasb->scasw,cmpsb->cmpsw,movsb->movsw,al->ax.For 32-bit code, replace all references to
siwithesiand all references todiwithedi.For 64-bit code, replace all references to
siwithrsiand all references todiwithrdi.