Removing character from array using strchr, does not return right value in plain C
-
29-10-2019 - |
Question
I am trying to remove first semicolon from a character arrary whose value is:
Input:
; Test: 876033074, 808989746, 825766962, ; Test1: 825766962,
Code:
char *cleaned = cleanResult(result);
printf("Returned BY CLEAN: %s\n",cleaned);
char *cleanResult(char *in)
{
printf("Cleaning this: %s\n",in);
char *firstOccur = strchr(in,';');
printf("CLEAN To Remove: %s\n",firstOccur);
char *restOfArray = firstOccur + 2;
printf("CLEAN To Remove: %s\n",restOfArray); //Correct Value Printed here
char *toRemove;
while ((toRemove = strstr(restOfArray + 2,", ;"))!=NULL)
{
printf("To Remove: %s\n",toRemove);
memmove (toRemove, toRemove + 2, strlen(toRemove + 2));
printf("Removed: %s\n",toRemove); //Correct Value Printed
}
return in;
}
Output (first semicolon still there):
; Test: 876033074, 808989746, 825766962; Test1: 825766962;
No correct solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow