Sign in to follow this  
xenoflare2

Checking for repeated characters in a character array

Recommended Posts

I'm having some problems doing this... my plan was to take the last letter in my array and then check it against all of the preceding letters to see if their were any duplicates (example: In the name John Smith it would meet this condition when the cycle compared the third term to the last) and if so change that dupicate to a space ( this is mainly because I already have a function that will remove spaces and change all the letters to caps and so fourth...) But it's not working so well. My code is currently: i=0; j=2; length=strlen(encrypter); k=strlen(encrypter); for (k=length-1; k>=0; k=k-1) { for (i=0; i<length-j; i++) { if (encrypter[k]==encrypter[i]) { encrypter[k]=' '; } j++; i=length-j; } i=0; } It only ever eliminates one letter in the array though...

Share this post


Link to post
Share on other sites
I don't see why you make it so complicated. What is the variable j for?
Try this way:

for (i = 0; encrypter[i] != 0; ++i)
for (j = i + 1; encrypter[j] != 0; ++j)
if (encrypter[i] == encrypter[j])
encrypter[i] = ' ';

I haven't tested this, but it should work.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this