-1

I would like to check if the first letter of a string is before or after the letter t in the alphabet.

For example, the user inputs "Brad" and it would print

"Your name starts with a letter that comes before "t"."

Something of that sort.

user6720691
  • 45
  • 1
  • 8
  • if your input is in variable In, you can check it like this `if (In[0] < 't')` – Prasanna Oct 07 '16 at 03:20
  • Hint: remember that characters are really numbers, for example capitol A, 'A' is 0x41. With that in mind, take a look at the ASCII character table (see http://www.asciitable.com/) and see if you can't think of a way to determine that 'B' (0x42) comes before t (0x74). – thurizas Oct 07 '16 at 03:21
  • But beware of the fact that there can be upper and lower cases. For that just convert everything to upper or lower before comparing – Prasanna Oct 07 '16 at 03:21
  • There is no guarantee letters have adjascent codes or even alphabetically ordered. It depends on your execution character set. – too honest for this site Oct 07 '16 at 13:08

2 Answers2

1

If your string is:

char str[10] = "Brad"

you can compare:

if (str[0]) < 't') {
    ...

which will evaluate to 1 (true) if 'B' is before the character 't' in the ASCII character set. Note that this comparison is case-sensitive, so you want to convert the characters that you are comparing to the same case for this to be meaningful. You can use the toupper() and tolower() functions from the ctype.h library to accomplish this. C treats chars as integer types, so you can perform mathematical operations with them.

Most introductory texts on C solve this problem the same way, but as @Olaf points out, the standard does not guarantee what values represent particular characters. So, when portability is a concern, you need to be more careful. That said, most systems use either ASCII or UTF-8, which is a superset of ASCII (they are identical for the first 128 characters), making this simple solution a reasonable place to start.

ad absurdum
  • 17,836
  • 5
  • 33
  • 54
  • There is no guarantee letters have adjascent codes or even alphabetically ordered. It depends on your execution character set. – too honest for this site Oct 07 '16 at 13:08
  • @Olaf, I am not exactly sure why these answers deserved downvotes. I think that they were pitched at the level of the perceived question. Your point is well-taken, though-- I have updated my answer to address your valid concerns about portability. – ad absurdum Oct 07 '16 at 18:10
  • Well, I removed the DV. Please uinderstand it is sometimes not well percieved to criticise on a DV. Just edit and address a comment to a commentor if there is one. Note: there is no requirement to leave a comment for a DV. I tend to do, just because I hope to push the poster to enhance the post. Which you did. – too honest for this site Oct 07 '16 at 18:25
  • @Olaf-- noted. Thank you for the comments, they did inspire me to look further into the matter. – ad absurdum Oct 07 '16 at 18:28
0

Something like below (add checks for null and if it the first character is t itself, etc).

#include <stdio.h>
#include <stdlib.h>

/* t's value in ASCII */
#define LETTER_T 116

int main(int argc, char * argv[])
{
      /* Use toupper or tolower functions to handle casing */
      char * str = "brad"; 
      char * result = (str[0] < LETTER_T) ? "before" : "after";
      printf("%s starts with a %c that comes %s the letter t.", str, str[0], result);
      return 0;
}
lolololol ol
  • 754
  • 1
  • 7
  • 17
  • There is no guarantee letters have adjascent codes or even alphabetically ordered. It depends on your execution character set. – too honest for this site Oct 07 '16 at 13:08
  • @Olaf This was meant to be a simple example, not a robust solution taking into account different character sets than ASCII (i.e. Unicode, UTF-8, etc). The answer is even qualified by 'something like below'. – lolololol ol Oct 07 '16 at 17:01
  • That does not even imply the solution can be completely off (as it would be for e.g. EBCDIC). Btw. using magic numbers is a no-go. Any reason you don't use the integer `'t'`? – too honest for this site Oct 07 '16 at 17:03