Home > database >  How to convert each number in a num String to an int in C
How to convert each number in a num String to an int in C

Time:03-21

I've been trying to do this problem on Project Euler

This is what I've done so far -

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(){
    char nums[] = "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450"
    int64_t sum = 1, new_sum = 0;
    int num;
    for(int i = 0; i < 988; i = 13){
        sum = 1
        for(int j = 0; j < 13;   j){
            //Converting nums[i] to int and storing it to num here
            sum *= num;
        }
        if(sum > new_sum){
            new_sum = sum;
        }
    }
    printf("%lld", new_sum);
}

I don't know how to convert each charNum in the String to an integer, I've tried atoi and sscanf and both of them ask for a char pointer.

I get these errors respectively

passing argument to parameter here
int      atoi(const char *); //sum *= atoi(nums[i])
format specifies type 'int *' but the argument has type 'int' [-Wformat]
            sscanf(nums[i], "%d", num);

Any help is appreciated.

CodePudding user response:

Just converting a character representing a digit to the digit itself is simply done by

int digit = nums[position] - '0';

C language guarantees for any character set that the digits 0-9 are succeeding one another in exactly that order (which is not necessarily the case for alphabetic characters, see e.g. (in-?) famous EBCDIC).

As I read the problem the maximum can be at any location, not only multiples of 13. For this speaks as well that the maximum of four subsequent digits is found at a location where the number of digits preceding is not a multiple of four.

Apart from, unless the number if digits is a multiple of 13, your loops would exceed array bounds of nums, so undefined behaviour (if not a multiple of 13, then for last iteration of i there are less than 13 digits left, but j still tries to iterate over all 13 digits).

Fixing both:

size_t sequenceLength = 13; // could be a function parameter

size_t len = strlen(nums); // sizeof would include the terminating 0 character!

if(len < sequenceLength)
{
    return 0; // if within a function, else whatever appropriate error handling
}

uint64_t max = 1; // can't have negative values anyway...
for(size_t i = 0; i < sequenceLength;   i)
{
    max *= nums[i] - '0'; // see above
}
// now we found first possible maximum: the product of first 13 digits

uint64_t num = max;
for(size_t i = sequenceLength; i < len;   i)
{
    // for next product, we have already included the first 12 digits
    // within the previous product!

    // but there is one surplus digit contained we need to eliminate:
    num /= nums[i - sequenceLength] - '0';

    // now we can include the yet missing one:
    num *= nums[i] - '0';

    // or as a one-liner:
    num = num / (nums[i - sequenceLength] - '0') * (nums[i] - '0');

    // TODO: update maximum, if need be, just as in your code
}

CodePudding user response:

Would you please try the following:

#include <stdio.h>
#include <stdlib.h>
#define N 13

int main() {
    char nums[] = "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450";
    int64_t prod = 1, max_prod = 0;
    int num;
    int pos;
    for (int i = 0; i < sizeof nums; i  = N) {
        prod = 1;
        for (int j = 0; j < N; j  ){
            // Converting nums[i] to int and storing it to num here
            num = nums[i   j] - '0';
            prod *= num;
        }
        if (prod > max_prod) {
            max_prod = prod;
            pos = i;
        }
    }
    printf("max product = %ld at %.*s\n", max_prod, N, nums   pos);
}

Output:

max product = 6270566400 at 4355766896648

BTW as the variable name sum is misleading, I've changed it to prod for product.

CodePudding user response:

I assume that you want a nuber for each digit (i.e., range 0 to 9) If yes, the below code should be not too far from what you need:

char nums[] = "73167176531330624919225119674426574742355349194934969835203... (so long)...";

char *p; 
long lTheTotalCount, lTheNumber;

lTheTotalCount = 0;
for (p=nums ; *p ; p  ) {
   lTheNumber = (long)(*p - '0');
   lTheTotalCount  = lTheNumber;
};
  • Related