实现大小写转换的三种方法
方法一:
#include<stdio.h>
#include<stdlib.h>
int main()
{
char str[] = "AbCdEf";
char c;
int i = 0;
while (str[i] != '\0')
{
c = str[i];
if (c >= 'A' && c <= 'Z')
{
c = c + 32;
}
else if (c >= 'a' && c <= 'z')
{
c = c - 32;
}
printf("%c",c);
i++;
}
printf("\n");
system("pause");
return 0;
}
运行结果如下:
方法二:
#include<stdio.h>
#include<stdlib.h>
int main()
{
char str[] = "AbCdEf";
char c;
int i = 0;
while (str[i] != '\0')
{
c = str[i];
if (c >= 'A' && c<='Z' || c>='a' && c <= 'z')
{
c ^= 32;
}
printf("%c", c);
i++;
}
printf("\n");
system("pause");
return 0;
}
运行结果如下:
方法三:
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
int main()
{
char str[] = "AbCdEf";
char c;
int i = 0;
while (str[i] != '\0')
{
c = str[i];
if (isupper(c))
{
c = tolower(c);
}
else if (islower(c))
{
c = toupper(c);
}
printf("%c", c);
i++;
}
printf("\n");
system("pause");
return 0;
}
运行结果如下: