I have an assignment to write a program that converts decimal numbers to binary numbers, in the C programming language.
This is the code I wrote:
#include <stdio.h>
#define MAX_LEN 1000
void translate_dec_bin(char s[]){
unsigned int decNum;
sscanf_s(s, "%d", &decNum);
while (decNum > 0){
printf("%d", decNum % 2);
decNum = decNum / 2;
}
printf("\n");
}
main(){
char s[MAX_LEN];
char c='\0';
int count=0;
printf("enter a nunber\n");
while (c < MAX_LEN && ((c = getchar()) != EOF) && c != '\n'){
s[count] = c;
++count;
}
translate_dec_bin(s);
}
However, when the input is 1, the output I get is 1 instead of 0001. In other words, I want the 0's to appear in my output. How can I do that?
First, read mah's comment: although he suggest hitting a fly with a nuclear missile, he suggest you a right approach.
But in this case, your problem is simple: your loop in translate_dec_bin function finishes, because it has nothing left to do.
Think about for a second: your condition is "while decNum is greater than 0, print 1 or 0", right?
I guess you know by now, that in C if you divide integer, the remainder is ommited, so 1/2=0.
Now look at your code: when you give argument "1", the loop iterates only once, so you get only one character in the output.
If you want more characters, you need more conditions. I leave it to you to come up with them, I'm not THAT reputation-hungry as mah suggested :)
And by the way: there is no way you can fit 1000 digits long number in an unsigned int, and that's what you're trying to do.
Assuming you already have computed your decimal number
the basic idea without using print formating is the following:
char* list[] = {"0000", "000", "00", "0", ""};
char* leading_of(int num) {
int v = 0;
while(num > 0 && v < 4){
num = num /10;
v++;
}
return list[v];
}
printf("%s%d", leading_of(decNum), decNum);
If the number of characters or the padding character is variable you need to drop the list and do something more clever instead.
Related
I'm trying to find the frequency of digit into given string,which contain digit and letters. When i run the same program i get different result, look like random output. where is the problem ?
int main() {
char num[1001];
int digit[10];
int j,i;
int count;
scanf("%s",num);
for(i=48;i<=57;i++)
{
count = 0;
for(j=0; num[j] != EOF;j++)
{
if(num[j] == i)
{
count++;
}
}
printf("%d ",count);
}
return 0;
}
You have missed an & in the line with scanf before num. Correct code should be:
scanf("%s", &num);
When you are scanning for the value, you need to provide the address of the variable. That is you let your program know where to put the value. &num points to the address of the variable num. But if you just write scanf("%s",num) you are providing value of the num variable, which you don't care at all. You'll be overwriting that value anyway.
At the end of the string, computer puts a null character \0 whose ASCII value is 0, to denote that this is the end of the string. Kind of like a '.' but for a computer. So you check until you find the null character in the for loop like: num[j] != '\0' [Not EOF]
I don't see any issue here, it seems output is not readable properly
so try printing correctly like
printf("%c=>%d ",i, count);
so that you can read what number how many times. if you find wrong frequency,
post the input for which you are getting wrong output.
It seems like you're trying to compare a char element with an integer type.
for example:
char num[7] = {"ABC123");
if (num[3] == 1)
{
printf("True\n");
}
else
{
printf("False\n");
}
return 0;
// This will return False, even though the element at index 3 is "1".
I've ran your code, and it seems like the output is the same given the same input.
Edit:
We wanted to compare a two digit number to a char data type - which is essentially a character - a one digit number/case.
When we take a number N and modulo by 10, we get the last digit, example:
48 % 10 = 8
To get the first digit we simply divide by 10, and take the quotient, example:
48 / 10 = 4 (remainder 8).
With this knowledge, we can compare the n-th char with first digit, and the n+1-th char with the last one (given that we only compare two digits, we'll stop at n+1-th).
Tip: a number char can be turned into a int using char = char - '0'
char num[1001];
int digit[10];
int j,i;
int count;
scanf("%s",num);
for(i=48;i<=57;i++)
{
count = 0;
for(j=0; num[j] != EOF;j++)
{
if(num[j] - '0' == i / 10 && num[j+1] - '0' == i % 10)
{
printf("%c", num[j] - '0');
count++;
}
}
printf("%d: %d \n",i, count);
}
printf("\n");
return 0;
// This code will print the digits and the frequency in a new line:
48: n times
49: n times
.
.
.
57: n times
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 7 years ago.
Improve this question
I have to write a program that converts an user input (which is a string) to an Integer. In the same time it should check, if the user input really is a number.
And also everything in one method.
and NO LIBRARY FUNCTIONS allowed.
I can't figure any idea how to do it. All I got for the beginning is just this pathetic structure
#include <stdio.h>
void main()
{
char input[100];
int i;
int sum = 0;
printf("Type a String which will be converted to an Integer: ");
scanf("%c, &input");
for (i = 0; i < 100; i++)
{
}
}
I appreciate any help, thanks
The conversion is the easy part...
But if you must not use library functions,
there is only one way to take a string, and that is argv;
there is only one way to give an integer, and that is the exit code of the program.
So, without much ado:
int main( int argc, char * argv[] )
{
int rc = 0;
if ( argc == 2 ) // one, and only one parameter given
{
unsigned i = 0;
// C guarantees that '0'-'9' have consecutive values
while ( argv[1][i] >= '0' && argv[1][i] <= '9' )
{
rc *= 10;
rc += argv[1][i] - '0';
++i;
}
}
return rc;
}
I did not implement checking for '+' or '-', and did not come up with a way to signal "input is not a number". I also just stop parsing at the first non-digit. All this could probably be improved upon, but this should give you an idea of how to work around the "no library functions" restriction.
(Since this sounds like a homework, you should have to write some code of your own. I already gave you three big spoons of helping regarding argv, the '0'-'9', and the conversion itself.)
Call as:
<program name> <value>
(E.g. ./myprogram 28)
Check return code with (for Linux shell):
echo $?
On Windows it's something about echo %ERRORLEVEL% or somesuch... perhaps a helpful Windows user will drop a comment about this.
Source for the "'0'-'9' consecutive" claim: ISO/IEC 9899:1999 5.2.1 Character sets, paragraph 3:
In both the source and execution basic character sets, the value of each character after 0 in the above list of decimal digits shall be one greater than the value of the previous.
I'm sure this is preserved in C11, but I only have the older C99 paper available.
Take hightes digit and add it to number, multiply the number by 10 and add the next digit. And so on:
#include <stdio.h> // scanf, printf
void main()
{
char input[100];
printf("Type a String which will be converted to an Integer: ");
scanf("%s", input);
int number = 0;
int neg = input[0] == '-';
int i = neg ? 1 : 0;
while ( input[i] >= '0' && input[i] <= '9' )
{
number *= 10; // multiply number by 10
number += input[i] - '0'; // convet ASCII '0'..'9' to digit 0..9 and add it to number
i ++; // step one digit forward
}
if ( neg )
number *= -1;
printf( "string %s -> number %d", input, number );
}
input[i] - '0' works, because ASCII characters '0'..'9' have ascending ASCII codes from 48 to 57.
So basically you want to know how something like the standard library atoi works. In order to do this, you need to consider how strings represent numbers.
Basically, a string (that represents a number) is a list o digits from 0 to 9. The string abcd (where a, b, c, d are placeholders for any digit) represents the number a*10 ^ 3 + b*10^2 + c * 10 + d (considering base 10 here, similar for other bases). So basically you need to decompose the string as shown above and perform the required arhitmetic operations:
// s - the string to convert
int result = 0;
for (int index = 0; index < strlen(s); index++) {
result = result * 10 + s[index] - '0';
}
The operation s[index] - '0' converts the character that represent a digit to its value.
// the function returns true for success , and false for failure
// the result is stored in result parameter
// nb: overflow not handled
int charToInt(char *buff,int *result){
*result=0;
char c;
while(c=*buff++){
if((c < '0') || (c >'9')) // accept only digits;
return 0;
*result *= 10;
*result += c-'0';
}
return 1;
}
Lot of things which are missed. Firstly taking a string in is done by scanf("%s",input); By the way in which you are receiving it, it only stores a character, secondly run the loop till the length of the string recieved. Check the below code.
#include <stdio.h>
#include<string.h>
void main()
{
char input[100];
int i;
int sum = 0;
printf("Type a String which will be converted to an Integer: ");
scanf("%s", input);
for (i = 0; i < strlen(input); i++)
{
if(input[i]>=48 && input[i]<=57)
{
//do something, it is a digit
printf("%d",input[i]-48);
//48 is ascii value of 0
}
}
Try it:
#include <stdio.h>
void main()
{
char input[100];
int i,j;
int val = 0;
printf("Type a String which will be converted to an Integer: ");
scanf("%s",input);
for(j=0; input[j] != '\0'; j++); // find string size
for (i = 0; i < j; i++)
{
val = val * 10 + input[i] - 48;
}
}
If you want your code to be portable to systems that don't use ASCII, you'll have to loop over your char array and compare each individual character in the source against each possible number character, like so:
int digit;
switch(arr[i]) {
case '0':
digit=0; break;
case '1':
digit=1; break;
// etc
default:
// error handling
}
Then, add the digit to your result variable (after multiplying it by 10).
If you can assume ASCII, you can replace the whole switch statement by this:
if(isdigit(arr[i])) {
digit=arr[i] - '0';
} else {
// error handling
}
This works because in the ASCII table, all digits are found in a single range, in ascending order. By subtracting the ordinal value of the zero character, you get the value of that digit. By adding the isdigit() macro, you additionally ensure that only digit characters are converted in this manner.
I want to write a C program which will take an IP address from the user like "112.234.456.789" in a string and give formatted output in addition of each block in string, e.g., "04.09.15.24" for the above IP address. Here's what I have so far:
#include<stdio.h>
#include<string.h>
#include<conio.h>
main()
{
char s[15],d[11];
int i=0,c = 0, sum[4] = {0};
d[i]=sum[c]/10;
printf("Enter ip address:");
gets(s);
printf("\n \n %s",s);
i=0;
for(c=0;c<15;c++)
{
if(s[c]!='.'||s[c]!='\0')
sum[i]=(s[c]-48)+sum[i];
else
i++;
}
for(i=0,c=0;c<4;c++,i+=3)
{
d[i]=(sum[c]/10)+48;
d[i+1]=sum[c]%10+48;
d[i+2]='.';
}
printf("\n \n %s",d);
getch();
}
The input should be an IP address like "112.234.546.234", and the output should be the result of adding the digits in each block, "04.09.15.06". The input and output should be in strings.
The problem with your code is that s[c]!='.'||s[c]!='\0' is going to evaluate true for any character in the input -- even '.'. This means i is never incremented, and ot only is every digit is summed to sum[0], but so is '.' - 48.
What you meant was s[c] != '.' && s[c] != '\0'.
I wrote the function you desire here.
#include <stdio.h>
#include <ctype.h>
void convert(const char *in, char *out) {
unsigned int sum = 0;
char ch;
do {
ch = *in++;
if (isdigit(ch)) {
sum += ch - '0';
} else {
*out++ = sum / 10 + '0';
*out++ = sum % 10 + '0';
if (ch == '.') {
*out++ = '.';
sum = 0;
}
}
} while (ch);
}
By the way, each "block" of the IPv4 address is an octet, and what you are doing is replacing each with its digit sum.
I just code you a simple example of how to "discard" unwanted characters.
#include <studio.h>
main ()
{
int add1, add2, add3, add4;
printf("enter an ip in the form xxx.xxx.xxx.xxx: )";
scanf("%d%*c%d%*c%d%*c%d", &add1, &add2, &add3, &add4);
printf("add1 = %d add2 = %d add3 = %d add4 = %d\n\n", add1, add2, add3, add4);
return 0;
}
console output:
enter a ip in the form xxx.xxx.xxx.xxx: 123.321.456.654
add1 = 123 add2 = 321 add3 = 456 add4 = 654
EDIT: you just have to play along with the "add#" variables to do your math thing.
It looks like homework (if this is the case, please tag it as homework), so I am going to give a few pointers:
Use fgets to read the input from the user. Read the input into a string.
Use sscanf to parse the string. Since you know there will be four positive integers, use "%u.%u.%u.%u" as the format string.
For each one of the four integers, compute the sum of the digits (using division by 10 and remainder by 10, as you just did).
Print the formatted output using printf (or snprintf to print to a string). If you want each sum to be formatted as a two-digits integer, with leading 0, use "%02u" as format specifier.
P.S. Be careful with snprintf, it might bite.
Other tips
Focus on one step at a time. Divide and conquer. Write a digit_sum function, taking an integer as argument, which computes the sum of its digits:
unsigned int digit_sum(unsigned int n)
{
unsigned int sum = 0;
while (n > 0) {
sum += n % 10;
n /= 10;
}
return sum;
}
Once your digit_sum function is working well, proceed with the main task.
In a program to find whether the given number is an Armstrong number, I stored the input no (3 digit) as string as follows.
char input[10];
scanf("%s",&input);
Now I have to calculate cube of each digit by using pow method of math.h as follows.
int a;
a = pow(input[0],3);
By coding like this, I could not get correct result. If I print the value of "a", it shows some irrelevant answer. My doubt is, how to convert from string value to integer value?
You are performing your calculation on the ASCII value of the digit. You'll need to convert it to a numeric value like so:
int digit = input[0] - '0';
int a; a = pow(digit, 3);
There are two problems, both already detailed. The first is that your scanf needs a char*, not a char**. Fix this with what Jeremy said:
scanf("%s", input);
Next, calculate the power correctly, like Adam said:
a = pow(input[0]-'0',3);
You do not need to get the address of the input array using &input. Simply passing input will pass the pointer to your string to scanf(). Your call to scanf() should look like this:
scanf("%s", input);
Another way of doing it, with the address of operator:
scanf("%s", &input[0]);
armstrong numbers are numbers that exhibit the armstron property in any given base, not just base 10.
int isArmstrong(int n, int b)
{
int sum = 0;
int n2 = n;
int nDigits = 0;
while(n2 != 0)
{
nDigits++;
n2 /= b;
}
n2 = n;
for(int i = 0; i < nDigits; i+++)
{
sum += pow(n2 % b, nDigits);
n2 /= b;
}
return sum == n;
}
On the other hand, you may need to replace the power with a more generic one like power = strlen(input)
so the code should look like this.
char input[10];
int power, sum = 0;
scanf("%s", input);
power = strlen(input);
sum += pow(input[0] - '0', power);
/* you need to compare in here */
Doh, I might have spend too much time far of C, because I think most of the answers here miss the final goal... The advice on dropping the & is fine, of course, but there are several other issues with the scanf.
First, it should be OK for throw away code/homework, but it is dangerous: type 11 chars and get a buffer overflow.
Second, IIRC, since you need a number, you should use an int variable and get that. Untested code from a rusty mind:
int inputNumber;
scanf("%d", &inputNumber);
Here you need the & because you no longer have a pointer.
C should do the conversion for you, and you have a safer input.
Other problematic code:
int a = pow(input[0], 3);
You are not making math with the first digit, but with the Ascii value of the first digit! Ie. 49 for 1, 50 for 2...
Since you got a number from my previous correction, divide it by the correct power of 10 to get the digit.
If you prefer to go the string route, use input[0] - '0' at least.
Freehand coding, building on #PhiLho's idea to use an actual integer input:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int a, sum, i;
printf("Enter an Armstrong number, an integer in the range 100..999:\n");
if(scanf("%d", &a) != 1)
return EXIT_FAILURE;
if(a < 100 || a > 999)
return EXIT_FAILURE;
/* Now extract digits, and compute sum of cubes. */
for(sum = i = 0; i < 3; i++)
{
sum += pow(a % 10, 3);
a /= 10;
}
printf("sum is: %d\n", sum);
return EXIT_SUCCESS;
}
I need to get the number of digits containing the number 1. I know in java I can take the input as a String and use charAt, but I understand there is no implicit String function in C. How can I accomplish this?
Division and modulus are your friends.
#include "stdio.h"
int main(){
int digits[] = {0,0,0,0,0,0,0,0,0,0};
int i = 11031;
while(i > 0){
digits[i % 10]++;
i = i / 10;
}
printf("There are %d ones.\n", digits[1]);
}
Homework?
You'd read it into a char* using the fread() function, and then store how many bytes were read in a separate variable. Then use a for loop to iterate through the buffer and count how many of each byte are present.
Try something like...
int digit = 0;
int value = 11031;
while(value > 0)
{
digit = value % 10;
/* Do something with digit... */
value = value / 10;
}
I see this as a basic understanding problem, which inevitably everyone goes through switching from one language to the next.
A good reference to go through to understand how string's work in C when you've started familiarity with java is look at how string.h works. Where as in java string's are an Object and built in, strings in C are just integer arrays.
There are a lot of tutorials out there, one that helped me when I was starting earlier in the year was http://www.physics.drexel.edu/students/courses/Comp_Phys/General/C_basics/ look at the string section.
Sometimes asking a question speeds up learning a lot faster than pouring through the text book for hours on end.
If you have just the number, then you can do this:
int val; //Input
...
int ones = 0;
while(val != 0) {
ones += ((val % 10) == 1) ? 1 : 0;
val /= 10;
}
If you have a string (char*), the you'd do something like this:
while(*str != '\0') {
if(*str++ == '1') {
ones++;
}
}
It's also worth noting that c does have a charAt function, in a way:
"java".charAt(i) == "c the language"[i];
By indexing into the char*, you can get the value you want, but you need to be careful, because there is no indexOutOfBounds exception. The program will crash if you go over the end of a string, or worse it may continue running, but have a messed up internal state.
Something along the lines of:
int val=11031;
int count=0;
int i=0;
char buf[100];
sprint(buf, "%d", val);
for(i=0; (i < sizeof(buf)) && (buf[i]); i++) {
if(buf[i] == '1')
count++;
}
int count_digit(int nr, int digit) {
int count=0;
while(nr>0) {
if(nr%10==digit)
count++;
nr=nr/10;
}
return count;
}
This sounds like a homework problem to me. Oh well, it's your life.
You failed to specify the type of the variable that contains the "input integer". If the input integer is an integral type (say, an "int") try this:
int countOnes(int input)
{
int result = 0;
while(input) {
result += ((input%10)==1);
result /= 10;
}
return result;
}
If the "input integer" is in a string, try this:
int countOnes(char *input)
{
int result = 0;
while(input && *input) {
result += (*input++ == '1');
}
return result;
}
Hope this helps. Next time, do your own homework. And get off of my lawn! Kids, these days, ...
int countDigit(int Number, int Digit)
{
int counter = 0;
do
{
if( (Number%10) == Digit)
{
counter++;
}
}while(Digit>0)
return counter;
}
Something along the lines of this:
#include <stdio.h>
main() {
char buf[100];
char *p = buf;
int n = 0;
scanf("%s", buf);
while (*p) {
if (*p == '1') {
n++;
}
p++;
}
printf ("'%s' contains %i ones\n", buf, n);
}
This will do it. :-)
int count_digits(int n, int d) {
int count = 0;
while(n*10/=10) if (n%10==d) count++
return count;
}
For all those who refer to the question as the homework question: I have to say, most of you provided a homework answer.
You don't do division/modulus to get the digits in production code, firstly because it's suboptimal (your CPU is designed for binary arithmetics not decimal) and secondly because it's unintuitive. Even if it's not originally a string, it's more optimal to convert it to one and then count the characters (std::count is the way to go in C++).