C语言删除已输出字符案例
时间: 2023-05-19 13:05:30 浏览: 88
以下是一个简单的 C 语言程序,可以删除已输出的字符:
```c
#include <stdio.h>
#include <conio.h>
int main()
{
char str[100];
int i, j, len;
printf("请输入一个字符串:");
gets(str);
len = strlen(str);
printf("删除后的字符串为:");
for(i = 0; i < len; i++)
{
for(j = i + 1; j < len; j++)
{
if(str[i] == str[j])
{
str[j] = ' ';
}
}
if(str[i] != ' ')
{
printf("%c", str[i]);
}
}
getch();
return 0;
}
```
这个程序会读取用户输入的字符串,然后删除其中已经输出的字符,最后输出剩余的字符。
相关问题
c语言格式化输出字符串
C语言中的格式化输出字符串是通过printf函数实现的。printf函数是C语言标准库函数,用于将格式化后的字符串输出到标准输出(终端屏幕)。printf函数的声明在头文件stdio.h中。
格式化输出字符串的基本语法是在printf函数中使用格式控制字符串和输出项。格式控制字符串以%开头,后面跟有各种格式控制符,用于说明输出数据的类型、宽度、精度等。
下面是一个示例,演示了如何使用printf函数进行格式化输出字符串:
```c
#include <stdio.h>
int main() {
int num = 123;
float f = 3.14;
char ch = 'A';
char str[] = "Hello, World!";
printf("整数:%d\n", num);
printf("浮点数:%f\n", f);
printf("字符:%c\n", ch);
printf("字符串:%s\n", str);
return 0;
}
```
输出结果:
```
整数:123
浮点数:3.140000
字符:A
字符串:Hello, World!
```
在格式控制字符串中,%d表示输出整数,%f表示输出浮点数,%c表示输出字符,%s表示输出字符串。输出项与格式控制字符串中的格式控制符一一对应。
C语言print打印输出字符数组
在C语言中,可以使用printf函数打印输出字符数组。下面是一个示例代码:
```c
#include <stdio.h>
int main() {
char str[] = "Hello, world!"; // 定义一个字符数组
printf("%s\n", str); // 使用%s格式化输出字符数组
return 0;
}
```
输出结果:
```
Hello, world!
```
在printf函数中,%s用来表示输出字符串,它会依次输出字符数组中的每个字符,直到遇到字符串结束符'\0'为止。注意,字符数组必须以'\0'结尾,否则输出结果可能会不可预测。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![ppt](https://img-home.csdnimg.cn/images/20210720083527.png)
![torrent](https://img-home.csdnimg.cn/images/20210720083646.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)