logoISU  

CS256 - Principles of Structured Design

Fall 2021

Displaying ./code/cs256su21code/jul06/tolower.c

/*

our implementation of the toLower function in C

this takes a character and returns it's cooresponding lowercase character

*/
#include <stdio.h>


int myToLower(int c);

int main(){

    int ch, newCh;

    printf("Enter a character: ");
    ch = getchar();

    newCh = myToLower(ch);

    printf("%c converted to lowercase is %c\n", ch, newCh);

	return 0;
}

int myToLower(int c){

	if('a' <= c && c <= 'z') //if true, the user already entered a lowercase character
		return c; //just return the character given

	if('A'<= c && c <= 'Z'){ // the user entered an uppercase character

		return c + 32; /* looking at the ASCII table, we see that the ASCII value of the lowercase 
						  character will always be 32 higher than the ASCII value of the uppercase 
						  character, so we can just add 32 to c and return the new character */

	} else { //if we end up here, then the user must have entered neither an uppercase nor a lowercase letter
		return c; //just return the character the user input in.
	}


}