C语言编写一个程序,将字符“0”分别转换为“9”、“1”转换为“8”、“2”转换为”7“、……”9“转换为”0“。
时间: 2023-11-01 09:07:56 浏览: 24
下面是C语言代码实现:
```c
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
printf("请输入字符串:");
scanf("%s", str);
int len = strlen(str);
for(int i = 0; i < len; i++) {
if(str[i] >= '0' && str[i] <= '9') {
str[i] = '9' - (str[i] - '0');
}
}
printf("转换后的字符串为:%s\n", str);
return 0;
}
```
在这个程序中,我们先定义了一个字符串 `str`,并从用户获取输入。接着,我们通过 `strlen()` 函数获取字符串的长度,并使用 `for` 循环遍历字符串中的每个字符。对于每个字符,我们判断它是否为数字,如果是数字,则将其转换为另一个数字。具体地,我们用字符 `'9'` 减去当前字符与字符 `'0'` 的差值,就可以得到需要转换的数字。最后,我们输出转换后的字符串。
例如,如果用户输入了字符串 `0123456789`,程序将会输出 `9876543210`。
相关问题
c语言编写long fun将一个数字字符转换为一个整数
以下是使用C语言编写的将一个数字字符转换为一个整数的函数:
```c
long fun(char c){
return c - '0';
}
```
此函数将输入的字符 `c` 转换为相应的整数。它使用 ASCII 码表中数字字符的编码值减去 `'0'` 的编码值,因为数字字符的编码值是连续的。
例如,输入字符 `'5'`,则函数返回整数 `5`。
C语言编写程序,将字符串中小写字母字符转换为大写。
以下是一个简单的C程序,可以将字符串中的小写字母全部转换为大写字母:
```c
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main() {
char str[1000];
printf("请输入字符串:");
fgets(str, sizeof(str), stdin); // 从标准输入读取字符串
int len = strlen(str);
for (int i = 0; i < len; i++) {
if (islower(str[i])) { // 判断是否为小写字母
str[i] = toupper(str[i]); // 将小写字母转换为大写字母
}
}
printf("转换后的字符串为:%s", str);
return 0;
}
```
这个程序首先从标准输入读取一个字符串,然后遍历字符串的每一个字符,如果发现是小写字母,就使用 `toupper()` 函数将它转换为大写字母。最后输出转换后的字符串。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)