I am working on some code that filters text before it is sent further into a program (this code removes everything but all alphanumeric characters and and underscores), the code itself works perfectly except for the fact that I cannot find a way to store the output of of it for use in other parts of the program, If i had to guess, this probably involves saving the stdout from putchar into a variable, but i cannot find much info for doing so online, if someone could point me in the right direction for this I would really appreciate it, thanks!
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(void) {
int i;
char *p;
char stg[] = "hello";
for (p = &stg[0]; *p != '\0'; p++) {
if (isalnum(*p) || *p == '_') {
putchar (*p);
}
}
putchar ('\n');
return 0;
}
Perhaps I don’t understand your “need” to use
putchar()while doing the filtering, but you can filter the input into an outputarray of charsto use however needed after the filtering as shown below.Output:
EDIT:
And if you want to just filter the string into an array without displaying the string using
putchar()you’d do something like this:And what exactly are you trying to do with the output of the filtered text?