C library - towlower() function



The C wctype library towlower() function is used to convert the given wide character to the lower case, if possible.

This function can be useful for case insensitive comparison, text normalization, or user input processing.

Syntax

Following is the C library syntax of towlower() function −

wint_t towlower( wint_t wc );

Parameters

This function accepts a single parameter −

  • wc − It is a wide character of type 'wint_t' to be converted into lower case.

Return Value

This function returns lowercase character if the wide character has changed to a lowercase character, otherwise unchanged character (if it is already lowercase or not an alphabetic character)

Example 1

The following is the basic c example that demonstrate the use of towlower() function.

#include <wchar.h>
#include <wctype.h>
#include <stdio.h>

int main() {    
   // wide character
   wchar_t wc = L'G';
   // convert to lowercase
   wint_t lower_wc = towlower(wc);
   
   wprintf(L"The lowercase equivalent of %lc is %lc\n", wc, lower_wc);
   return 0;
}

Output

Following is the output −

The lowercase equivalent of G is g

Example 2

We create a c program to compare strings are equal or not. after converting to lowercase using the towlower(). If both strings are equal, it means case insensitive, otherwise sensitive.

#include <wchar.h>
#include <wctype.h>
#include <stdio.h>
#include <string.h>

int main() {
   wchar_t str1[] = L"Hello World";
   wchar_t str2[] = L"hello world";
   
   // flag value
   int equal = 1;

   // comepare both string after compare
   for (size_t i = 0; i < wcslen(str1); ++i) {
      if (towlower(str1[i]) != towlower(str2[i])) {          
         // strings are not equal
         equal = 0;
         break;
      }
   }

   if (equal) {
      wprintf(L"The strings are equal (case insensitive).\n");
   } else {
      wprintf(L"The strings are not equal.\n");
   }

   return 0;
}

Output

Following is the output −

The strings are equal (case insensitive).

Example 3

The below example, normalized the wide character into a lowercase character.

#include <wchar.h>
#include <wctype.h>
#include <stdio.h>

int main() {
   // Wide string 
   wchar_t text[] = L"Normalize THIS Tutorialspoint!";
   size_t length = wcslen(text);

   wprintf(L"Original text: %ls\n", text);

   // Normalize the text to lowercase
   for (size_t i = 0; i < length; i++) {
      text[i] = towlower(text[i]);
   }
   wprintf(L"Normalized text: %ls\n", text);
   
   return 0;
}

Output

Following is the output −

Original text: Normalize THIS Tutorialspoint!
Normalized text: normalize this tutorialspoint!
c_library_wctype_h.htm
Advertisements