help for shell script of finding shortest substring from given string by user

please give me proper solution for finding a shortest substring from given string if string itself and first char and last char of that substr are also given by user

if S="dpoaoqooroo" and FC="o" and LC="o",then shortest substr is "oo" and rest of the string is "dpoaoqroo"
i have code but it is not working properly....so if u can give some new code or modify below code..
echo "enter the string"
read str
echo -e "\nenter first char of the substring"
read fc
echo -e "\nenter last char of the substring"
read lc
len=${#str}
no=`echo "$str" |awk -F "$lc" '{print NF}'`
no1=`echo "$str" |awk -F "$fc" '{print NF}'`
fci=`expr index "$str" $fc`
lci=`expr index "$str" $lc`
if {(test $no -eq 1 || test $no1 -eq 1)}
then
echo "substring not found 1"
exit 0
fi
flag=0
olen=0
ip2=""
for ((i=1;i<=$(($no-1));i++))
do
ip1=`echo "$str" |cut -d "$lc" -f$i`
echo "ip1 for $i times is $ip1"
len1=${#ip1}
ip2=${ip1##**$fc}
echo "ip2 for $i times is $ip2"
len2=${#ip2}
fcin=`expr index "$ip1" $fc`
if {(test $flag -eq 0 && test $len1 -ne 0 && test $fcin -ne 0)}
then
oip2=$ip2
olen=$len2
flag=1
key=1
else
if {(test "$olen" -gt "$len2" && test "$len1" -ne 0 && test "$fcin" -ne 0)}
then
oip2=$ip2
olen=$len2
key=1
fi fi
done
if (test "$key" = 1 )
then
echo "last shortest string is \"$fc$oip2$lc\""
str2=`echo ${str/$fc$oip2$lc/""}`
echo "the rest of the string is \"$str2\""
else
echo "substring not found last"
fi

The reading of the code is very hard. Please, use the "CODE" tags.
Regards.