用c语言编程实现:删除一个字符串中的所有*号,并将其中的小写字母转换成大写字母,输出变化后的字符串。例如:若从键盘上输入一个字符串为:****abCD**Efg*hi*Jk******,最终在屏幕上的输出结果为:ABCDEFGHIJK
时间: 2024-06-18 19:05:06 浏览: 31
以下是用C语言编程实现删除字符串中所有*号,并将其中的小写字母转换成大写字母的代码:
```c
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main()
{
char str[100];
int i, j, len;
printf("请输入一个字符串:");
gets(str); // 输入字符串
len = strlen(str);
for (i = 0, j = 0; i < len; i++)
{
if (str[i] != '*') // 如果当前字符不是*号
{
if (islower(str[i])) // 如果当前字符是小写字母
{
str[j++] = toupper(str[i]); // 将小写字母转换为大写字母
}
else
{
str[j++] = str[i]; // 直接复制当前字符
}
}
}
str[j] = '\0'; // 字符串末尾加上\0,表示结束符
printf("变化后的字符串为:%s\n", str); // 输出变化后的字符串
return 0;
}
```
相关问题
c语言编程:(使用指针)输入任意一个字符串,将其中的大写字母转换成小写字母
好的,下面是使用指针实现将字符串中的大写字母转换成小写字母的c语言代码:
```c
#include <stdio.h>
void to_lower(char *str) {
while (*str != '\0') {
if (*str >= 'A' && *str <= 'Z') {
*str += 'a' - 'A';
}
str++;
}
}
int main() {
char str[100];
printf("请输入一个字符串:");
scanf("%s", str);
to_lower(str);
printf("转换后的字符串为:%s\n", str);
return 0;
}
```
代码的思路是遍历输入的字符串,如果当前字符是大写字母,则将其转换成小写字母。其中使用了指针来遍历字符串。
在c语言中实现:输入一个长度不超过20的字符串,将字符串中的所有小写字母替换为大写字母
以下是在C语言中实现将字符串中的小写字母替换为大写字母的示例代码:
```c
#include <stdio.h>
#include <string.h>
int main() {
char str[21]; //定义一个长度为21的字符数组,用于存储输入的字符串
int i;
printf("请输入一个长度不超过20的字符串:");
scanf("%s", str); //输入字符串
for (i = 0; i < strlen(str); i++) { //遍历字符串中的每一个字符
if (str[i] >= 'a' && str[i] <= 'z') { //如果该字符是小写字母
str[i] = str[i] - 32; //将小写字母转换为大写字母
}
}
printf("转换后的字符串为:%s", str); //输出转换后的字符串
return 0;
}
```
在上述代码中,我们定义了一个长度为21的字符数组用于存储输入的字符串。然后使用scanf函数输入字符串。接着使用for循环遍历字符串中的每一个字符,如果该字符是小写字母,则通过ASCII码将其转换为大写字母。最后使用printf函数输出转换后的字符串。
相关推荐
![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_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)