Count occurrences of a substring: Difference between revisions

Content added Content deleted
(Add Batch File)
(My first ever edit in an Ubuntu Virtual Machine)
Line 1,827: Line 1,827:
2 occurence(s) of cat inside catapultcatalog</pre>
2 occurence(s) of cat inside catapultcatalog</pre>


=={{header|UNIX Shell}}==
{{works with|Bash}}
<lang bash>#!/bin/bash

function countString(){
input=$1
cnt=0

until [ "${input/$2/}" == "$input" ]; do
input=${input/$2/}
let cnt+=1
done
echo $cnt

}

countString "the three truths" "th"
countString "ababababab" "abab"</lang>
{{Out}}
<pre>3
2</pre>


=={{header|VBA}}==
=={{header|VBA}}==