Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Novice question about string manipulation...

Status
Not open for further replies.

protectorfeelgood

Technical User
Nov 10, 2003
5
0
0
NO
Hi all,

Hopefully someone out there could help me with this question.

I've made a sub asking users to input a three letter sentence...
The output at the end should be a list of all letters used in the sentence - sorted alphabetically and a statement telling how many times the letter were used in total.

So far I've made it check for threee words, and somehow sort the sentence...but as you can see - there's still stuff missing.
Mostly in the sorting part of the script.

Please excuse any bad English, as it is not my native tongue.

Any help would be highly appreciated.

Here's my code so far:

Code:
sub WordSplit
dim strMessage,arrWords,strSentence,startagain,arrCounter

Do
startagain ="no"
strSentence = Trim(InputBox("Write a three word sentence"))
arrWords = Split(strSentence, " ")
arrCounter = UBound(arrWords)+1

If arrCounter <> 3 then
  alert &quot;Use three words only!&quot;
  startagain = &quot;yes&quot;
  Else
  if CheckForNumbers(strSentence) then	
    alert &quot;Don't use numbers!&quot;
    startagain = &quot;yes&quot;
    else
  End If
End If
Loop While startagain = &quot;yes&quot;
  		


dim intCounter,i,k,arrMatrix,strSpaces
intCounter = 1
i = 1

strSpaces = Replace(strSentence,&quot; &quot;,&quot;&quot;)
ReDim arrMatrix(len(strSpaces)-1)
for k=1 to len(strSpaces)
  arrMatrix(k-1)=mid(strSpaces,k,1)
next


do while not i > Ubound(arrMatrix) 
  if arrMatrix(i-1) = arrMatrix(i) then 
    intCounter = intCounter + 1 
    i = i + 1 
  else 
    strMessage = strMessage & intCounter & &quot; Instances of &quot; & arrMatrix(i-1) & vbCrLf
    i = i + 1 
    intCounter = 1 
  end if
loop

strMessage = strMessage & intCounter & &quot; Instances of &quot; & arrMatrix(i-1) & vbCrLf
alert (strSentence & vbCrLf & strMessage)

End Sub

function CheckForNumbers(string)
  dim i
  CheckForNumbers = false
  for i = 1 to len(string)
    if IsNumeric(mid(string,i,1)) then
      CheckForNumbers = true 
    end if
  next
end function
 
A simple way to count characters is this function
Code:
Function CountChars(myString As String) As Variant
    Dim Counts(255) As Integer
    Dim n           As Integer
    
    For n = 1 To Len(myString)
        Counts(Asc(Mid$(myString, n, 1))) = Counts(Asc(Mid$(myString, n, 1))) + 1
    Next n
    CountChars = Counts

End Function
And you would call it with something like
Code:
Dim Counts As Variant
Dim strMessage As String

Counts = CountChars ( Replace(&quot;The Counted Characters&quot;,&quot; &quot;, &quot;&quot;) )
For n = 0 to Ubound(Counts)
   If Counts(n) > 0 Then
      strMessage = strMessage & Counts(n) & &quot; Instances of &quot; & Chr$(n) & vbCrLf
   End If
Next n
Msgbox strMessage
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top