Register
It is currently Fri Oct 24, 2014 6:44 am

Find the first alphabetic letter from the input:


All times are UTC - 6 hours


Post new topic Reply to topic  [ 6 posts ] 
Author Message
 PostPosted: Sun Apr 25, 2010 7:29 pm   

Joined: Sun Apr 25, 2010 7:02 pm
Posts: 3
Find the first alphabetic letter from the input:
Accept the input of a series of upper case alphabetic letters one at a time. The input ends with a 0. Find and display the first letter in alphabetic order. For example, input of D, G, T, S, V, G, C, K, P should result in C. Any invalid input character (eg. #, $, 3, a, etc.) should cause an error message and be ignored. Who know how to solve this problem, please help me.


Top
 Profile  
 PostPosted: Sun Apr 25, 2010 8:05 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 550
Hi alibaba,

This is homework, we won't do it for you.

This must relate to your course, so first read it

Please, let us know what you've tried so far, and we'll be glad to help where you have difficulties.


Top
 Profile  
 PostPosted: Mon Apr 26, 2010 7:21 am   

Joined: Sun Apr 25, 2010 7:02 pm
Posts: 3
This is my code:
Code:
input=-1; temp="Z"
while [ $input -ne 0 ]; do
echo "Please enter the upper case letter"; read input
if [ $input\< "Z" -o $input\> "A" ]; then
         if [ $input < $temp ]; then
         temp= $input
         fi
else
         echo "Invalid letter"
fi
done
echo "The first letter is $temp"

It doesn't work and i think it also has another problem with temp = "Z", because if the first input is 0, the output will be Z, so i think this is a problem. I tried to use array, but i haven't studied about array in bash scripts yet. So some command i don't know. Can you give me some advise. Thaks any way


Top
 Profile  
 PostPosted: Mon Apr 26, 2010 7:51 am   

Joined: Mon Nov 17, 2008 7:25 am
Posts: 221
Since you atleast tried, i'll give a nudge on the way :)

Code:
#!/bin/bash
# Read the input string (like "klkslka")
read -p "Enter string with input: " input
# Test the content in $input for the following content
case $input in
   # Any digits? (0-9)
   *[0-9]*) echo "Digit in input" ;;
   # Any special characters (google for "regexp punct" if you need to know which ones)
   *[[:punct:]]*) echo "Punct characters in input" ;;
   # If neither special characters or digits are found then the leftovers mush be alpha-characters (upper or lower case)
   # You could test case style also by either using [:upper:]/[:lower:] or [A-Z]/[a-z]
   *)
      # Format the input so you can sort the string letter by letter. The sed regexp says to put a newline after each character (which /g does)
      formatted_input=$(echo $input | sed -e "s/\(.\)/\1\n/g" | sort)
      # Echo the output, the head/tail combo is because this generates a newline at the top (atleast for me)
      echo "$formatted_input" | head -n2 | tail -n1
   ;;
esac


Hope this gives you a clue :)

Best regards
Fredrik Eriksson


Top
 Profile  
 PostPosted: Mon Apr 26, 2010 3:15 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 550
Code:
#!/bin/bash

input=-1
temp="Z"

#while [ $input -ne 0 ] #umq! +integer expected
while [ "$input" != 0 ]
do echo "Please enter an upper case letter"
   read input
#   if [ $input\< "Z" -o $input\> "A" ]
   if [[ $input == [A-Z]\|[^a-z] ]] #no quotes needed with double square brackets
   then if [[ $temp > $input ]]
        then temp="$input" #no spaces around equal sign
        fi
   else
        [ "$input" != 0 ] && echo "Invalid letter"
   fi
done
echo "The first letter is $temp"
using case, as suggested, is better.


Top
 Profile  
 PostPosted: Mon Apr 26, 2010 5:00 pm   

Joined: Sun Apr 25, 2010 7:02 pm
Posts: 3
Thanks everybody, i also have tried case, i thought it was a good method, but i didnt know *[[:punct:]]*), so i couldnt figure it out. :P


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Google [Bot] and 7 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP