Hi, I have made a function that takes a pointer to a string, and the strings length and cleans out all the rubbish and adds a terminating null so that I can use the string without possible problems.
The function works fine appart from the code to copy the new string back over-writing the old one at the end. I think I have gone about it in the wrong way but strings confuse me a bit still.
Here is the function, but I am sure its the stuff under the "Now put the cleaned up string back again" comment thats the problem.
Code:
void fix_string(int length, char *old_string)
{
int i, j;
char new_string[MAX_NAME_LENGTH];
/* Loose the extra off the end of a long name */
if (length >= MAX_NAME_LENGTH)
length = MAX_NAME_LENGTH;
/* Put only the letters and numbers into the new name */
for (i = 0, j = 0; i < length; i++)
if( isalnum(old_string[i]) )
new_string[j++] = old_string[i];
else
printf("Woopdy blody do\n");
/* Add the terminating null character */
new_string[j] = '\0';
printf("test: %s\n", new_string);
/* Now put the cleaned up string back again */
while (new_string[i] != '\0')
{
old_string[i] = new_string[i];
i++;
}
old_string[i] = '\0';
}
Thanks