DEV Community

Fahad Vahora
Fahad Vahora

Posted on

Linux Practical Set B Solution – Vowels Count & Case Conversion

In this article, we will solve VNSGU TYBCA Semester 5 Linux (UNIX) Practical – Set B using shell scripting.

We will learn how to:

  • Count the number of vowels in a string
  • Convert a string into uppercase
  • Convert a string into lowercase
  • Perform the same task using different Linux/UNIX approaches

📌 Practical Question

Write a script to count the number of vowels in the entered string and convert the string into upper case or lower case.


🧠 Concepts Used

This practical covers:

  • read
  • Variables
  • String length
  • for loop
  • if condition
  • case
  • tr
  • Bash parameter expansion
  • String manipulation

✅ Solution 1: Using for Loop

This is one of the best approaches for understanding the logic behind vowel counting.

Shell Script

#!/bin/bash

echo "Enter a string:"
read str

count=0

for ((i=0; i<${#str}; i++))
do
    ch=${str:$i:1}

    case "$ch" in
        a|e|i|o|u|A|E|I|O|U)
            count=$((count + 1))
            ;;
    esac
done

echo "Number of vowels: $count"

echo "Uppercase: ${str^^}"
echo "Lowercase: ${str,,}"
Enter fullscreen mode Exit fullscreen mode

Example Output

Enter a string:
Hello Linux

Number of vowels: 4
Uppercase: HELLO LINUX
Lowercase: hello linux
Enter fullscreen mode Exit fullscreen mode

🔍 Understanding the Script

Step 1: Take String Input

read str
Enter fullscreen mode Exit fullscreen mode

The entered string is stored in the variable str.


Step 2: Initialize Counter

count=0
Enter fullscreen mode Exit fullscreen mode

Initially, the number of vowels is zero.

Whenever a vowel is found, we increase the counter:

count=$((count + 1))
Enter fullscreen mode Exit fullscreen mode

Step 3: Find String Length

${#str}
Enter fullscreen mode Exit fullscreen mode

This gives the number of characters in the string.

For example:

Hello
Enter fullscreen mode Exit fullscreen mode

has a length of:

5
Enter fullscreen mode Exit fullscreen mode

Step 4: Get One Character

ch=${str:$i:1}
Enter fullscreen mode Exit fullscreen mode

This extracts one character from the string.

For example, if:

str = Hello
Enter fullscreen mode Exit fullscreen mode

then the loop extracts:

H
e
l
l
o
Enter fullscreen mode Exit fullscreen mode

one by one.


Step 5: Check for Vowels

case "$ch" in
    a|e|i|o|u|A|E|I|O|U)
        count=$((count + 1))
        ;;
esac
Enter fullscreen mode Exit fullscreen mode

If the character is one of:

a e i o u
A E I O U
Enter fullscreen mode Exit fullscreen mode

the counter is increased.


Step 6: Convert to Uppercase

${str^^}
Enter fullscreen mode Exit fullscreen mode

This converts the string into uppercase.

Example:

hello linux
Enter fullscreen mode Exit fullscreen mode

becomes:

HELLO LINUX
Enter fullscreen mode Exit fullscreen mode

Step 7: Convert to Lowercase

${str,,}
Enter fullscreen mode Exit fullscreen mode

This converts the string into lowercase.

Example:

HELLO LINUX
Enter fullscreen mode Exit fullscreen mode

becomes:

hello linux
Enter fullscreen mode Exit fullscreen mode

Note: ${str^^} and ${str,,} are Bash parameter-expansion features and are not available in very old shells.


✅ Solution 2: Using tr

Linux provides the tr command for character translation.

We can use it to convert the string into uppercase/lowercase and also count vowels.

Shell Script

#!/bin/bash

echo "Enter a string:"
read str

lower=$(echo "$str" | tr '[:upper:]' '[:lower:]')

count=$(echo "$lower" | tr -cd 'aeiou' | wc -c)

upper=$(echo "$str" | tr '[:lower:]' '[:upper:]')

echo "Number of vowels: $count"
echo "Uppercase: $upper"
echo "Lowercase: $lower"
Enter fullscreen mode Exit fullscreen mode

Example Output

Enter a string:
Hello Linux

Number of vowels: 4
Uppercase: HELLO LINUX
Lowercase: hello linux
Enter fullscreen mode Exit fullscreen mode

🔍 How tr Works

Convert to Lowercase

tr '[:upper:]' '[:lower:]'
Enter fullscreen mode Exit fullscreen mode

Converts uppercase letters into lowercase.


Convert to Uppercase

tr '[:lower:]' '[:upper:]'
Enter fullscreen mode Exit fullscreen mode

Converts lowercase letters into uppercase.


Remove Everything Except Vowels

tr -cd 'aeiou'
Enter fullscreen mode Exit fullscreen mode

Here:

  • -c → complement
  • -d → delete

So characters other than a, e, i, o, u are removed.

For:

Hello Linux
Enter fullscreen mode Exit fullscreen mode

the result becomes approximately:

eoiu
Enter fullscreen mode Exit fullscreen mode

Then:

wc -c
Enter fullscreen mode Exit fullscreen mode

counts the remaining characters.

Therefore:

count=$(echo "$lower" | tr -cd 'aeiou' | wc -c)
Enter fullscreen mode Exit fullscreen mode

gives the total number of vowels.


✅ Solution 3: Using grep

Another approach is to use grep to identify vowels.

Shell Script

#!/bin/bash

echo "Enter a string:"
read str

count=$(echo "$str" | grep -o -i '[aeiou]' | wc -l)

upper=$(echo "$str" | tr '[:lower:]' '[:upper:]')
lower=$(echo "$str" | tr '[:upper:]' '[:lower:]')

echo "Number of vowels: $count"
echo "Uppercase: $upper"
echo "Lowercase: $lower"
Enter fullscreen mode Exit fullscreen mode

Example Output

Enter a string:
Welcome to Linux

Number of vowels: 6
Uppercase: WELCOME TO LINUX
Lowercase: welcome to linux
Enter fullscreen mode Exit fullscreen mode

📊 Comparing the Solutions

Method Vowel Counting Case Conversion Difficulty
for + case Bash ⭐ Easy
tr + wc tr ⭐⭐
grep + wc tr ⭐⭐

Which solution should you use?

For a practical examination, I recommend the for loop + case solution if you want to demonstrate your shell scripting logic.

If the examiner expects Linux command usage, the tr solution is a very good option.


📝 Quick Revision

Count vowels using loop

case "$ch" in
    a|e|i|o|u|A|E|I|O|U)
        count=$((count + 1))
        ;;
esac
Enter fullscreen mode Exit fullscreen mode

Uppercase

${str^^}
Enter fullscreen mode Exit fullscreen mode

Lowercase

${str,,}
Enter fullscreen mode Exit fullscreen mode

Uppercase using tr

echo "$str" | tr '[:lower:]' '[:upper:]'
Enter fullscreen mode Exit fullscreen mode

Lowercase using tr

echo "$str" | tr '[:upper:]' '[:lower:]'
Enter fullscreen mode Exit fullscreen mode

Count vowels using tr

echo "$str" | tr -cd 'aeiou' | wc -c
Enter fullscreen mode Exit fullscreen mode

🎯 Practical Exam Tips

While writing this program in the practical exam, remember:

  1. Initialize the vowel counter with 0.
  2. Check both uppercase and lowercase vowels.
  3. Use quotes around string variables when passing them to commands.
  4. Understand how ${#str} gives the string length.
  5. Remember that ${str^^} and ${str,,} are Bash-specific features.
  6. If you want a more portable command-based approach, use tr.

📚 Related Linux Practical Sets

This solution is part of the VNSGU TYBCA Sem 5 Linux (UNIX) Practical – OCT/Nov 2025 solution series.

  • Set A – Simple Interest & Compound Interest
  • Set B – Vowels Count & Case Conversion
  • Set C – Palindrome String
  • Set D – File Operations & Text Processing
  • Set E – Display Lines with Validation
  • Set F – Count Files & Directories
  • Set G – Recently Modified Files
  • Set H – Employee Gross Salary

🐧 Conclusion

The Vowels Count and Case Conversion practical is a useful shell scripting exercise for understanding strings, loops, conditions, Linux commands, and Bash parameter expansion.

Try writing each solution yourself instead of simply copying the code. Understanding the logic will make it much easier to solve similar questions during the practical examination.

Happy Learning & Best of Luck for your Linux Practical! 🐧💻

Top comments (0)