Search code examples
cgetchar

Storing words read from a file excluding special characters


I am having trouble storing the strings correctly in this bit of code. I am excluding any special characters using isalnum(). The issue I am trying to overcome is that this method is also storing empty strings as well. I'm trying to find a way to iterate over special characters without storing them into a string.

char* readWords(void) {
  char str[max];
  int c;
  int i = 0;

  while ((c = getchar()) != EOF){
    if(isalnum(c)){
      str[i] = c;
      i++;
    } else {
      return stringStore(str,i); // i is the length of the string
      i = 0;
    }
  }
  return NULL;
}

Solution

  • if(i!=0)return stringStore(str,i);
    

    also

    if(i!=0)return stringStore(str,i); else return NULL;
    

    – BLUEPIXY