输入一个句子,用C语言编写一个函数,不使用ctype.h,它的输入就是这个句子,把这个句子中的小写字母 变成大写字母,把大写字母变成小写字母,并在屏幕上打印出来。
时间: 2024-05-14 07:14:08 浏览: 9
```c
#include <stdio.h>
void changeCase(char sentence[]) {
int i = 0;
while (sentence[i] != '\0') {
if (sentence[i] >= 'a' && sentence[i] <= 'z') {
sentence[i] = sentence[i] - 32;
}
else if (sentence[i] >= 'A' && sentence[i] <= 'Z') {
sentence[i] = sentence[i] + 32;
}
i++;
}
printf("%s", sentence);
}
int main() {
char sentence[100];
printf("Please enter a sentence: ");
fgets(sentence, 100, stdin);
changeCase(sentence);
return 0;
}
```
相关问题
用c语言编写一个函数,输入一个16进制数,输出相应的10进制数
好的,我可以帮你解答这个问题。以下是一个用 C 语言编写的函数,可以将输入的 16 进制数转换为相应的 10 进制数:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int hexToDec(char hex[]) {
int len = strlen(hex);
int base = 1;
int dec = 0;
for (int i = len - 1; i >= 0; i--) {
if (isdigit(hex[i])) {
dec += (hex[i] - '0') * base;
} else {
dec += (toupper(hex[i]) - 'A' + 10) * base;
}
base *= 16;
}
return dec;
}
int main() {
char hex[100];
printf("请输入一个16进制数:");
scanf("%s", hex);
printf("对应的10进制数为:%d\n", hexToDec(hex));
return 0;
}
```
这个函数的基本思路是将输入的 16 进制数从低位到高位依次转换为 10 进制数,然后相加得到最终的结果。在转换过程中,我们需要判断每个位上的字符是数字还是字母,并根据情况进行相应的转换。
输入一个句子,用C语言编写一个函数,它的输入就是这个句子,把这个句子中的小写字母 变成大写字母,把大写字母变成小写字母,并在屏幕上打印出来。
```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;
}
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)