C language provides toLower() and toUpper() functions in ctype.h to convert a single character to its lowercase or uppercase equivalent respectively.
We can use these functions in a loop over the characters in the string to convert it to lowercase or uppercase.
- tolower(ch) : Converts the character to lowercase
- toupper(ch) : Converts the character to uppercase
Example :
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> void convertToLowercase(char *); void convertToUppercase(char *); int main(void) { convertToLowercase("Hello"); convertToUppercase("World"); return EXIT_SUCCESS; } void convertToLowercase(char* str) { int i; for (i = 0; i <= strlen(str); i++) { str[i] = tolower(str[i]); } printf("String Hello converted to Lowercase is %s ", str); } void convertToUppercase(char* str) { int i; for (i = 0; i <= strlen(str); i++) { str[i] = toupper(str[i]); } printf("\nString World converted to Uppercase is %s ", str); }
Output :
String Hello converted to Lowercase is hello
String World converted to Uppercase is WORLD
© 2017, https:. All rights reserved. On republishing this post, you must provide link to original post