473,473 Members | 1,843 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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
Mar 9 '18 #1
9 1890
Luuk
1,047 Recognized Expert Top Contributor
Expand|Select|Wrap|Line Numbers
  1. #include "stdio.h"
  2.  
  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. }
  11.  
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
donbock
2,426 Recognized Expert Top Contributor
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
Luuk
1,047 Recognized Expert Top Contributor
Expand|Select|Wrap|Line Numbers
  1. #include "stdio.h"
  2. #include "limits.h"
  3.  
  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. }
  14.  
Mar 17 '18 #4
donbock
2,426 Recognized Expert Top Contributor
No need for c on line 9 just print i twice, once with %c and once with %i.
Mar 18 '18 #5
Luuk
1,047 Recognized Expert Top Contributor
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
...
Mar 18 '18 #6
donbock
2,426 Recognized Expert Top Contributor
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
Luuk
1,047 Recognized Expert Top Contributor
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
donbock
2,426 Recognized Expert Top Contributor
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
Luuk
1,047 Recognized Expert Top Contributor
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

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

Similar topics

2
by: Ebi | last post by:
Hi I'm writing a project in C++ borland 5; and I need a function that get a string or char* variable and convert all of the small letters to the capital. please help me. -- Sincerely Yours...
11
by: Kai Bohli | last post by:
Hi all ! I need to translate a string to Ascii and return a string again. The code below dosen't work for Ascii (Superset) codes above 127. Any help are greatly appreciated. protected...
5
by: bouy78 | last post by:
I now know that I know nothing... using System; using System.IO; namespace ADODotNet { public class Class1 { public Random r = new Random(); string nodes = new string { "192.168.0.0",...
2
by: Merrigan | last post by:
Hi There, I'm sure I have done this before, but cannot remember how, or find out how to do it quickly - but is there a way/function/something in python to make all the letters of a raw_input()...
2
hsriat
by: hsriat | last post by:
This question might sound stupid, but I just want to know if it effects the performance of the browser if the whole HTML code is in CAPITAL letters or small letters?
3
by: RaniC | last post by:
In a for, while or do loop statement the outputs from left to right is the normal way to have it processed such as loop1 loop2 loop3 loop4 loop5 ... Without doing an array I would like to have...
3
by: ahd2008 | last post by:
Good day all, I am creating a new from for the purpose of allowing the Admin(s) to create new users. There is a filed for the password and another one to confirm the password. However, MS Access...
1
by: Shepard | last post by:
Hi, I have been learning JSP and Servlets when I came upon JSTL and EL concepts.. I am new to JSP... From what I understand JSTL and EL was introduced in JSP so as to avoid using scriplets<%%> in...
1
by: ranisonavane181 | last post by:
In one array capital letter and small letter word array I wan to convert into two array one small letter and other capital array and display it.
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.