5. To count the number of vowels in a given string.

echo "enter the string"
read string
len=`expr length $string`
echo "length of $string is $len"

vowel=0
consonant=0
i=1
while [ $i -le $len ]
do
    ch=`echo $string | cut -c $i`

    case $ch in
    [aeiouAEIOU])     vowel=`expr $vowel + 1`;;
    *)             consonant=`expr $consonant + 1`;;
    esac
i=`expr $i + 1`
done

echo "total vowels in a string = $vowel"
echo "total consonants in a string = $consonant"

No comments

Powered by Blogger.