C语言标准库中提供了许多函数可以实现将字符串转为大小写。你可以使用以下函数进行转换:
#include<ctype.h>
int toupper(int c):将一个小写字符转换为大写字符。
int tolower(int c):将一个大写字符转换为小写字符。#include<string.h>
char *strupr(char *string):将一个字符串转换为大写。
char *strlwr(char *string):将一个字符串转换为小写。
举例1:
#include <stdio.h>
#include <ctype.h>int main()
{char str1[] = "Hello, World!";char str2[] = "HELLO,WORLD!";for (int i = 0; str1[i] != '\0'; i++) {str1[i] = toupper(str1[i]);str2[i] = tolower(str2[i]);}printf("%s\n", str1);printf("%s\n", str2);return 0;
}
输出:
HELLO, WORLD!
hello,world!
举例2:
#include<stdio.h>
#include<string.h>int main()
{char str[] = "Hello, World!";strupr(str);printf("%s\n", str);return 0;
}
输出:
HELLO, WORLD!