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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • zentrox21
    • Mar 2018
    • 0

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

    A = 1 so on then small letters a = 27 using ascii please help thank you so much
  • Luuk
    Recognized Expert Top Contributor
    • Mar 2012
    • 1043

    #2
    Code:
    #include "stdio.h"
    
    int main() {
            int i=1;
            while (i<=26) {
                        printf("%c=%i\n", (char)(i+64), i);
                        i=i+1;
            }
            return 0;
    }
    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 ... ;)

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      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.
      Last edited by donbock; Mar 16 '18, 05:24 AM. Reason: Added why-an-int paragraph.

      Comment

      • Luuk
        Recognized Expert Top Contributor
        • Mar 2012
        • 1043

        #4
        Code:
        #include "stdio.h"
        #include "limits.h"
        
        void main() {
                int c=0;
                int i=CHAR_MIN;
                while (i<=CHAR_MAX) {
                            if (islower(i) || isupper(i)) {
                                printf("%c=%i\n", (char)(i), ++c);
                            }
                            i=i+1;
                }
        }

        Comment

        • donbock
          Recognized Expert Top Contributor
          • Mar 2008
          • 2427

          #5
          No need for c on line 9 just print i twice, once with %c and once with %i.

          Comment

          • Luuk
            Recognized Expert Top Contributor
            • Mar 2012
            • 1043

            #6
            But then it will print
            A=65
            B=66
            ...
            a=97
            a=98
            ...

            And zentrox21 wanted to see:
            A=1
            B=2
            ...
            a=27
            b=28
            ...

            Comment

            • donbock
              Recognized Expert Top Contributor
              • Mar 2008
              • 2427

              #7
              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.

              Comment

              • Luuk
                Recognized Expert Top Contributor
                • Mar 2012
                • 1043

                #8
                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!)

                Comment

                • donbock
                  Recognized Expert Top Contributor
                  • Mar 2008
                  • 2427

                  #9
                  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.

                  Comment

                  • Luuk
                    Recognized Expert Top Contributor
                    • Mar 2012
                    • 1043

                    #10
                    Ok, for now, we have (or you!) have helped him enough!

                    Let's wait for further questions for this weird assignment.... ;)
                    Last edited by Luuk; Mar 19 '18, 11:52 PM. Reason: emitocon.... changed from �� to ;)

                    Comment

                    Working...