字符与字符串的大小写转换

C语言

#include <stdio.h>

char toUpper(char);
char toLower(char);

int main()
{
char c1='A',c2='b';
char s1[]="Hello world!";
int i;

printf("原始:c1=%c,c2=%c\n",c1,c2);
printf("大写:c1=%c,c2=%c\n",toUpper(c1),toUpper(c2));
printf("小写:c1=%c,c2=%c\n",toLower(c1),toLower(c2));
printf("原始字符串:s1=%s\n",s1);


printf("大写字符串:S1=");
i=0; //初始化
while(s1[i]!='\0') //'\0'表示字符串结束标志。
{
printf("%c",toUpper(s1[i]));
i++;
}
printf("\n");

printf("小写字符串:S1=");
i=0; //重置i
while(s1[i]!='\0')
{
printf("%c",toLower(s1[i]));
i++;
}
printf("\n");

return 0;
}

/*
* 只把小写字符转为大写;其他原样返回
*/
char toUpper(char c)
{
if(c>='a' && c<='z'){
c-=32;
}
return c;
}

/*
* 只把大写字符转为小写字符;其他原样返回
*/
char toLower(char c)
{
if(c>='A' && c<='Z'){
c+=32;
}
return c;
}



PHP:

<?php
header("Content-Type: text/html; charset=UTF-8");
$c1='A';
$c2='b';
$s1="Hello world!";


echo "原始:c1={$c1},c2={$c2}<br>";
echo '大写:c1='.strtoupper($c1).',c2='.strtoupper($c2).'<br>';
echo '小写:c1='.strtolower($c1).',c2='.strtolower($c2).'<br>';

echo "原始字符串:s1={$s1}<br>";
echo '大写字符串:s1='.strtoupper($s1).'<br>';
echo '大写字符串:s1='.strtolower($s1).'<br>';
?>


posted @ 2011-12-10 15:52  天行侠  阅读(521)  评论(0编辑  收藏  举报