I want to search for a string and find the number of occurrences in a file using the vi editor.
You need the n flag. To count words use:
n
:%s/\i\+/&/gn
and a particular word:
:%s/the/&/gn
See count-items documentation section.
If you simply type in:
%s/pattern/pattern/g
then the status line will give you the number of matches in vi as well.