422,764 Members | 1,289 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 422,764 IT Pros & Developers. It's quick & easy.

c language for loop and ascii input1 output= A then small letters a=27 so on

P: 0
A = 1 so on then small letters a = 27 using ascii please help thank you so much
Mar 9 '18 #1
Share this Question
Share on Google+
9 Replies

Expert 100+
P: 921
Expand|Select|Wrap|Line Numbers
  1. #include "stdio.h"
  3. int main() {
  4.         int i=1;
  5.         while (i<=26) {
  6.                     printf("%c=%i\n", (char)(i+64), i);
  7.                     i=i+1;
  8.         }
  9.         return 0;
  10. }
It's only doing the capitals

But, I wonder, what are you going to do with this piece of code that you probably do not understand?

Or can you show everybody here how to expand this piece of code to do also the lower-case letters?

And, finally, yes, I am no a C-programmer ... ;)
Mar 11 '18 #2

Expert 100+
P: 2,373
The C Standard does not mandate ASCII encoding for characters. Code that assumes ASCII encoding is not portable. Perhaps the assignment requires you to make this assumption.

On the other hand, you could iterate an int variable from CHAR_MIN to CHAR_MAX (from limits.h), and then use character classification functions (from ctype.h) to identify, for example, alphabetic, uppercase, and lowercase characters, and finally print the decoded and encoded values for characters that make it through the classification filter.

Why did I say int variable rather than char variable? Because (1) the int range is a superset of the char range (that is, a char can be assigned to an int without any loss of sign or precision); and (2) the character classification functions take an int argument.
Mar 16 '18 #3

Expert 100+
P: 921
Expand|Select|Wrap|Line Numbers
  1. #include "stdio.h"
  2. #include "limits.h"
  4. void main() {
  5.         int c=0;
  6.         int i=CHAR_MIN;
  7.         while (i<=CHAR_MAX) {
  8.                     if (islower(i) || isupper(i)) {
  9.                         printf("%c=%i\n", (char)(i), ++c);
  10.                     }
  11.                     i=i+1;
  12.         }
  13. }
Mar 17 '18 #4

Expert 100+
P: 2,373
No need for c on line 9 just print i twice, once with %c and once with %i.
Mar 18 '18 #5

Expert 100+
P: 921
But then it will print

And zentrox21 wanted to see:
Mar 18 '18 #6

Expert 100+
P: 2,373
My mistake, I thought he wanted to print the encoded value for each letter.

Your code assumes that if you sort the uppercase letters in alphabetic order then the corresponding encoded values increase monotonically (but the values need not be contiguous); it makes the same assumption regarding the lowercase letters; and it assumes all uppercase letters have smaller encoded values than any lowercase letter.

These assumptions are true for ASCII encoding but I donít know that the C Standard requires that they be true for any and all character encodings.
Mar 19 '18 #7

Expert 100+
P: 921
LOL the order of characters, in 'any' encoding i know of, is the same as in ASCII.

Probably just to make sure every system can order alphabetically...
(and lazyness of everyone who invented another character encoding...)

But, as far as, he C Standard requires things, you are right!
(But i never read a document about that subject, as fara as i can remember!)
Mar 19 '18 #8

Expert 100+
P: 2,373
EBCDIC has smaller encoded values for lowercase than for uppercase. This violates the third assumption.

A portable approach is to iterate through the literal string constant "ABC...XYZabc...xyz". The array index is the rvalue and the corresponding element in the array is the lvalue. However, whether this solves the OP's problem depends on the precise wording of the assignment.
Mar 19 '18 #9

Expert 100+
P: 921
Ok, for now, we have (or you!) have helped him enough!

Let's wait for further questions for this weird assignment.... ;)
Mar 19 '18 #10

Post your reply

Sign in to post your reply or Sign up for a free account.