How do I create a program where the input of a letter (a-z) is converted to ASCII and printed out. I am stuck at the point where it has to show all the ASCII numbers before it.
Let's say if the user inputs the character c, I want to print out 97,98,99 for character inputs a,b,c respectively.
I am somewhat a beginner. Pretty sure I have to use a loop.
Hi, as I am a beginner can you possibly check my way of doing it, unsure where I am doing wrong. Your code has a few things which I am unfamiliar with. I am aiming to restrict it to only A-Z. So here's the code :
#include <stdio.h>
int main()
{
char c,z;
printf("Enter An English upper case letter: ");
scanf("%c", &z);
for (c=65;c<91;c++)
{
if(c==z)
printf("%c",z);
}
return 0;
}
#include <stdio.h>
void get_ASCII_value(char c)
{
printf("The ASCII value of %c = %d", c, c);
}
int main(void)
{
char c;
printf("Enter a character: ");
scanf("%c", &c);
get_ASCII_value(c);
return 0;
}
To clarify, %d prints the ASCII value for that specific char
As for your specific case:
#include <stdio.h>
int main()
{
char c,z;
printf("Enter An English upper case letter: ");
scanf("%c", &z);
if (z < 65 || z > 90)
return 0;
for (c = 65; c <= z; c++)
printf("The ASCII value of %c = %d\n", c, c);
return 0;
}
#include <stdio.h>
#include <stdint.h>
#include <limits.h>
int main(void)
{
for(uint8_t c = 0; c < UCHAR_MAX; ++c)
{
uint8_t out[2] = {c};
printf("%d (%s)\n", c, isprint(c)? out : "*unprintable*");
}
return 0;
}
###Output
Success #stdin #stdout 0s 4288KB
0 (*unprintable*)
1 (*unprintable*)
2 (*unprintable*)
3 (*unprintable*)
4 (*unprintable*)
5 (*unprintable*)
[....]
29 (*unprintable*)
30 (*unprintable*)
31 (*unprintable*)
32 ( )
33 (!)
34 (")
35 (#)
36 ($)
37 (%)
38 (&)
39 (')
40 (()
41 ())
42 (*)
43 (+)
[...]
65 (A)
66 (B)
67 (C)
68 (D)
69 (E)
70 (F)
71 (G)
Related
This question already has an answer here:
scanf a big hexadecimal value
(1 answer)
Closed 2 years ago.
I want to read a hexadecimal number from the user. I use C99.
My idea was to read a char and check by the character code what hexadecimal number it could be.
Here is the code:
#include <stdio.h>
int main() {
char count;
int c;
printf("Enter hex value:\n");
scanf("%c", &count);
if (count >= 48 && count <= 57) {
c = count - 48;
}
if (count >= 65 && count <= 70) {
c = count - 55;
}
if (count >= 97 && count <= 102) {
c = count - 87;
}
printf("%d", c);
return 0;
}
But I think there should be easier ways. Because it can only read one number and not longer ones.
Is there anything that could help?
You can use scanf with %x:
#include <stdio.h>
int main() {
int a;
scanf("%x", &a);
printf("%d", a);
}
Output:
a -> 10
ff -> 255
I will take hex input from user into buffer array, but i don't take it, scanf does not appropriate. Input data like this (0x 06 41 42 43 0f 52 53). Also then i want to transform string to integer type some part of array. I used atoi, what is best way for it?
#include <stdio.h>
int main(){
char buffer[1000];
char dest[3];
int x;
//scanf("%s",buffer);
x=atoi(strncpy(dest,buffer+1,4))
}
The following simple function converts a string with a hexadecimal number to an integer:
int atox(const char *s)
{
int x= 0;
while (*s) {
x= x*16+(*s>'9'?(toupper(*s)-'A'+10):*s-'0');
s++;
}
return x;
}
and call it like:
printf("%02x\n",atox("42"));
printf("%02x\n",atox("a1"));
printf("%02x\n",atox("A1"));
Note that the string must be exactly the string to convert, so no spaces or whatever.
Here is the code using strtol function considering numbers as Hex inside the string (For base value 8 they will be octal):
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char *s="00 06 41 42 43 0f 52 53";
char *endpoint=s;
int base=16,count=1;
long int result=0;
do
{
result=strtol(endpoint,&endpoint,base);
printf("value %d %ld\n",count,result);
count++;
}while((endpoint - s) < (size_t)strlen(s) );
return 0;
}
I have an assignment where I'm suppose to read input lines such as
2
67 5 100 1 11 97 98 10 1 110
15 72 10 101 47 67 88 20 94 6 22 11
4
61 11 93 4 73 39 78 34 17 104
23 43 11 93 65 52 20 96 66 31 86 24 40 61 102 13 50 51
73 43 28 73 8 89 31 68 77 27 24 77 42 72 15 24 64 51
25 75 7 90 10 111 17 16
.....
process every two integers (the first line in a block only tells us how many words we will process), add them then match
them to their corresponding ASCII char. The example above would be two blocks.
The output should be:
Decoded messages:
Hello World!
Happy Bhutanese teacher's day!
I'm having problems when it comes to dealing with a file with multiple blocks, more than 20 and so forth following the same format on one input text. I can handle one block fine, two blocks okay but not fine because my program doesn't seem to end. No line will be longer than 256 characters. numOfPuzzl refers to how many words we process per block.
I'd greatly appreciate any help. I attached my code and commented as much as I can too.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char* argv[])
{
//user will type in file name they choose to process and we allocated in filename[]
char filename[256];
printf("Enter filename: ");
scanf("%s", filename);
//process filename username typed in
FILE *pFile;
pFile = fopen(filename, "r");
//if there's nothong to read
if (pFile == NULL){
exit(1);
}
printf("Decoded messages:\n");
//create array we will putting lines into
char myString[256];
//simply gets the first line, which is always a lone integer
fgets(myString, 256, pFile);
int numOfPuzzl;
sscanf(myString, "%d", &numOfPuzzl);
//printf("puzzles to solve: %d\n", numOfPuzzl);
int wordsProcessed = 0;
//just remember that myString has entire line
//start processing the lines that follow, a line is a word
while (fgets(myString, 256, pFile) != NULL){
int num = 0; //first integer in line
int secondNum = 0; //second int. in line
int tot = 0; //how many bytes
int bytes_read = 0; //bytes processed
char *endOfStrAdr = strchr(myString, '\0'); //returns address of end terminating char.
int endOfStrIdx = endOfStrAdr - myString; //this will give me the index of where the terminating char. occurs within my array
//start scanning integers within array making sure to not sccan out of bounds
while (tot < endOfStrIdx){
//first integer allocated as well as how many byes it was
sscanf(myString + tot, "%d %n", &num, &bytes_read);
tot += bytes_read; //keeps tab so we don't have to read from the begn. of array everytime
//second integer allocated as well as how many byes it was
sscanf(myString + tot, "%d %n", &secondNum, &bytes_read);
tot += bytes_read; ////keeps tab so we don't have to read from the begn. of array everytime
printf("%c", (char) num + secondNum); //add the two integers and cast them to char
//we want to check if we are the end of the string, our word
if (tot == endOfStrIdx){
printf(" ");
wordsProcessed++;
//we want to print a new line char. if we finished processing all the words for the puzzle
if (wordsProcessed == numOfPuzzl){
printf("\n");
fgets(myString, 256, pFile);
}
}
}
}
fclose(pFile);
}
Ignore blank lines between puzzles.
Reset parameters (numOfPuzzl and wordsProcessed) before processing new puzzles.
To archive that, change
if (wordsProcessed == numOfPuzzl) {
printf("\n");
fgets(myString, 256, pFile);
}
into
if (wordsProcessed == numOfPuzzl) {
printf("\n");
while ( fgets(myString, 256, pFile) != NULL ){
if ( sscanf(myString, "%d", &numOfPuzzl) == 1 )
break;
}
wordsProcessed = 0;
}
I suggest like this:
#include <stdio.h>
#include <stdlib.h>
//stringize macro
#define S_(x) #x
#define S(x) S_(x)
int main(void){
char filename[FILENAME_MAX + 1];
printf("Enter filename: ");
scanf("%" S(FILENAME_MAX) "[^\n]", filename);
FILE *pFile;
if(NULL==(pFile = fopen(filename, "r"))){
perror("can't opne file");
exit(EXIT_FAILURE);
}
printf("Decoded messages:\n");
int numOfLines;
while(1==fscanf(pFile, "%d", &numOfLines)){
for(int i = 0; i < numOfLines; ++i){
int num1, num2, state;
char ck;
while(3==(state=fscanf(pFile, "%d %d%c", &num1, &num2, &ck)) || 2 == state){
putchar(num1 + num2);
if(state == 2 || state == 3 && ck == '\n')
break;
}
putchar(' ');
}
putchar('\n');
}
fclose(pFile);
return 0;
}
My program gives me error(not exactly an error but it just prints error instead of even or odd) even if I put a number or letters. The code works if I remove the isdigit checker(3rd line). I do no know what am I doing wrong. Can someone please help me. Thanks in advance. Here is my code.
int main()
{
int n;
printf("Input an integer\n");
scanf("%d", &n);
if(!isdigit(n))
{
print("error");
return 0;
}
n%2 == 0 ? printf("Even\n") : printf("Odd\n");
return 0;
}
isdigit is not for this purpose.
If you want to check if the input is vaild, one method is to load with %s and use strtol.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
void print(const char *s) {
puts(s);
}
int main()
{
char nstr[100] = {0};
int n;
char *e;
printf("Input an integer\n");
scanf("%99s", nstr);
n=(int)strtol(nstr, &e, 10);
if(nstr[0] == '\0' || *e != '\0')
{
print("error");
return 0;
}
n%2 == 0 ? printf("Even\n") : printf("Odd\n");
return 0;
}
man -a isdigit
isdigit()
checks for a digit (0 through 9).
Thus isdigit fails if ascii value of n is not anything but
Oct Dec Hex Char
--------------------------
060 48 30 0
061 49 31 1
062 50 32 2
063 51 33 3
064 52 34 4
065 53 35 5
066 54 36 6
067 55 37 7
070 56 38 8
071 57 39 9
man -a ascii
thus,
if(!isdigit(n))
{
print("error");
return 0;
}
is not an appropriate option. you should probably find some other option to validate n.
The isdigit function checks a character to see if it is in the '0' to '9' range. More specifically, it checks if the ASCII value of the character is between 48 (the code for '0') and 57 (the code for '9').
You're passing an int to this function, not a character, so it's not appropriate to use this function here. Just remove this check and it will work.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n;
printf("Please enter your number\n");
scanf("%d",&n);
if( n%2==0)
printf("The number is even\n");
else
printf("The number is odd\n");
System("pause");
return 0;
}
Check this one.
First need to convert the string into its individual ASCII value and then we have to accomplish following task:
Check if two consecutive ASCII(values of the string) difference is 1 or not. If difference is 1
then they will be coupled together and print them.
ex.
ABCD
individual ASCII value : 65 67 68 69
Difference of two consecutive ASCII value is 1 so coupled together then print them.
#include<stdio.h>
#include<string.h>
int main(){
char str[100];
int i=0;
printf("Enter any string: ");
scanf("%s",str);
printf("ASCII values of each characters of given string: ");
while(str[i])
printf("%d ",str[i++]);
return 0;
}
This code print the ASCII values but i don't know how to check the difference between two consecutive values and how to couple them.
#include<stdio.h>
int main(){
char str[100]="";
int i, last;
printf("Enter any string: ");
scanf("%s",str+1);//top is dummy
printf("ASCII values of each characters of given string: ");
i = 1;
while(str[i])
printf("%d ",str[i++]);
printf("\n");
last = i;
for(i=1;i<last;++i){
if(str[i] == str[i-1] + 1 || str[i] == str[i-1] - 1 ||
str[i] == str[i+1] + 1 || str[i] == str[i+1] - 1)
printf("%c", str[i]);
}
printf("\n");
return 0;
}
Well to give you a clue and not solve your problem entierly:
As char is an integral type in C, do the following:
if( 'B' - 'A' == 1){
printf("OK\n");
}
if( 'C' - 'A' == 2){
printf("OK\n");
} //etc
(On ideone)
Just think about what should you change in the loop and I think you'll get the idea :)
I think the easiest way to go about your problem it after you read string, create a pointer to it, then just advance a pointer through string checking where *pointer - *(pointer - 1) == 1. Do this for pointer values > string[0] and you can check if your two adjacent characters are coupled. (you will need the absolute value of the difference). A quick hack at your solution would look something like this:
#include<stdio.h>
#include<string.h>
int absdiff (unsigned a, unsigned b) {
if (a == b) return 0;
return (a > b) ? a - b : b -a;
}
int main(){
char str[100];
char cpl[3];
cpl[2]='\0'; // null terminate cpl
//int i=0;
printf("Enter any string: ");
scanf("%s",str);
printf("ASCII values of each characters of given string: \n");
char *ptr = str;
while(*ptr) {
if ((ptr - str) > 0) {
if (absdiff (*ptr, *(ptr - 1)) == 1) {
cpl[0] = *(ptr - 1);
cpl[1] = *ptr;
printf (" coupled : %s\n", cpl);
ptr++;
continue;
}
}
printf(" %c = %u\n",*ptr, *ptr);
ptr++;
}
putchar ('\n');
return 0;
}
And if given the string 'my_dog_is_unable_to_bark.' would produce the following output:
Enter any string: my_dog_is_unable_to_bark.
ASCII values of each characters of given string:
m = 109
y = 121
_ = 95
d = 100
o = 111
g = 103
_ = 95
i = 105
s = 115
_ = 95
u = 117
n = 110
a = 97
coupled : ab
l = 108
e = 101
_ = 95
t = 116
o = 111
_ = 95
b = 98
coupled : ba
r = 114
k = 107
. = 46
Of course you can reformat the output in any manner you like, but for illustrative purposes, this gave a good confirmation of the character -> unsigned int values.