有人可以用一段代码帮助我吗? [英] Could someone help me with a fragment of code?

查看:67
本文介绍了有人可以用一段代码帮助我吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

全部晚上,



对于作业,我必须编写一个例程,删除同一个字符的所有第二次出现。例如,如果我输入apple,输出将是aple,其中只有字母表中的每个字母中的一个在输出内。

另一个例子:aaabbb给出ab。





我写了这段代码,我不明白为什么它不起作用。那些对c有更多了解的人可以帮个忙吗?谢谢。











Evening all,

For an assignment I have to write a routine that will delete all the second occurrence of the same character. For example if i input apple the output will be aple where only one of each letter from the alphabet is within the output.
another example: aaabbb gives ab.


I have written this code which I can't see why it isnt working. Could someone with a bigger knowledge of c give me a hand? Thank you.





void dublicateRemoved( char key[] )
	{
		int letterFoundSame = 0;
		for(int i = 0; i < 16; i++) //Go through each letter key // 0 to 15
   		{           	
      		for(int x = 0; x <= i; x++) //Go through all letters so far.
         		{
            		if(key[i] == cutKey[x]) //True at anypoint then we disgard it as we want no dublicates.
               		letterFoundSame = 1;
                  else
                  	letterFoundSame = 0;
            	}
       		//So if a letter has been found to be the same we dont want to add the letter to the list     
       		if(letterFoundSame == 0) //Not been repeated
         		cutKey[i] = key[i]; 
      	}
}

推荐答案

如果N是最大长度,请制作两个char数组,比如A [N]和B [N]。

在迭代A时,使用辅助函数检查char c是否在B中:

如果不将c添加到B并增加索引。继续直到c = A [i]为空(== 0)
if N is max length, make two char arrays, say A[N] and B[N].
while iterating through A, make helper function to check if char c is in B:
if not add c to B and increment index. keep going until c=A[i] is null (==0)


你的cutKey数组需要它自己的索引 - 所以你有



Your cutKey array needs its own index - so you have

if(letterFoundSame == 0) //Not been repeated
                cutKey[cutKeyIndex++] = key[i];





那么你的'通过所有字母到目前为止'循环使用cutKeyIndex(不是我)作为其限制。



你在测试中不需要其他 - 只需在内循环之前设置为0然后









then your 'go through all the letters so far' loop uses the cutKeyIndex (not i) as its limit.

You don't need an else in the test - just set to 0 before the inner loop then



if(key[i] == cutKey[x]) //True at anypoint then we disgard it as we want no dublicates.
{
                    letterFoundSame = 1;
                    break;
}





否则你找到一个匹配,将标志设置为1,然后继续查看下一个字符,找不到匹配,并将其设置回0。



otherwise you find a match, set the flag to 1, then keep looking at the next character, find no match, and set it back to 0.


这篇关于有人可以用一段代码帮助我吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆