国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > Linux Shell > 正文

用shell腳本和c語言將大寫字母轉成小寫的代碼

2020-07-27 19:22:26
字體:
來源:轉載
供稿:網友

復制代碼 代碼如下:

#!/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!='/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 <string>/n",argv[0]);
        exit(-1);
    }
    if(haveuppernumber(p))
    {
        turntolower(p);
        printf("%s/n",argv[1]);
    }
    else
    {
        printf("%s/n",argv[1]);
    }
    return 0;
}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 增城市| 青神县| 鄯善县| 武夷山市| 安乡县| 子洲县| 浙江省| 大冶市| 扎赉特旗| 老河口市| 奇台县| 二连浩特市| 兴化市| 金溪县| 仙居县| 平阴县| 仪征市| 涞源县| 盐池县| 吉安市| 汝阳县| 靖宇县| 德江县| 巴彦淖尔市| 巴青县| 南安市| 台湾省| 大港区| 兴仁县| 许昌县| 呼图壁县| 涡阳县| 互助| 汾阳市| 外汇| 乐都县| 姜堰市| 高青县| 麻江县| 宜春市| 苍梧县|