c语言输入一个长度不超过 100 的字符串,删除串中的重复字符。输入:输入要检查的字符串,长度不超过100个字符。例如:abacaeedabcdcd。 输出: 删除重复字符后的字符串。例如:abced。
时间: 2024-05-05 20:18:46 浏览: 22
Sure, 我可以回答这个问题。下面是C语言的实现方式:
```
#include <stdio.h>
#include <string.h>
void removeDuplicates(char str[], int len)
{
int index = 0;
for (int i = 0; i < len; i++) {
int j;
for (j = 0; j < i; j++)
if (str[i] == str[j])
break;
if (j == i)
str[index++] = str[i];
}
str[index] = '\0';
}
int main()
{
char str[101];
printf("请输入不超过100个字符的字符串:\n");
scanf("%s", str);
int len = strlen(str);
removeDuplicates(str, len);
printf("删除重复字符后的字符串为:%s\n", str);
return 0;
}
```
希望对您有所帮助。
相关问题
c语言输入一个长度不超过 100 的字符串,删除串中的重复字符。
可以使用一个布尔型的数组来记录每个字符是否出现过,如果出现过就将其删除。具体实现如下:
```c
#include <stdio.h>
#include <string.h>
int main() {
char s[101];
scanf("%s", s);
int len = strlen(s);
int appeared[256] = {0}; // 初始化为0
int i, j;
for (i = 0, j = 0; j < len; j++) {
if (!appeared[s[j]]) { // 如果字符没出现过
s[i++] = s[j]; // 将其加入结果串中
appeared[s[j]] = 1; // 标记为出现过
}
}
s[i] = '\0'; // 结束字符串
printf("%s\n", s);
return 0;
}
```
这里使用了一个 appeared 数组来记录每个字符是否出现过。具体实现中,我们从头到尾遍历字符串,如果当前字符没有出现过,就将其加入结果串中,并将其标记为出现过。如果当前字符已经出现过,就不做任何操作,直接跳过。遍历完成后,将结果串以'\0'结尾并输出即可。
用c语言输入5个长度不超过100的字符串,输出其中最长的字符串及其长度
可以使用一个循环来输入5个字符串,同时记录每个字符串的长度,然后再找到最长的字符串并输出其长度和内容。
以下是使用C语言实现的示例代码:
```c
#include <stdio.h>
#include <string.h>
int main() {
char str[5][101]; // 存储5个字符串,每个字符串长度不超过100
int len[5]; // 存储5个字符串的长度
int max_len = 0; // 最长字符串的长度
int max_index = 0; // 最长字符串的下标
// 循环输入5个字符串,并记录每个字符串的长度
for (int i = 0; i < 5; i++) {
printf("请输入第%d个字符串:", i+1);
scanf("%s", str[i]);
len[i] = strlen(str[i]);
}
// 找到最长的字符串
for (int i = 0; i < 5; i++) {
if (len[i] > max_len) {
max_len = len[i];
max_index = i;
}
}
// 输出最长的字符串及其长度
printf("最长的字符串为:%s\n", str[max_index]);
printf("长度为:%d\n", max_len);
return 0;
}
```
注意,上述代码中使用了C语言标准库的`strlen()`函数来获取字符串长度,需要在程序开头添加`#include <string.h>`头文件。另外,为了避免输入字符串超过数组长度导致的缓冲区溢出,我们限制了每个字符串的长度不超过100。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![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)