Question 4 Write a function which accepts a character as a parameter and

Question 4 write a function which accepts a character

This preview shows page 8 - 10 out of 10 pages.

Question 4 Write a function which accepts a character as a parameter and returns true if it is a digit and false, otherwise. Solution bool isDigit(char ch) { if (ch >= '0' && ch <= '9') return true; return false; } Question 5 Write a function which accepts a lowercase letter as a parameter and changes the lowercase letter to its equivalent uppercase letter. Solution char lowerToUpper(char ch) { if (isLowerCaseLetter(ch)) ch = ch - 32; return ch; } To convert from an upper case to a lowercase letter, we simply add 32: char upperToLower(char ch ){ if (isUpperCaseLetter(ch)) ch = ch + 32; return ch; }
Background image
9 Finding Position of a Letter in the Alphabet Suppose we have a character which represents a letter in the alphabet. We would like to know what is the position of the letter in the alphabet (a value between 1 and 26). Now, both 'a' and 'A' have a position of 1 in the alphabet. The character ‘a’ is represented 97 and the character ‘A’ is represented by 32 in the ASCII table. Can we use the integer value of each letter to help us find the position of that letter in the alphabet? Starting from ‘A’, we already know that we can find ‘B’ by adding 1. Now, ‘B’ – ‘A’ is (66 – 65) or 1. But, ‘B’ is in position 2 of the alphabet (i.e., add 1 to the subtraction). Similarly, ‘C’ – ‘A’ is (67 – 65) or 2, but ‘C’ is in position 3 of the alphabet (i.e., add 1 to the subtraction). Also, ‘D’ – ‘A’ is (68 – 65) or 3, but ‘D’ is in position 3 of the alphabet. After a while, it can be observed that the position of any letter in the alphabet can be found by subtracting ‘A’ and then adding 1. For example, char c; int position; c = 'E'; position = c – 'A' + 1; cout << "Position of " << c << " is " << position << endl; A similar approach can be used to find the position of any lowercase letter in the alphabet. However, instead of subtracting uppercase ‘A’, we subtract lowercase ‘a’: char c; int position; c = 'e'; position = c – 'a' + 1; cout << "Position of " << c << " is " << position << endl; Question 6 Write a function, getPosition, which given a character as a parameter, returns 0 if the character is not a letter of the English alphabet; otherwise, it returns the position of the letter in the alphabet. For example,
Background image
10 given the letter ‘G’ it returns 7. The function should work if the character is either an uppercase or a lowercase letter. Solution int getPosition(char c) { int pos = 0; if (isLetter(c)) { if (isLowerCaseLetter(c)) pos = c - 'a' + 1; else pos = c - 'A' + 1; } return pos; }
Background image

You've reached the end of your free preview.

Want to read all 10 pages?

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes