How to extract only characters, numbers and spaces from a char array in C

1 Answer

0 votes
#include <stdio.h> 
#include <string.h> 
#include <stdlib.h> 
#include <ctype.h> 
  
void extract_characters_numbers_spaces(char *s, int len, char *result) { 
    int result_i = 0;
      
    for (int i = 0; i < len; i++) { 
         char ch = s[i]; 
         if (isalpha(ch) || isdigit(ch) || ch == ' ') 
            result[result_i++] = ch; 
    } 
    result[result_i] = '\0';
} 
     
int main() 
{ 
    char arr[] = "c++14$vb.net&%java*() php <>/python 3.7.3"; 
    char *result = (char *)malloc(strlen(arr) + 1 * sizeof(char));
      
    extract_characters_numbers_spaces(arr, strlen(arr), result);
      
    puts(arr);
    puts(result);
      
    free(result);
       
    return 0; 
} 
     
     
     
/*
run:
     
c++14$vb.net&%java*() php <>/python 3.7.3
c14vbnetjava php python 373
   
*/

 



answered Aug 14, 2019 by avibootz
edited Aug 14, 2019 by avibootz

Related questions

...