How to remove all non-alphanumeric characters from a string in C

1 Answer

0 votes
#include <stdio.h>
#include <ctype.h>
#include <string.h>
 
char* remove_all_non_alphanumeric_characters(char* str) {
    char cleanedStr[1024];
    int j = 0;
    for (int i = 0; str[i]; i++) {
        if (isalnum(str[i])) {
            cleanedStr[j++] = str[i];
        }
    }
    cleanedStr[j] = '\0';
 
    strcpy(str, cleanedStr);
     
    return str;
}
 
int main() {
    char str[] = "C 99:is: a, general-purpose (programming) language.";
     
    printf("%s\n", remove_all_non_alphanumeric_characters(str));
 
    return 0;
}
  
  
/*
run:
  
C99isageneralpurposeprogramminglanguage
 
*//

 



answered Jul 1, 2024 by avibootz
edited 3 days ago by avibootz

Related questions

...