Problem making a function to remove white space and punctuation from a character array

Hello,

I'm trying to create a function that takes in an array of characters with a string, for example ENCRYPT THIS MESSAGE. I have read a few other topics on this site about people doing the same thing but none of the answers worked for me. So I tried to implement a function that takes in the character array and returns a new character array without spaces or punctuation. Here is the function I have so far:

1
2
3
4
5
6
7
8
9
  void RemovePunct(char old_String[], char new_String[]){
        for (int i = 0; i != 50; i++)
        {
		if(isspace(old_String[i])==true) continue;
		if(ispunct(old_String[i])==true) continue;
                if(isalnum(old_String[i])) new_String[i] = old_String[i]; 
        }
        return;
}


When I run the example string through this function it returns "ENCRYPTSTHIS▒MESSAGEE", "ENCRYPTTHIS▒MESSAGE", "ENCRYPTTHIS▒MESSAGEE", or some similar variation with extra letters or special characters. Sometimes it removes one space successfully but the other space is always a letter or some weird character. Anyone have any ideas what is wrong with this function or how to make it work successfully? Thanks in advanced for any help.
The trick is to remember that you need two indices: one for the old string and one for the new string.

Every time you look at a character in the old string you'll add one to it's index.

But only when you copy a value to the new string do you want to add one to the new string's index.

That is, the indices will not necessarily be the same!

Hope this helps.
Topic archived. No new replies allowed.