您现在的位置是:网站首页> 编程资料编程资料
用shell脚本和c语言将大写字母转成小写的代码_linux shell_
2023-05-26
337人已围观
简介 用shell脚本和c语言将大写字母转成小写的代码_linux shell_
#!/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
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!='\0';q++)
{
if(*q>='A'&&*q<='Z')
{
printf("[#have upper number and i will turn them to lower #]\n");
return 1;
}
}
printf("[#no upper number#]\n");
return 0;
}
void turntolower(char *p)
{
for(;*p != '\0';p++)
{
if(*p>='A' && *p<='Z')
{
*p+=' ';
}
}
}
int main(int argc , char *argv[])
{
char *p;
p=argv[1];
if(argc != 2)
{
printf("Usage : %s
exit(-1);
}
if(haveuppernumber(p))
{
turntolower(p);
printf("%s\n",argv[1]);
}
else
{
printf("%s\n",argv[1]);
}
return 0;
}

相关内容
- 大小写字母转换的shell脚本代码_linux shell_
- 获取磁盘IO与系统负载Load的shell脚本_linux shell_
- 分享个简易版Linux服务器初始化Shell脚本_linux shell_
- 几个常用的Linux操作系统监控脚本代码_linux shell_
- Linux命令行里的“瑞士军刀”_linux shell_
- 你可能不知道的Shell(有趣的知识)_linux shell_
- Linux shell脚本基础学习详细介绍(完整版)第1/2页_linux shell_
- linux服务器安全加固shell脚本代码_linux shell_
- shell判断文件,目录是否存在或者具有权限的代码_linux shell_
- 用shell脚本实现自动切换内网和外网实现高可用_linux shell_
