广告
返回顶部
首页 > 资讯 > 后端开发 > 其他教程 >用shell脚本和c语言将大写字母转成小写的代码
  • 740
分享到

用shell脚本和c语言将大写字母转成小写的代码

大写字母转成脚本 2022-06-04 21:06:54 740人浏览 安东尼
摘要

#!/bin/bash#name: upper_to_lower.sh#the function is trun uper to lower #like ABCd to abcd haveuppernum


#!/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 <string>" >&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<stdio.h>
#include<stdlib.h>

int haveuppernumber(char *p)
{
char*q=p;

for(;*q!='';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 != '';p++)
{
if(*p>='A' && *p<='Z')
{
*p+=' ';
}
}
}

int main(int arGC , char *argv[])
{
char *p;
p=argv[1];

if(argc != 2)
{
printf("Usage : %s <string>n",argv[0]);
exit(-1);
}
if(haveuppernumber(p))
{
turntolower(p);
printf("%sn",argv[1]);
}
else
{
printf("%sn",argv[1]);
}
return 0;
}


查看图片

--结束END--

本文标题: 用shell脚本和c语言将大写字母转成小写的代码

本文链接: https://www.lsjlt.com/news/17989.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • 用shell脚本和c语言将大写字母转成小写的代码
    #!/bin/bash#name: upper_to_lower.sh#the function is trun uper to lower #like ABCd to abcd haveuppernum...
    99+
    2022-06-04
    大写字母 转成 脚本
  • c语言怎么将大写字母转成小写
    本文小编为大家详细介绍“c语言怎么将大写字母转成小写”,内容详细,步骤清晰,细节处理妥当,希望这篇“c语言怎么将大写字母转成小写”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。#!/bin/bash#name: u...
    99+
    2023-06-16
  • 大小写字母转换的shell脚本代码
    以下脚本,可以进行目录或文件大小写字母转换,代码如下: #!/bin/sh #edit by www.lsjlt.com # [:upper:] [ A - Z ] # [:lower:] [ a - z...
    99+
    2022-06-04
    脚本 写字母 大小
  • c语言中如何将大写字母转成小写
    今天就跟大家聊聊有关c语言中如何将大写字母转成小写,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。#!/bin/bash#name: upper_to_lower.sh#the fun...
    99+
    2023-06-09
  • c语言大小写字母转化代码怎么写
    在C语言中,可以使用字符数组和ASCII码来实现大小写字母的转化。下面是一个示例代码,将输入的字符串中的大写字母转换为小写字母,小写...
    99+
    2023-10-23
    c语言
  • C语言详解用char实现大小写字母的转换
    目录一、例题二、例题三、总结一、例题 将多个字母进行小写转换成大写 代码: #include <stdio.h> int main (void) { char a,b,...
    99+
    2022-11-13
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作