Jump to: navigation, search

The C Programming Language, 2nd Edition, by Kernighan and Ritchie
Exercise 2.10 on page 52

Rewrite the function lower, which converts upper case letters to lower case, with a conditional expression instead of if-else .



Solution by Vidhan Gupta

/* Rewrite the function lower, which convers upper case letters to lower case,
with a conditional expression instead of if-else. */
#include <stdio.h>

// Function prototype of lower
int lower(int c);

int main()
{
    // Test lower function
    int c = 'Q';
    printf("%c", lower(c));
    return 0;
}

// Function that converts UPPER case character to lower case character
int lower(int c)
{
    return (c >= 'A' && c <= 'Z') ? c += 'a' - 'A' : c;
}

OUTPUT:

q

Solution by Bryan Williams

/*

  Exercise 2-10. Rewrite the function lower, which converts upper case letters
		 to lower case, with a conditional expression instead of if-else.



  Assumptions : by conditional expression they mean an expression involving a ternary operator.

  Author: Bryan Williams 

*/

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

#define TEST
#define ORIGINAL            0
#define SOLUTION            1
#define PORTABLE_SOLUTION   0


/*
   ok, the original routine we are trying to convert looks like this.. 
*/
#if ORIGINAL
/* lower: convert c to lower case; ASCII only */
int lower(int c)
{
  if(c >= 'A' && c <= 'Z')
    return c + 'a' - 'A';
  else
    return c;
}
#endif

/*
   the natural solution for simply making this a conditional (ternary) return instead of an 
   if ... else ...
*/
#if SOLUTION
/* lower: convert c to lower case; ASCII only */
int lower(int c)
{
  return c >= 'A' && c <= 'Z' ? c + 'a' - 'A' : c;
}
#endif



/*
   the more portable solution, requiring string.h for strchr but keeping the idea of a 
   conditional return.  
*/
#if PORTABLE_SOLUTION
/* lower: convert c to lower case */
int lower(int c)
{
  char *Uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  char *Lowercase = "abcdefghijklmnopqrstuvwxyz";
  char *p = NULL;

  return NULL == (p = strchr(Uppercase, c)) ? c : *(Lowercase + (p - Uppercase));
}
#endif



/*
    ok, this bit is just a test driver... exclude as required
*/
#ifdef TEST
int main(void)
{
  char *Tests = "AaBbcCD3EdFGHgIJKLhM2NOjPQRkSTlUVWfXYf0Z1";
  char *p = Tests;
  int Result = 0;  

  while('\0' != *p)
  {
    Result = lower(*p);
    printf("[%c] gives [%c]\n", *p, Result);
    ++p;
  }

  /* and the obligatory boundary test */
  Result = lower(0);
  printf("'\\0' gives %d\n", Result); 

  return 0;
}

#endif

Solution by Pilcrow

/***********************************
Elementary, Watson
-- Pilcrow
won't work on EBCDIC machines, ;-(
***********************************/

#include <stdio.h>

unsigned char llower(char);

int main(void)
{
    int i;
    char test[] = "AaBbCcdDeE1234ZzyY";
    i = 0;
    puts(test);
    while(test[i] != '\0')putchar(llower(test[i++]));
    putchar('\n');
}

unsigned char llower(char x)
{
    return (x >= 'A' && x <= 'Z') ? x = x - 'A' +'a' : x;
}

Solution by Jrun

#include <stdio.h>

void lower(char s[]);

int main() {
  char s[] = "HelLo WorlD!";
  lower(s);
  printf("%s\n", s);
  return 0;
}

void
lower(char s[]) {
  int c, i;
  c ^= c;
  i ^= i;
  /* subtlety of Precedence and Order of Evaluation, title of the next
   * chapter had me on this loop. experiment with having i incremented
   * in the while line rather than what is now. good preparation for the
   * next chapter. */
  while((c = s[i]) != '\0')
 /* preceding sub-chapter is about assignmet operators
  * so this seems more appropriate */
    (c >= 'A' && c <= 'Z') ? s[i++] += 'a' - 'A' : i++ ;
}

Solution by j7miran

/******************
-j7miran
*******************/

#include <stdio.h>

void main()
{
	int c = 100;
	printf("The ASCII lower case value of %d is %c\n", c, lower(c));
}

//lower convert c to lower case; ASCII only

/*
int lower(int c)
{
	if (c >= 'A' && c <= 'Z')
	{
		return c + 'a' - 'A';
	}
	else
	{
		return c;
	}
}
*/

//version 2 using ternary operator
int lower(int c)
{
	return (c >= 'A' && c <= 'Z') ? (c + 'a' - 'A') : c;
}


Solution by Luke Panayi

#include <stdio.h>

int lower(int c)	{
	return (c >= 'A' && c <= 'Z') ? c + 'a' - 'A' : c;
}

int main()	{
	printf("%c\n", lower('A'));
	return 0;
}
Personal tools