Bash: Pattern Matching
Given an array of strings, count the strings
that contain at least one uppercase character
and output the result to stdout.
Example:
my array=[FirstWord" "Word2" "thirdiword']
The match_uppercase function will return 2
since "FirstWord" and "Word2" each contain
an uppercase letter.
My solution
1#1/usr/bin/env bash
3 Input array is read and stored in to my_array variable.
#You can view the code by pressing> button above.
Sample inpu and output
The match uppercase function will return 2
since "FirstWord" and "Word2" each contain
an uppercase letter.
Input Format For Custom Testing
The only line that contains the elements of
myarray
Sample Case
Sample Input For Custom Testing
Abc
bcd
Efs
def
cDe
Sample Output
Explanation
There are 3 strings containing at least one
uppercase character: Abc', "Efg". and
TCDe".
Given an array of strings, count the strings
that contain at least one uppercase character
and output the result to stdout.
Example:
my array=[FirstWord" "Word2" "thirdiword']
The match_uppercase function will return 2
since "FirstWord" and "Word2" each contain
an uppercase letter.
My solution
1#1/usr/bin/env bash
3 Input array is read and stored in to my_array variable.
#You can view the code by pressing> button above.
Code:
function match_uppercase()
typeset-a data-("se")
#Write your code here
Let count = 0
for upper in $data[@] ;
do
if I[ Supper [@) [A-Z] ;; then
echo $((count+1))
fi
done
Sample inpu and output
The match uppercase function will return 2
since "FirstWord" and "Word2" each contain
an uppercase letter.
Input Format For Custom Testing
The only line that contains the elements of
myarray
Sample Case
Sample Input For Custom Testing
Abc
bcd
Efs
def
cDe
Sample Output
Explanation
There are 3 strings containing at least one
uppercase character: Abc', "Efg". and
TCDe".