Search code examples
clowercase

Why is the function not returning lowercase letters?


I'm currently new to C so how things work is still slightly alien to me as I come from an OOP background of Java. I'm trying to get an input from the terminal and then turn it into all lowercase letters however it seems the function isn't working. Any tips if possible?

#include <stdio.h>

int lower(int c){
  if (c >= 'A' && c <= 'Z')
    return c + 'a' - 'A';
  else
    return c;
}

int main(){
  int c;
  while ((c = getchar()) != EOF){
    lower(c);
    putchar(c);
  }
}


Solution

  • The code should use the return values of lower:

    putchar(lower(c));
    

    You may also like to use the standard library function tolower.