Question

I'm trying to convert a long long integer to a string array with the 1's column in position 0 of the array, the 10's column in position 1, the 100's column in position 2, like this:

INPUT: 4444555566667777 -----> OUTPUT: [4,4,4,4,5,5,5,5,6,6,6,6,7,7,7,7]

To test my code, I wrote for my last line printf("%d\n",number_str[3]). I expected my program to output the value of position 4, "7". Instead, it output "52". Changing my last line to printf("%d\n",number_str[4]) yields "53" instead of "6", as I expected. Can anyone explain what's going on?

Surely 52 and 53 correspond to ASCII values, but then, how can I convert them to integers? Can I do this in line?

Where I'm headed with this part of my program is to add up all of the numbers in the 10's, 1,000's, 100,000's, 10,000,000's... columns. Every other digit in a base-10 credit card number. This is one step in my attempt at a Luhn validation.

// Libraries
#include <cs50.h>
#include <stdio.h>
#include <string.h>

// Program 
int main(void)
{
    // Get CC number
    printf("Enter your credit card number: ");
    long long number_ll = GetLongLong();
    // printf("%lld\n", number_ll);

    // Convert long long to string 
    char number_str[64];
    sprintf(number_str, "%lld", number_ll);
    // printf("%s\n", number_str);

    // Get length of card number string
    int cclength = strlen(number_str);

    // Check the length of card number string
    if ( ! (cclength == 13 || cclength == 15 || cclength == 16))
        printf("INVALID\n");
    else  
        printf("%d\n",number_str[3]);
Was it helpful?

Solution 2

Using "%d" on a char will print its ASCII code. Use "%c" to print itself.

And your string's order is reversed compared to your purpose. The rightmost digit(1's column) is at the tail of the string, and the leftmost one is in position 0.

So to print the number at position i (count from right to left), you should use:

printf("%c\n", number_str[cclength - i - 1]);

OTHER TIPS

To convert ascii into integer use

#include <stdlib.h>

int atoi(const char *str);

change this

printf("%d\n",number_str[3]);

to

char buf[2];
buf[0]=number_str[3];
buf[1]='\0';
printf("%d\n",atoi((const char*)buf));

I'm going to go ahead and expand on my comment since I don't believe either of the other answers responded to your full question.

By reading the CC number into a long long, and then using sprintf to plug the number into a character array, I would say you're correctly getting the number into a form that you can use for validation. In fact, you can check the return value of sprintf to see whether or not it's a valid number (although a failure case would be unlikely since you're plugging in a long long.

Once you have the CC number in a character array, you know that each element of the array will contain one character, which corresponds to one digit in the CC number. It sounds like for your purposes, it's more useful for the values in the array to be the decimal values, rather than the ASCII values. Logically, this is the difference between the values '0' and 0. You can look up any ASCII chart to see the corresponding ASCII value for each character, but since characters can be manipulated just like integers, you can traverse the array:

for(i = 0; i < 64; i++) num_str[i] -= '0';

Note that this doesn't handle there being less than 64 characters or uninitialized values in the array after the CC number characters, so you'll need to modify it. What's important to realize is that you're just shifting the character values down by '0', which happens to have the integer value 48.

Once you do this conversion, printing out a value in the array with printf using %d as the format specifier will work like you expect; even though the array data type is char, each element may be printed as a decimal integer.

Once you've read the number into the char array and made the conversion, all you need to do is traverse the array again, performing whatever steps are involved in the CC Validation process. You may need to traverse the array in reverse if the validation method requires the digits to be in order.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top