C语言将字符串小写字母转换为大写字母
时间: 2023-12-25 17:03:10 浏览: 106
可以使用 C 语言中的 toupper 函数来将小写字母转换为大写字母。例如,可以使用以下代码:
```c
#include <stdio.h>
#include <ctype.h>
int main() {
char str[] = "Hello, World!";
int i = 0;
while (str[i]) {
putchar(toupper(str[i]));
i++;
}
return 0;
}
```
这段代码将输出 "HELLO, WORLD!",其中 toupper 函数将小写字母转换为大写字母。
相关问题
C语言将字符串小写字母转换为大写字母且不使用转换函数
可以使用循环的方式,遍历字符串中的每个字符,判断该字符是否为小写字母,如果是,则将其转换为对应的大写字母。
代码示例:
```c
#include <stdio.h>
int main()
{
char str[100];
int i;
printf("Enter a string: ");
gets(str);
for(i=0; str[i]!='\0'; i++)
{
/* Check if character is lowercase */
if(str[i]>='a' && str[i]<='z')
{
/* Convert to uppercase */
str[i] = str[i] - 32;
}
}
printf("Uppercase string is: %s", str);
return 0;
}
```
该程序先读入一个字符串,然后遍历字符串中的每个字符,判断是否为小写字母。如果是,就将其ASCII码值减去32,将其转换为大写字母。最后输出转换后的字符串。
C语言利用函数将字符串小写字母转换为大写字母
在C语言中,可以使用标准库函数`tolower()`和`toupper()`来转换字符串中的字母大小写。以下是一个示例程序,展示了如何将字符串中的小写字母转换为大写字母:
```c
#include <stdio.h>
#include <ctype.h>
#include <string.h>
void convertToUpperCase(char *str) {
for(int i = 0; str[i]; i++) {
str[i] = toupper(str[i]);
}
}
int main() {
char str[100];
printf("请输入一个字符串: ");
fgets(str, sizeof(str), stdin);
// 去除换行符
str[strcspn(str, "\n")] = '\0';
printf("原始字符串: %s\n", str);
convertToUpperCase(str);
printf("转换后的字符串: %s\n", str);
return 0;
}
```
在这个程序中,`convertToUpperCase`函数遍历字符串中的每一个字符,并使用`toupper()`函数将其转换为大写字母。`toupper()`函数定义在`ctype.h`头文件中,用于将小写字母转换为大写字母。
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)