1

I just finished the Caesar program and it works, but when I run style50 it complains that this line is too long:

if ((text[i] >= 'a' && text[i] <= 'z') || (text[i] >= 'A' && text[i] <= 'Z'))

I'd rather just have one if statement, not two because I find two statements a bit more confusing (especially since I have it already nested in another if statement).

Any suggestions on how to make it shorter? I was thinking about substituting text[i] with just a one letter char variable, but I think that would make it more cryptic in the end.

1

Well, one way is to use the functions isupper() and islower()

if (islower(text[i]) || isupper(text[i]))

alternatively, even shorter is using the isalpha() function

if (isalpha(text[i]))  

which returns true if the char is alphabetic (ie, upper or lower).

You'll need to #include <ctype.h> to use those functions.

Hope that helps! Brenda.

1
  • Thanks Brenda! It's really nice to know about the two functions you've shown. I can keep both in mind for another program. – Jonobugs Jun 20 '14 at 1:27
2

This is quite simple to do, as all you really are doing through the line is test whether or not the the letter is even a letter, which can simply be checked by C's own function isalpha(text[i]).

For more, simply execute "man isalpha" at the terminal and read the manual page for the function, so, now, you long line if ((text[i] >= 'a' && text[i] <= 'z') || (text[i] >= 'A' && text[i] <= 'Z')) can simply be shortened to if (isalpha(text[i]) == 1) or if (isalpha(text[i])). And be sure you are #including ctype.h as instructed by the man page.

Happy Coding!

3
  • Isn't that exactly what I suggested? :) – curiouskiwi Jun 19 '14 at 7:43
  • Hmmm,Just don't have the habit of reading other's answers before making mine, so,yeah, you suggested the same thing, maybe in a easier way, so sorry :( – Nib Jun 19 '14 at 8:04
  • Great! Thanks for the great explanation. I had no idea that function existed. I hope you don't mind, but I have the check answer mark to Brenda as she answered first, but I READ your answer first...and I appreciate the well thought out answer. – Jonobugs Jun 20 '14 at 1:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .