#include "stdio.h"
#include "string.h"
#include <stdlib.h>
void inv(char *s)
{
int i;
for(i=0;i<strlen(s);i++)
{
if(*(s+i)>=65 && *(s+i)<=92)
*(s+i)+=32;
else if (*(s+i)>=97 && *(s+i)<=122)
*(s+i)-=32;
}
}
void main()
{
char string[100];//给string数组分配100个空间
printf("请输入字符串:\n");
gets(string);
inv(string);//string 就已经是char*类型了
printf("转换后:\n");
puts(string);
system("pause");
}
A~Z: 0x41 ~ 0x5A.
小写字符范围为:
a~z:0x61 ~0x7A.
可以发现,对应的大小写字符之间的规律为,小写字符比相应大写字符大0x20,即10进制的32。
所以,要大写转小写,可以写作
#define TO_LOWER(c) (c+0x20)
而小写转大写,可以写作
#define TO_UPPER(c) (c-0x20)
//nginx中的源码
#define ngx_tolower(c) (u_char) ((c >= 'A' && c <= 'Z') ? (c | 0x20) : c)
#define ngx_toupper(c) (u_char) ((c >= 'a' && c <= 'z') ? (c & ~0x20) : c)
void ngx_strlow(u_char *dst, u_char *src, size_t n)
{
while (n) {
*dst = ngx_tolower(*src);
dst++;
src++;
n--;
}
}