Solved how do i make a code that finds the positions of words in a

June 24, 2016 at 07:03:59
Specs: Windows 7
i am having trouble making a code that when its given a sentance to work with and you also give it a word if the words is in the sentance it will tell you all the postoins and occurences of the word

See More: how do i make a code that finds the positions of words in a

Report •

June 24, 2016 at 14:33:50
Read about regular expression operations:

Report •

June 26, 2016 at 04:39:21
::====== script starts here ===============
:: wordpos.bat 2016-06-25 18:10:55.96
@echo off & setLocal enableDELAYedeXpansioN

set/p WORD= word?
if not defined WORD cls & echo done & goto :eof

set/p SENTENCE=<myfile

echo !SENTENCE! | find /i "!WORD!" > nul
if errorlevel 1 echo !WORD! not there && goto :eof

call :main !SENTENCE!
goto :eof

set/a N=0 & set POS=
for %%a in (%*) do (
if %1 equ !WORD! set POS=!POS! !N!
if '%2' neq '' SHIFT && set/a N+=1 && goto :loop
echo !WORD! at !POS!
goto :eof
::====== script ends here =================


M2 Get custom script or take private lessons

Report •

June 28, 2016 at 00:53:30
✔ Best Answer
Here is a vba solution, you can specify the word you want to find in Range A1 and the sentence in A2

1       Hello

2       Good morning and Hello all Hello

Here we are looking for the word "Hello" within the sentence "Good morning and Hello all Hello"

The positions for the word are 18 and 28 and it occurs two times

the code with output

Word: "Hello" found in position 18
Word: "Hello" found in position 28
Word: "Hello found 2 times

here is the badger

Sub FindWord()
    Dim iWhat As String
    Dim iWhere As String
    Dim iPos As Integer
    Dim iFound As Integer
    Dim iCounter As Integer
    iWhat = Range("A1")
    iWhere = Range("A2")
    iPos = 1
    iCounter = 0
    Do Until iPos < 0
        iPos = InStr(iPos, iWhere, iWhat)
        iPos = iPos + 1
        If iPos - 1 <= 0 Then Exit Do
        iCounter = iCounter + 1
        Debug.Print "Word: " & """" & iWhat & """" & " found in position " & iPos - 1
        Debug.Print "Word: " & """" & iWhat & " found " & iCounter & "  times"
End Sub

This can also be modified so that you can pass it the two variables Word and sentence

message edited by AlwaysWillingToLearn

Report •

Related Solutions

June 28, 2016 at 02:11:57
Methinks OP has flown to greener pastures. But I tend toward the regexp solution, as ijack hinted, due to delimiters other than space. Words tend to include only the characters A-z, and the single-quote for apostrophe. But words in sentences can have commas, exclams, colons, semicolons, single and double quotes, and dots as delimiters, and of course, the single-quotes would have to be filtered for apostrophes
versus quotation. So a string would have to be broken up on any instance of any character other than A-z, and a lot of handling for single-quotes vs apostrophes. If you replace all the char.s other than A-z with something, then you can work from there. But: "It's been done, done, and done again". No need to re-invent (esp. for probably homework problem ha ha!)

Report •

June 28, 2016 at 03:00:35
It's official. We're having an epidemic of 'OP flew the coop'


M2 Get custom script or take private lessons

Report •

Ask Question