Upper case letters to lower case in C language

Rewrite the function lower, which converts upper case letters to lower case, with a conditional expression instead of if-else.

/* Function lower(c) using conditional expression */ #include<stdio.h> int lower(int c); int main(void) { int c; while ((c = getchar()) != EOF) { putchar(lower(c)); } } int lower(int c) { return c >= 'A' && c <= 'Z' ? c + 'a' - 'A' : c; }

In this program we are going to convert upper case letters to lower case. We declare a function called lower in the beginning of the program. When the program enters the while loop it each character it gets to lower:

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

The lower function checks for all the uppercase characters and prints everything in the lowercase. It does this by a conditional statement, where if a upper case character is found, it subtracts ‘A’ to get relative index, adds it to ‘a’ to return corresponding smaller case character, if a lower case character is found, it is simply returned:

return c>='A' && c<='Z'? c+'a'-'A':c;

This article needs improvement. You can help improve this article. You can also write similar articles and help the community.

6 Comments on “Upper case letters to lower case in C language

  1. # Program to check if a number is prime or not
    
    num = 407
    
    # To take input from the user
    #num = int(input("Enter a number: "))
    
    # prime numbers are greater than 1
    if num > 1:
       # check for factors
       for i in range(2,num):
           if (num % i) == 0:
               print(num,"is not a prime number")
               print(i,"times",num//i,"is",num)
               break
       else:
           print(num,"is a prime number")
           
    # if input number is less than
    # or equal to 1, it is not prime
    else:
       print(num,"is not a prime number")