输入一个句子,用C语言编写一个函数,它的输入就是这个句子,把这个句子中的小写字母 变成大写字母,把大写字母变成小写字母,并在屏幕上打印出来。
时间: 2024-05-10 20:20:00 浏览: 6
```c
#include <stdio.h>
#include <ctype.h>
void transform(char* sentence){
int i = 0;
while(sentence[i] != '\0'){
if(islower(sentence[i])){
sentence[i] = toupper(sentence[i]);
}else if(isupper(sentence[i])){
sentence[i] = tolower(sentence[i]);
}
i++;
}
printf("%s\n", sentence);
}
int main(){
char sentence[] = "Hello World!";
transform(sentence);
return 0;
}
```
相关问题
用C语言编写一个程序,统计输入的字符串中大写字母和小写字母的个数
好的,我可以为您提供这个程序的代码。请注意,这个程序只能处理单个输入字符串,如果要处理多个字符串,需要在代码中加入循环。
```c
#include <stdio.h>
int main() {
char input[1000];
int i, upper = 0, lower = 0;
printf("请输入一个字符串:");
fgets(input, 1000, stdin);
for (i = 0; input[i] != '\0'; i++) {
if (input[i] >= 'A' && input[i] <= 'Z')
upper++;
else if (input[i] >= 'a' && input[i] <= 'z')
lower++;
}
printf("大写字母个数:%d\n", upper);
printf("小写字母个数:%d\n", lower);
return 0;
}
```
这个程序会让用户输入一个字符串,并统计其中大写字母和小写字母的个数。程序中使用了一个字符数组 `input` 存储输入的字符串,使用了两个变量 `upper` 和 `lower` 存储大写字母和小写字母的个数。其中,`fgets()` 函数用于读取用户输入的字符串,`for` 循环用于遍历字符串中的每一个字符,如果字符是大写字母则 `upper` 自增,如果是小写字母则 `lower` 自增。最后,程序输出统计结果。
用C语言写编写函数检测输入的一个字符是大写字母还是小写字母或不是26英文字母
可以使用C语言中的标准函数isupper和islower来判断输入的字符是否为大写字母或小写字母。具体实现如下:
```c
#include <stdio.h>
#include <ctype.h>
void detectCharType(char c){
if(islower(c)){
printf("%c是小写字母\n", c);
}else if(isupper(c)){
printf("%c是大写字母\n", c);
}else{
printf("%c不是26英文字母\n", c);
}
}
int main(){
char c;
printf("请输入一个字符:");
scanf("%c", &c);
detectCharType(c);
return 0;
}
```
首先,我们通过scanf函数读取输入的字符c。然后调用detectCharType函数来判断字符类型。如果是小写字母,则调用islower函数返回非零值,输出“是小写字母”;如果是大写字母,则调用isupper函数返回非零值,输出“是大写字母”;如果不是26英文字母,则输出“不是26英文字母”。
相关推荐
![](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)