写一个函数toUpperCase(char a[]),用于将字符串中的小写字母转化成大写字母,并在main函数中调用。
时间: 2024-04-29 10:23:34 浏览: 25
下面是一个C++的实现:
```cpp
#include <iostream>
using namespace std;
void toUpperCase(char a[]) {
int i = 0;
while (a[i] != '\0') {
if (a[i] >= 'a' && a[i] <= 'z') {
a[i] -= ('a' - 'A');
}
i++;
}
}
int main() {
char str[] = "Hello, world!";
toUpperCase(str);
cout << str << endl;
return 0;
}
```
思路是遍历字符串中的每个字符,如果它是小写字母,就将它转化为对应的大写字母。在main函数中调用toUpperCase函数即可。
相关问题
用c语言写一个函数toUpperCase(char a[]),用于将字符串中的小写字母转化成大写字母,并在main函数中调用。 例如:
```c
#include <stdio.h>
#include <string.h>
void toUpperCase(char a[]) {
int len = strlen(a);
for(int i=0; i<len; i++) {
if(a[i]>='a' && a[i]<='z') { // 判断是否为小写字母
a[i] = a[i] - 'a' + 'A'; // 转化为大写字母
}
}
}
int main() {
char str[100];
printf("请输入字符串:");
scanf("%s", str);
toUpperCase(str);
printf("转换后的字符串为:%s\n", str);
return 0;
}
```
示例输出:
请输入字符串:Hello, World!
转换后的字符串为:HELLO, WORLD!
c语言编写一个函数,将字符串中的小写字母转换为大写字母,主西数输入字符串并输出结果。
下面是一个简单的 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()` 函数输出转换后的字符串。