c语言函数调用字符串大写转小写
时间: 2023-06-21 22:09:35 浏览: 61
可以使用C语言的标准库函数`tolower()`将字符串中的大写字母转换为小写字母。下面是一个示例代码:
```c
#include <stdio.h>
#include <ctype.h>
void toLower(char* str) {
int i = 0;
while(str[i]) {
str[i] = tolower(str[i]);
i++;
}
}
int main() {
char str[] = "HELLO WORLD";
toLower(str);
printf("%s\n", str); // 输出 hello world
return 0;
}
```
在上面的代码中,`toLower()`函数接受一个字符串参数,并使用`tolower()`函数将其中的大写字母转换为小写字母。然后,在`main()`函数中,我们将一个字符串“HELLO WORLD”传递给`toLower()`函数,并将其输出为“hello world”。
相关问题
c语言函数调用字符串大小写
可以使用C语言内置的字符串处理函数来实现字符串大小写转换,例如使用toupper()和tolower()函数。
以下是一个示例代码,可以将字符串中的所有小写字母转换为大写字母:
```c
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int main()
{
char str[100];
printf("请输入一个字符串:");
fgets(str, 100, stdin); // 从标准输入读取字符串
for (int i = 0; i < strlen(str); i++) {
if (islower(str[i])) { // 判断字符是否为小写字母
str[i] = toupper(str[i]); // 转换为大写字母
}
}
printf("转换后的字符串为:%s", str);
return 0;
}
```
同理,如果需要将字符串中的所有大写字母转换为小写字母,可以使用tolower()函数。
c语言字符串小写转大写程序
下面是一个 C 语言字符串小写转大写的示例程序:
```c
#include <stdio.h>
#include <ctype.h>
void to_upper(char *str) {
while (*str) {
*str = toupper(*str);
str++;
}
}
int main() {
char str[100];
printf("请输入一个字符串:");
fgets(str, sizeof(str), stdin);
to_upper(str);
printf("转换后的字符串为:%s", str);
return 0;
}
```
程序中使用了 `toupper()` 函数将字符串中的小写字母转成大写字母。函数 `to_upper()` 接受一个指向字符串的指针,然后遍历字符串中的每个字符,将小写字母转成大写字母。 `main()` 函数中获取用户输入的字符串,调用 `to_upper()` 函数将其转换成大写字母,并输出结果。