convert hexadecimal to decimal in c programming [closed] - c

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to convert from hexadecimal string to decimal number. But my result is different.
This is my code in c:
#include <stdio.h>
main() {
char c[100];
int n,i;
n=0;
printf("Enter Your Hexadecimal:\n");
scanf("%s",c);
for(i=0; c[i]!='\n' ; i++)
if(c[i]>='0' && c[i]<='9')  n=n*16+(c[i]-'0');
else if(c[i]=='a' || c[i]=='A') n=n*16+(10);
else if(c[i]=='b' || c[i]=='B') n=n*16+(11);
else if(c[i]=='c' || c[i]=='C') n=n*16+(12);
else if(c[i]=='d' || c[i]=='D') n=n*16+(13);
else if(c[i]=='e' || c[i]=='E') n=n*16+(14);
else if(c[i]=='f' || c[i]=='F') n=n*16+(15);
printf("%d",n)
getch();
}
The result is
Enter Your Hexadecimal:
2f
-585093843 /* This is the result of my code*/<br>
..........Added Answer After Some Times!................
After one year I add the answer of this question for beginners and newbies incC Language ...Hope to be useful
#include "stdio.h"
int main() {
char c[100];
int n,i;
n=0;
printf("Enter Your Hexadecimal:\n");
scanf("%s",&c);
for(i=0; c[i]!='\n' && c[i]!=0 ; i++){
if(c[i]>='0' && c[i]<='9')
n=n*16+(c[i]-'0');
else if(c[i]=='A' || c[i]=='a')
n=n*16+(10);
else if(c[i]=='B' || c[i]=='b')
n=n*16+(11);
else if(c[i]=='C' || c[i]=='c')
n=n*16+(12);
else if(c[i]=='D' || c[i]=='d')
n=n*16+(13);
else if(c[i]=='E' || c[i]=='e')
n=n*16+(14);
else if(c[i]=='F' || c[i]=='f')
n=n*16+(15);
else{
printf("Error:Your number Is Not Valid!");
return -1;
}
}
printf("%d",n);
return 0;
}

Point 1. You never initialized i. No type mentioned for c [maybe it'll get type default to int]
Point 2. You did not use a loop to check all the digits in the input[if it is having more than one digit].
Point 3. You did not check for the success of scanf()
Point 4. You never validated the input, neither you did limit the input to one digit [as per current logic].
Point 5. Did not use a proper signature for main(), no return [more of a good practice point of view]
and maybe many more. I'm out.

Related

Use of fgets in c [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I need to write a program which converts a number from one base to another.
i need to get a user input in the form of: <original base><new base><number in original base>
I’m not allowed to use scanf and also im not allowed to assume the size of the line.
I have already tried using fgets() but I don’t know how to use it without limiting the size. I would love to get some ideas of what to do. Thanks.
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int BaseChanger()
{
char input[12];
printf("enter the original base, new base ,number");
fgets(input, 12, stdin);
}
You can use this snippet as a form of getline
int max= 100;
char* array;
array = malloc(max*sizeof(char));
if(array == NULL)
exit(1);
int c,i=0;
while( ( c = getchar()) != EOF && c != '\n' && i < max ) {
array[ i++ ] = c ;
if( i == max)
{
char * narray = realloc(array, max *= 2);
if( narray == NULL ){
free(array);
exit(1);
}
array = narray;
}
...
}
Once you do this, extract the numbers and then do the rest of the logic.

why does this c program print twice [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
#include <stdio.h>
int main()
{
char c = 'A';
while (c != ',')
{
printf("Input a character:");
scanf("%c", &c);
if (c >= '0' && c <= '9')
{
printf("%d\n", (int)c);
}
}
}
After taking in the first set of input, this code prints out "Input a character" twice each time - why is this?
cause you press a number PLUS enter and enter will be read by scanf() at the next call
#include <stdio.h>
int main(void) {
char c = 'A';
while (c != ',') {
printf("Input a character:");
if (scanf("%c", &c) != 1) {
return 0; // we stop if user don't input anything
}
if (c >= '0' && c <= '9') {
printf("%d\n", (int)c); // by the way did you want (int)(c - '0') ?
} else {
printf("enter a number ! you enter %d\n", c);
}
}
}

Trouble with C program blank output [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
int i=0,j=0;
char string[100], string2[100];
scanf("%s",&string);
while (string[i]!='\0'){
if(string[i]=='a' || string[i]=='e' || string[i]=='i' || string[i]=='o' || string[i]=='u' || string[i]=='A' || string[i]=='E' || string[i]=='I' || string[i]=='O' || string[i]=='U'){
string[i]=string2[j];
}
string[i] = tolower(string[i]);
string[i] = string2[j];
string2[j-1]='.';
}
printf("%s", string2);
return 0;
The question is entering a word and then removing all vowels, adding '.' after every constant and making all upper case letters lower case.
Since string is an array, you don't use & when passing it to scanf(), this gives you a double pointer and is an error. Any time you find yourself with a 10 clause if statement, you're just asking for problems (e.g. easy to get tripped up by typos.) You can simplify this test with index() and a string containing all the vowels. It wouldn't hurt to comment as you write your code to indicate which of the requirements each section implements. The i variable needs to be incremented every time through the loop, the j variable needs to be incremented every time a new character is added to string2. After the scanf(), you shouldn't be assigning into string, treat it as readonly, only assign into string2. And j-1 shouldn't happen. Finally, since string2 isn't intialized, there may be garbage in it and you haven't null terminated it. Putting it all together:
#include <ctype.h>
#include <stdio.h>
#include <strings.h>
#define VOWELS "AEIOUaeiou"
int main()
{
char string[100], new_string[100] = { 0 };
// enter a word
scanf("%s", string);
for (int i = 0, j = 0; string[i] != '\0'; i++)
{
// remove all vowels
if (index(VOWELS, string[i]) == NULL)
{
// make all upper case letters lower case
new_string[j++] = tolower(string[i]);
if (isalpha(string[i]))
{
new_string[j++] = '.'; // add '.' after every consonant
}
}
}
printf("%s\n", new_string);
return 0;
}
I'm assuming "after every constant" was meant to read "after every consonant", otherwise please clarify what you mean by constant.

Converting an array of letter to an array of numbers [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a text file in D drive named "data.txt". It has several columns and rows like:
A B A B V B
B V A B A A
B B A A B A
I want to read the "program.txt" file and then convert it to numbers say A=1, B=2, V=3 and then write it so that it will be a array of number. But I'm not sure where to begin. Can someone point me in the right direction?
#include <stdio.h>
int main() {
int n,k;
char C;
FILE *fptr;
fptr=fopen("D:\\program.txt","r+");
printf("Enter n: ");
scanf("%d",&n);
if (n == C){
fprintf(fptr,"%d",3);
}
fclose(fptr);
return 0;
}
From your question, it looks like you want to assign a new number to each character as you encounter it. So you need to keep track of what you've seen and what number was assigned, as well as the current number.
You can do this with an array:
int values[256] = {0};
int next_value = 1;
When you read a character, you can check if you've already seen it. You may also want to check that it's in the right range. If you haven't seen it, assign a number. Otherwise use the number that was originally assigned:
if (c >= 'A' && c <= 'Z') {
if (!values[c]) values[c] = next_value++;
printf ("%d", values[c]);
}
Then it's just a matter of reading the file. There are many ways to do this, but I think the simplest in this case is to read each character from the file in a loop and output them after translating the ones you're interested in:
int c;
while (EOF != (c = getc(fp))) {
if (c >= 'A' && c <= 'Z') {
if (!values[c]) values[c] = next_value++;
printf ("%d", values[c]);
} else {
putchar(c);
}
}
#include <stdio.h>
int main() {
char line_buff[16];
FILE *fptr;
fpos_t pos;
fptr=fopen("D:\\program.txt","r+");
fgetpos(fptr, &pos);
while(fgets(line_buff, sizeof(line_buff), fptr)){//read one line
char *p = line_buff;
while(*p){//convert
if(*p == 'A')
*p++ = '1';
else if(*p == 'B')
*p++ = '2';
else if(*p == 'V')
*p++ = '3';
else
++p;
}
fsetpos(fptr, &pos);
fprintf(fptr, "%s", line_buff);//rewrite
fflush(fptr);
fgetpos(fptr, &pos);//memo position
}
fclose(fptr);
return 0;
}

Weird results in c [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I'd like to ask you about my prog. The main purpose of this one is to fill the "result" array with data collect from "tab1" and "tab2" arrays. Could anybody check, why does results are so weird? Thank you.
#include <stdio.h>
#include <stdlib.h>
void laczenie(char tab1[],char tab2[])
{
int i;
char* result =(char*) malloc(100*sizeof(char));
for(i=0;i<30;i++)
{
if(tab1[i] != '\0') tab1[i]==result[i];
else if (tab2[i] !='\0')tab2[i]==result[i];
else printf(" ");
}
for(i=0;i<30;i++)printf("%c",result[i]);
free(result);
}
int main()
{
char x[10]={'n','a','p','i','s','1'};
char y[10]={'n','a','p','i','s','2'};
//char x[10] = {"napis1"};
//char y[10] = {"napis2"};
laczenie(x,y);
return 0;}
In addition to LihOs answer above this block looks wrong:
if(tab1[i] != '\0')
tab1[i]==result[i];
else if (tab2[i] !='\0')
tab2[i]==result[i];
else printf(" ");
Don't you mean to assign the value in tab1[i]or tab2[i] to result[i]like this?
if(tab1[i] != '\0')
result[i] = tab1[i];
else if (tab2[i] !='\0')
result[i] = tab2[i];
else printf(" ");
Also using magic numbers like in the loops: for(i=0;i<30;i++) is pretty bad practice, you should probably be using a constant for the size value (which you could then use in both the loops and in the array declarations. And why loop to 30 when the arrays is 10 elements only?
In your function you check for null-terminating character:
if(tab1[i] != '\0')
but where is null-terminating character here?
char x[10]={'n','a','p','i','s','1'};
Try:
char x[7]={'n','a','p','i','s','1','\0'};
Also note that tab1[i]==result[i]; compares tab[1] with result[i], if you want to assign the result[i] to tab1[i], use assignment operator =:
tab1[i]=result[i];
if(tab1[i] != '\0')
result[i] = tab1[i];
else if (tab2[i] !='\0')
result[i] = tab2[i];
else printf(" ");`
This is still wrong as by the time you assign tab2 to result i will be already at 6 so you will have to use two for loops i suppose for assign tab1 and tab 2

Resources