c语言编写一个函数,将字符串中的小写字母转换为大写字母,主西数输入字符串并输出结果。
时间: 2023-06-15 12:06:23 浏览: 259
下面是一个简单的 C 语言函数,可以将输入字符串中的小写字母转换为大写字母,并输出结果:
```c
#include <stdio.h>
#include <string.h>
void toUpperCase(char *str) {
int i;
int len = strlen(str);
for (i = 0; i < len; i++) {
if (str[i] >= 'a' && str[i] <= 'z') {
str[i] -= 32; // 将小写字母转换为大写字母
}
}
}
int main() {
char str[100];
printf("请输入字符串:");
gets(str);
toUpperCase(str);
printf("转换后的字符串为:%s", str);
return 0;
}
```
在上面的代码中,`toUpperCase()` 函数接收一个字符串指针作为参数,遍历字符串中的每个字符,如果字符是小写字母,则将其转换为大写字母。在主函数中,我们使用 `gets()` 函数获取用户输入的字符串,并将其传递给 `toUpperCase()` 函数进行转换。最后,我们使用 `printf()` 函数输出转换后的字符串。
相关问题
编写一个函数,将字符串中的小写字母转换为大写字母,主西数输入字符串并输出结果。(c语言)
好的,以下是 C 语言的代码实现:
```c
#include <stdio.h>
#include <ctype.h>
void to_uppercase(char input_string[]) {
int i = 0;
while (input_string[i] != '\0') {
if (islower(input_string[i])) {
input_string[i] = toupper(input_string[i]);
}
i++;
}
}
int main() {
char input_str[100];
printf("请输入字符串:");
scanf("%s", input_str);
to_uppercase(input_str);
printf("转换后的字符串为:%s\n", input_str);
return 0;
}
```
这个函数的实现思路也是遍历输入的字符串中的每个字符,如果该字符是小写字母,则使用 `toupper()` 函数将其转换为大写字母。由于 C 语言中字符串以 '\0' 结尾,所以在遍历字符串时要判断字符是否为 '\0' 结尾。最终在 `main()` 函数中调用 `to_uppercase()` 函数实现字符串转换。
C语言编写一个函数,将字符串中的小写字母转换为大写字母,主函数输入字符串并输出结果
下面是一个将字符串中的小写字母转换为大写字母的函数实现:
```c
#include <stdio.h>
#include <ctype.h>
void to_uppercase(char* str) {
int i = 0;
while (str[i] != '\0') {
if (islower(str[i])) {
str[i] = toupper(str[i]);
}
i++;
}
}
int main() {
char str[100];
printf("请输入一个字符串:");
scanf("%s", str);
to_uppercase(str);
printf("转换后的字符串为:%s\n", str);
return 0;
}
```
该函数通过遍历字符串中的每个字符,如果是小写字母,就将其转换为大写字母。主函数中,首先输入一个字符串,然后调用 `to_uppercase` 函数将其中的小写字母转换为大写字母,并输出转换后的结果。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)