Counting patterns in a shell string

Hello,

I am writing a shell script and I need to find a way to
count the number of whitespaces in a string.

Eg:
NAME="Bob Hope"
I am looking for a way to count the number of whitespaces in this string. So a command that would take this string and return 1.
Or take
"First Middle Last" and return 2.
I know this could be achieved using an array to store the NAME and then searching through each element of the array and count the number of whitespaces. But I was hoping that there would be a simplier way.

Thank You

Don't worry. I found a solution.

NAME="Bob Hope"
spacecount=0
for((i=0; i<${#NAME};i++))
if test ${NAME:$i:1} = " "
then
let 'spacecount=spacesount+1'
fi
done

Just a simple loop

kevin80,
why don't you post the solution so if someone has a similar need in the future, he can just search the forum.

Try this:

NAME="Bob Hope"
let v_space_count=`echo $NAME|awk 'BEGIN { RS=" " } END {print NR }'`-1
echo $v_space_count

Regards,
Yeheya