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;
Username: User email: User first name: User last name: User display name: User ID: 0

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

Notify of
1 Comment
Newest Most Voted
Inline Feedbacks
View all comments