Click here to Skip to main content
14,932,737 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I wanna make a code in C that reads n characters,verifies if the characters are letters or numbers between 0 and 9 and if they are,then the program displays their decimal ASCII value,else it will be displayed a message:"Unknown Character".

Eg:Input:4
A b 1 !
Output:65 98 49 Unknown Character

What I have tried:

//My code in C++
C++
#include<iostream>
using namespace std;
int main( )
{
	int i;
	int  n;
	char c;
	cin >> n;

	for (i = 0; i < n; i++)
	{
		cin >> c;
		if ((c >= 'a'&&c <= 'z') || (c >= 'A'&&c <= 'Z') || (c >= '0'&&c <= '9'))
		{
			cout << (int)c << " ";
		}
		else
		{
			cout << "Unknown Character" << " ";
		}			
	}
	cout << "\n";
	system("pause");
	return 0;
}

//My code in C
#include<stdio.h>
int main()
{
	int i, n;
	char c;
	scanf_s("%d", &n);
	for (i = 0; i < n; i++)
	{
		scanf_s("%c", &c);
		if ((c >= 'a'&&c <= 'z') || (c >= 'A'&&c <= 'Z') || (c >= '0'&&c <= '9'))
		{
			printf("%d\n", c);
		}
                else
               {
                     printf("Unknown Character");
               }	
	}
	system("pause");
	return 0;
}
Posted
Updated 30-Aug-18 1:51am
v2

1 solution

Why do you wrote it in C++ when you have to use C? Or did you found the code in the net?

Anyway, your code is working and needs only a little tweaking to produce the expeceted result.

The scanf() function will stop when a not matching character occurs leaving all remaining characters in the input buffer. When reading an integer value that means that the line feed character \n placed in the buffer by pressing the return key stops the conversion but is left in the buffer. When then calling scanf() again within the loop, it reads that line feed which is treated as unknown character.

The solution is quite simple: Tell scanf() to read and ignore the line feed:
scanf_s("%d\n", &n);
To get the required output format, all you have to do is replacing the \n in the printf() format string by a space, add a space at the end when printing "Unknown Character", and finally print a new line before terminating the application.
   
Comments
Neri-kun 30-Aug-18 6:10am
   
No,I didn't find that code in C++ on the internet.I conceived the idea myself.However when i tried to make that code work,first i tried to make it in C,but it didn't work.Then to make sure that the idea of the code could work,I switched to C++ and C++ syntaxes and it worked.Anyway i realized something else when i introduce in the input a value n,when introducing the characters and using space between them,my code percieves the space left between the other characters as a character.

In other words,for example,when I have:

Input:3
abc
Output:97 98 99

but when i introduce this:
Input:a b c
Output:97 Unknown Character98.

So what can i modify so that when i introduce:a b c with space between them,the program will display their decimal ASCII values?

Btw,here is a little update on the code:

//My code in C
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
int main()
{
int i, n;
char c;
scanf("%d\n", &n);
i = 0;
while (i != n)
{
scanf("%c", &c);
if ((c >= 'a'&&c <= 'z') || (c >= 'A'&&c <= 'Z') || (c >= '0'&&c <= '9'))
{
printf("%d ", c);
}
else
{
printf("Unknown Character");
}
i++;

}

printf("\n");
system("pause");
return 0;
}
Jochen Arndt 30-Aug-18 6:26am
   
When you enter "a b c" it has 5 characters but you have specified that only 3 should be there. So only the first 3 are processed and the space is an unknown one.

There are multiple solutions depending on what you want to do:
- Read the input as string and process that
- Use getchar() to read a single character
- Optionally ignore spaces

Note also that there is the function isalnum() defined in ctype.h (std::isalnum() in ccytpe for C++).
Neri-kun 30-Aug-18 6:54am
   
Here's what I came up with:
//My code in C
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
int main()
{
int i, n;
char c;
scanf("%d\n", &n);
i = 0;
while (i != n)
{
scanf("%c", &c);
if ((c >= 'a'&&c <= 'z') || (c >= 'A'&&c <= 'Z') || (c >= '0'&&c <= '9'))
{
printf("%d ", c);
}
else
if (c == ' ')
{
printf("%c", c);
i--;
}
else
{
printf("Unknown Character");
}
i++;
}
printf("\n");
system("pause");
return 0;
}
So basically i realized that by using if (c == ' '){printf("%c", c); i--;} the code will ignore the spaces between characters.However,at the same time,when i realized it works this way,i have been thinking that even if i declared that c as char and used "%c" when being scanned that c acts as a string.So,i thought of changing its declaration as such:char c[size] and then %s when scanning and printing.The code still worked as I wanted to,but when exiting the command prompt i ended up getting an exception thrown:"Run-Time Check Failure #2 - Stack around the variable 'c' was corrupted.".Can you tell me what it means?
Jochen Arndt 30-Aug-18 7:08am
   
If you define a string (an array of characters), it must be large enough to hold the entered data plus one for the terminating NULL that is placed at the end when reading with "%s". If more characters are read, an array overflow occurs which results in a memory corruption (stack memory here because the array is on the stack).

Solution:
Ensure that the array is large enough and tell scanf() about the actual size. Example:
char str[16];
scanf("%15s", str);
Neri-kun 30-Aug-18 8:19am
   
Ok,thanks for your explanation :).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900