3 Replies - 15845 Views - Last Post: 23 March 2011 - 12:15 AM Rate Topic: -----

#1 mightysparks  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 33
  • Joined: 23-February 11

C program to display ASCII value?

Posted 19 March 2011 - 05:02 AM

We have to create a C program that continuously reads a single character and displays the ASCII value of the character, and quits if # is entered. So far, my program does that, but I'm having a couple of problems.

1) After reading in a character, it displays the following (say the character was 'b'):
ASCII value for b is 98.
Enter a single character: ASCII value for
is 10.


I can't work out how to get it to continuously ask for input, without displaying that extra bit marked in italics. I tried putting it outside the 'do', but then it only asked once and now I'm lost.

2) We also have to change the case of the character if it's alphabetic. So, if 'b' is the input, it displays the character for 'b' and 'B'. I have no idea how to do this.

3) It says "You are not allowed to use any of the Standard C Library functions for character processing in your solution." What does this mean?

#include <stdio.h>

int main ()
{
	char letter = 'A';
	
	do 
	{
		printf("Enter a single character: ");
		scanf("%c", &letter);
	
		printf("ASCII value for %c is %d \n", letter, (int) letter);
	}
		while(letter != '#');
	
	return(0);
}



Is This A Good Question/Topic? 0
  • +

Replies To: C program to display ASCII value?

#2 sk1v3r  Icon User is offline

  • D.I.C Addict

Reputation: 231
  • View blog
  • Posts: 668
  • Joined: 06-December 10

Re: C program to display ASCII value?

Posted 19 March 2011 - 05:21 AM

after you enter b you press enter, and so it counts '\0' as a character.
so You could add
if(letter != '\0')
{
    //..
}


This post has been edited by sk1v3r: 19 March 2011 - 05:21 AM

Was This Post Helpful? 0
  • +
  • -

#3 sk1v3r  Icon User is offline

  • D.I.C Addict

Reputation: 231
  • View blog
  • Posts: 668
  • Joined: 06-December 10

Re: C program to display ASCII value?

Posted 19 March 2011 - 05:30 AM

also, to change a letter to capital without using a function you can do this. First, check whether that character is a lower-case letter, keeping in mind that these are between 'a' and 'z' and their values are kept as you would expect, (e.g., 'b' is 'a' + 1)
then to change it to capital, just add to it the difference between a letter and the capital of that letter, e.g.

char example = 'c';
char capital = example + ('A' - 'a');
printf("capital of %c is %c", example, capital); // output --> capital of c is C


Was This Post Helpful? 0
  • +
  • -

#4 mightysparks  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 33
  • Joined: 23-February 11

Re: C program to display ASCII value?

Posted 23 March 2011 - 12:15 AM

Ok, I think the 'enter' thing is working now, but I've got some other problems.

1) How can I limit the input length? If I type in 2 letters, it displays output correctly the first time, but if I type it again, it says it's a non-alphabetical character. If I type in a lot of numbers, it outputs the 'non-alphabetical character' output a few times.

2) I'm still not sure what it means by not being allowed to use standard library functions. I don't know if I'm using them or not, or what I'm supposed to be using instead.

#include <stdio.h>
#include <ctype.h>

int main ()
{
	char letter, upper;
	
	do 
	{
		printf("Enter a single character: ");
		scanf("%c%*c", &letter);
	if(letter != '\0')
	{
		//..
	}
		if(letter >= 'a' && letter <= 'z')
		{
			upper = letter + ('A' - 'a');
		printf("The ASCII value for %c is %d \n", letter, (int) letter);
		printf("The ASCII value for %c is %d \n", upper, (int) upper);
		}
		else
			if(upper >= 'A' && upper <= 'Z')
			{
				letter = upper + ('a'- 'A');
		printf("The ASCII value for %c is %d \n", letter, (int) letter);
		printf("The ASCII value for %c is %d \n", upper, (int) upper);				
			}
			else
				{		
					printf("You have entered a non-alphabetic character, please try again. \n");
				}
	}
		while(letter != '#');
	return(0);
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1