Tolower

From aldeid
Jump to navigation Jump to search

Syntax

int tolower ( int c );

Description

Convert uppercase letter to lowercase

Converts c to its lowercase equivalent if c is an uppercase letter and has a lowercase equivalent. If no such conversion is possible, the value returned is c unchanged.

Notice that what is considered a letter may depend on the locale being used; In the default "C" locale, an uppercase letter is any of: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z, which translate respectively to: a b c d e f g h i j k l m n o p q r s t u v w x y z.

In other locales, if an uppercase character has more than one correspondent lowercase character, this function always returns the same character for the same value of c.

In C++, a locale-specific template version of this function (tolower) exists in header <locale>.

Parameters

c
Character to be converted, casted to an int, or EOF.

Return Value

The lowercase equivalent to c, if such value exists, or c (unchanged) otherwise.

The value is returned as an int value that can be implicitly casted to char.

Example

Source Run
#include <stdio.h>
#include <ctype.h>

int isVowel(char c);

int main(int argc, char *argv[])
{

  FILE *pFile = NULL;
  int c = 0;
  int countVowels = 0;

  pFile = fopen("pets", "r");

  if (pFile==NULL) perror("Error opening file\n");
  else {
    do {
      c = fgetc(pFile);
      if(isVowel(c)) countVowels++;
    } while (c != EOF);
    printf("The file contains %d vowels\n", countVowels);
    fclose(pFile);
  }

  return 0;
}

int isVowel(char c)
{
  c = tolower(c);
  if (c=='a' || c== 'e' || c=='i' || c=='o' || c=='u' || c=='y')
    return 1;
  else
    return 0;
}
$ cat pets
You have 3 pets and prefer cats.
$ ./code 
The file contains 10 vowels