Copy Code code as follows:
#!/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 $ | tr ' [: Upper:] ' [: Lower:] ')"
If ["$str"!= $]; Then #get some problem
echo [#have Upper Number,and I trun them to lower:#] "
Return 1 #have Upper number
Else
return 0 #no Upper number
Fi
}
If [$#-ne 1]; Then
echo "Usage: $ <string>" >&2
Exit 1
Fi
if! Haveuppernumber $; Then #when if is 0 it run?
#if [0]; Then #in shell true 0, false return 1
echo $ | TR ' [: Upper:] ' [: Lower:] ' #it can turn the upper number to lower
# echo $ | TR ' [: Lower:] ' [: Upper:] ' #it can turn the lower number to upper
Else
echo "[#no Upper number:#]"
echo $
Fi
Exit 0
Function Description: When you enter "./upper_to_lower.sh AABBCCDD", you will first determine if the input format is correct, and then determine if there are uppercase letters in the string if there is a display "[#have Upper Number,and I well trun them to lower:#] "and a string that is converted to lowercase letters, and if there is no capital letter showing" [#no Upper number:#] "and a lowercase string.
And then try to use the C language to achieve the same function, as follows:
Copy Code code as follows:
#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'll 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 ("%s\n", argv[1]);
}
Else
{
printf ("%s\n", argv[1]);
}
return 0;
}