再次返回C newb。我正在尝试K和R C的练习,在尝试练习1-14的路上,我真的很难过。 我的解决方案有效,但并不总是正确的,我正在寻求帮助来改进我所写的内容,或者是否有更好的(更容易理解!)方式! 我的剧本:
#include <stdio.h>
/* How many times a character appears in an array */
main()
{
int c;
int count = 0;
int uniquecount = 0;
char array[20];
array[0] = '\0';
while((c = getchar()) != EOF)
{
array[++count] = c;
}
/* for each element in array,
* check if array[each] in newarray.
* if array[each] in newarray
* break and start checking again.
* if array[each] not in newarray
* add array[each] to end of newarray*/
printf("count = %d\n", count);
array[count] = '\0';
char newarray[count];
newarray[0] = '\0';
for(int a = 0; a < count; ++a)
{
for(int b = 0; b <= a; ++b)
{
if(newarray[b] == array[a])
break;
if(newarray[b] != array[b])
{
newarray[b] = array[b];
++uniquecount;
}
}
}
printf("uniquecount = %d\n", uniquecount);
newarray[uniquecount + 1] = '\0';
printf("array => ");
for(int i = 0; i < count; ++i)
printf("\'%c\'", array[i]);
printf("\n");
printf("newarray => ");
for(int i = 0; i < uniquecount + 1; ++i)
{
if(newarray[i] != '\0')
printf("\'%c\'", newarray[i]);
}
printf("\n");
}
当我尝试一些简单的字符串时,它可以工作,有时它不会:
./times_in_array
this is
count = 9
uniquecount = 5
array => '''t''h''i''s'' ''i''s'' '
newarray => 't''h''i''s'' '
./times_in_array
something comes
count = 16
uniquecount = 11
array => '''s''o''m''e''t''h''i''n''g'' ''c''o''m''e''s'
newarray => 's''o''m''e''t''h''i''n''g'' ''c'
./times_in_array
another goes
count = 13
uniquecount = 12
array => '''a''n''o''t''h''e''r'' ''g''o''e''s'
newarray => 'a''n''o''t''h''e''r'' ''g''o''e''s'
有人可以指导我到哪里错吗?非常感谢!
答案 0 :(得分:1)
获取数组的唯一元素
a) sort it
b) mark element one as unique
v) iterate over it from i = 1 to i = N-1 and mark an element as unique if array[i-1] != array[i]
假设您想要每个重复元素的一个副本。要消除重复元素,请修改算法以查找与上面的元素不相等的元素。
您的代码遭受错误的字符串比较。
答案 1 :(得分:1)
我们通常会写int main(void)
而不是main()
。
此外,了解当您访问新阵列时非常重要:
newarray[b]
您实际上正在访问未初始化的内存。但是,由于具有匹配array[a]
的随机字符的概率太小,所以你似乎可以逃脱它。
因此我建议您初始化新阵列,如下所示:
for(int i = 0; i < count; ++i)
newarray[i] = '\0';
现在,我坚持让你明白你做错了什么,而不是提供我的解决方案。这就是你教育自己的方式。在内循环开始时打印数据非常有用,例如:
printf("b = %d, a = %d, newarray[b] = %c, array[a] = %c, array[b] = %c\n", b, a, newarray[b], array[a], array[b]);
并在增加唯一计数器时打印一条消息,如下所示:
printf("UNIQUE, %d\n", uniquecount);
执行程序时,您会看到:
...
b = 9, a = 12, newarray[b] = g, array[a] = s, array[b] = g
b = 10, a = 12, newarray[b] = , array[a] = s, array[b] = o
UNIEUQ, 10
b = 11, a = 12, newarray[b] = , array[a] = s, array[b] = e
UNIEUQ, 11
b = 12, a = 12, newarray[b] = , array[a] = s, array[b] = s
UNIEUQ, 12
uniquecount = 12
array => '''a''n''o''t''h''e''r'' ''g''o''e''s'
newarray => 'a''n''o''t''h''e''r'' ''g''o''e''s'
强烈暗示你出了什么问题。在&#39;之后(最后一个字母)第一次被发现是唯一的(这很好),我们不打破循环,因此你的代码将继续检查新数组并被欺骗这个&#39; S&#39;再次独一无二。
所以当你找到一个独特的元素时添加一个休息时间,你现在应该没事了:
++uniquecount;
break;