I'm not sure if I'm just being really dumb but this function I have made should take chars from one array and selectively put them in another to remove things like spaces and punctuation. However, what returns seems to just be the first char (from the printf).
char * getWord(char *array) {
char *temp = malloc(sizeof(char) * 20);
int i= 0;
while(i < 20) {
if(validChar(array[i])) {
printf("pass 1 - %c\n", array[i]);
temp[i] = array[i];
printf("pass 2 - %c\n", temp[i]);
i=i+1;
}
else {
i=i+1;
}
}
printf("%s\n", temp);
return temp;
}
The validChar function:
bool validChar(char given) {
char a[]=". ,;:*!?'-\n\r";
for(int q = 0; q <=12; q++) {
if(given == a[q]){
return false;
}
}
return true;
}
In each "pass 2", the correct print is shown so the loop is working as intended. The thing I really don't understand is when I remove the conditional to select valid chars, the final print statment works as intended and prints the entire string.
forloop invalidCharrun only to 11?