用shell脚本和c语言将大写字母转成小写的代码
本文导语: 代码如下:#!/bin/bash#name: upper_to_lower.sh#the function is trun uper to lower #like ABCd to abcd haveuppernumber(){ #test if the string have upper number str="$(echo $1 | tr '[:upper:]' '[:lower:]')" if [ "$str" != $1 ] ; then #get some problem echo "[#have ...
#!/bin/bash
#name: upper_to_lower.sh
#the function is trun uper to lower
#like ABCd to abcd
haveuppernumber()
{
#test if the string have upper number
str="$(echo $1 | tr '[:upper:]' '[:lower:]')"
if [ "$str" != $1 ] ; then #get some problem
echo "[#have upper number,and i well trun them to lower:#]"
return 1 #have upper number
else
return 0 #no upper number
fi
}
if [ $# -ne 1 ] ; then
echo "Usage: $0 " >&2
exit 1
fi
if ! haveuppernumber $1 ; then #when if is 0 it run?
#if [ 0 ] ; then #in shell true return 0 ,false return 1
echo $1 | tr '[:upper:]' '[:lower:]' #it can turn the UPPER number to lower
# echo $1 | tr '[:lower:]' '[:upper:]' #it can turn the lower number to UPPER
else
echo "[#no upper number:#]"
echo $1
fi
exit 0
功能说明:当输入”./upper_to_lower.sh AaBbCcdd“时会先判断输入格式是否正确,然后判断字符串中是否有大写字母如果有显示"[#have upper number,and i well trun them to lower:#]"和转换成小写字母后的字符串;如果没有大写字母显示"[#no upper number:#]"和小写字符串。
然后又试着用c语言实现相同的功能,如下:
#include
#include
int haveuppernumber(char *p)
{
char*q=p;
for(;*q!='';q++)
{
if(*q>='A'&&*q='A' && *p