Use shell script and C to turn uppercase letters into lowercase code _linux shell

Source: Internet
Author: User
Tags lowercase

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;
}


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.