Arrays 如何删除字符数组中的所有重复字符
我想从数组中删除所有重复的字符。下面是一个例子Arrays 如何删除字符数组中的所有重复字符,arrays,c,Arrays,C,我想从数组中删除所有重复的字符。下面是一个例子 "aabccdee" 我在用C语言做这个。仅使用数组、循环、if、else(条件语句),不使用指针 #包括 int main(){ charc[10]; char com[10]={0,}; 字符结果[10]={0,}; int-cnt=0; 对于(int i=0;i
"aabccdee"
我在用C语言做这个。仅使用数组、循环、if、else(条件语句),不使用指针
#包括
int main(){
charc[10];
char com[10]={0,};
字符结果[10]={0,};
int-cnt=0;
对于(int i=0;i<10;i++){
scanf(“%c”、&c[i]);
}
对于(int i=0;i<10;i++){
对于(int j=i+1;j<10;j++){
如果(c[i]==c[j]){
com[i]=c[i];
cnt++;
printf(“%c”,com[i]);
}
}
}
对于(int i=0;i
我以为
如何删除所有重复字符?首先,在我们发言之前,您必须检查 在使用scanf扫描字符时,需要添加空格 所以 变成
scanf(" %c", &c[i]);
其次,您的想法有点混乱,因为结果显示您只处理案例,而没有继续验证整个阵列。你需要学习
你的问题是,当你移动你的表格(不是完全)时,你仍然打印出这个尺寸
因此,基本上,您的代码应该是这样的:
#include<stdio.h>
int main() {
char c[10];
int length=5;
for (int i = 0; i < 5; i++) {
scanf(" %c", &c[i]);
}
int j,k,i;
for(i=0; i<length; i++)
{
for(j=i+1; j<length; j++)
{
if(c[i] == c[j])
{
length--;
for(k=j; k<length; k++)
{
c[k] = c[k + 1];
}
j--;
}
}
}
printf("\n");
for (int i = 0; i < length; i++) {
printf("%c", c[i]);
}
}
#包括
int main(){
charc[10];
整数长度=5;
对于(int i=0;i<5;i++){
scanf(“%c”、&c[i]);
}
int j,k,i;
对于(i=0;i<P<好,这样做是明目张胆地回答家庭作业,但我很少这样做,认为这是一项有趣的任务。当然,没有对效率的要求,但它看起来对我来说是有效的。据我所知,第一个和最后一个病例是拐角病例,所以我单独处理这些问题,并在中间使用一个循环。e不允许使用strlen
,那么您可以使用自己的方法或其他方法,这不是此问题的主要焦点(最好使用命令行参数中的字符串)
欢迎使用Stack overflow!好的,首先,如果您有类似于
“aba”
的东西要删除吗'a'
或者只有当它重复时才删除它,例如aab
我想删除所有重复的元素。只剩下不重复的元素:)@lmeguran在两个嵌套循环中,检查是否有另一个值。当然,在i==j的情况下,您不应该进行比较。如果没有其他值,则添加它。如果顺序不重要,您可以对数组进行排序,并在排序后挤压所有重复项,因为它们都在一起。此代码也起作用,但我希望删除所有重复项元素。只留下不重复的元素。非常感谢。我已经用另一种方法解决了这个问题。哈哈。但真的很感激
scanf(" %c", &c[i]);
#include<stdio.h>
int main() {
char c[10];
int length=5;
for (int i = 0; i < 5; i++) {
scanf(" %c", &c[i]);
}
int j,k,i;
for(i=0; i<length; i++)
{
for(j=i+1; j<length; j++)
{
if(c[i] == c[j])
{
length--;
for(k=j; k<length; k++)
{
c[k] = c[k + 1];
}
j--;
}
}
}
printf("\n");
for (int i = 0; i < length; i++) {
printf("%c", c[i]);
}
}
#include <stdio.h>
#include <string.h>
int main(void)
{
char source[] = "aabccdee";
char result[sizeof(source)] = { 0 };
unsigned resultIndex = 0;
unsigned i = 0;
// do this to avoid accessing out of bounds of source.
if (strlen(source) > 1)
{
// handle the first case, compare index 0 to index 1. If they're unequal, save
// index 0.
if (source[i] != source[i+1])
{
result[resultIndex++] = source[i];
}
// source[0] has already been checked, increment i to 1.
i++;
// comparing to strlen(source) - 1 because in this loop we are comparing the
// previous and next characters to the current. Looping from 1 to second-to-the-
// last char means we stay in bounds of source
for ( ; i < strlen(source) - 1; i++)
{
if (source[i-1] != source[i] && source[i] != source[i+1])
{
// write to result if curr char != prev char AND curr char != next char
result[resultIndex++] = source[i];
}
}
// handle the end. At this point, i == the last index of the string. Compare to
// previous character. If they're not equal, save the last character.
//
if (source[i] != source[i-1])
{
result[resultIndex] = source[i];
}
}
else if (strlen(source) == 1)
{
// if source is only 1 character, then it's trivial
result[resultIndex] = source[i];
}
else
{
// source has no length
fprintf(stderr, "source has no length.\n");
return -1;
}
// print source and result
printf("source = %s\n", source);
printf("result = %s\n", result);
return 0;
}
source = "aabccdee"
result = "bd"
source = "aaee"
result =
source = "a"
result = "a"
source = "abcde"
result = "abcde"
source = "abcdee"
result = "abcd"
source = "aabcde"
result = "bcde"
source = "aaaaaaaaaaaabdeeeeeeee"
result = "bd"
source = ""
source has no length.