Warm tip: This article is reproduced from serverfault.com, please click

Juxtaposing integer ASCII codes for alphabet

发布于 2020-12-09 07:50:18

I want to convert Name and Surname(e.g., Nova Stark) into a large integer by juxtaposing integer ASCII codes for alphabet, print the corresponding converted integer then cut the large integer into two halves and add the two halves.Following is my approach:-

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

    char* arr2str(int arr[], int size) {
        static char buffer[256];
        memset(&buffer[0], 0, sizeof(buffer)/sizeof(char));
        char *ptr = &buffer[0];
        for(int i=0; i<size; ++i) {
            sprintf(ptr += strlen(ptr), "%d", arr[i]);
        }
        return buffer;
    }

    int arr2int(int arr[], int size)
       {
        char buffer[256] = {0,};
        char *ptr = &buffer[0];
        for(int i = 0; i < size; ++i) {
            sprintf(ptr += strlen(ptr), "%d", arr[i]);
        }
        return atoi(&buffer[0]);
    }

int main()
{
    int *A;
    long long int num;
    int div,base=10;
    char name[50],asc[200];
    printf("Enter your name : ");
    scanf(" %[^\n]",name);
    int len=strlen(name);
    A=(int*)malloc(len*sizeof(int));
    for(int i=0;i<len;i++)
    {
        A[i]=name[i];

    }
    char *str = arr2str(A, len);  //for converting array to string
    num = arr2int(A, len);         //again for converting the character array to integer.
    //num=array_to_num(A,len);
    div=base;
    while(num/div>div)
    {
        div=div*base;
    }
    long long int a=num/div;
    long long int b=num%div;
    long long int c=a+b;
    printf("The required integer is %lld and the sum is %lld ",num,c);
    return 0;
}

But I am not getting the desired output. Please help! SS of my output Also, if there exists a simpler approach to the problem please specify that too.

Questioner
Nova Stark
Viewed
11
ralf htp 2020-12-09 18:55:31

Following is running, for explanations see comments in the code. Maybe you have to handle the whitespaces in the name:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>



int main()
{
    int *A;
    long long int num;
    int div,base=10;
    char name[50];
    printf("Enter your name : ");
    scanf(" %[^\n]",name);
    int len=strlen(name);
    A=(int*)malloc(len*sizeof(int));
    for(int i=0;i<len;i++)
    {
        *(A+i)=(int)name[i];  // *(A+i) is accessing array as pointer+index : https://www.programiz.com/c-programming/c-dynamic-memory-allocation 
    // letters to ascii is done by simply typecasting to (int)

    }
    
    // string to int array
    
    for(int i=0;i<len;i++)
    {
        printf("%i -> %c \n", *(A+i), (char)(*(A+i)));

    }
     
     
    // long long int from concatenating the elements of the int array 
    
    
    int s_idx=0; // index for string
    char str[512];  //string of fixed size, possibly malloc this
     
    for (int i=0; i<len; i++)
    s_idx += snprintf(&str[s_idx], 512-s_idx, "%d", *(A+i));
    
    printf("%s \n", str);
   

    num = strtoll(str, NULL, 10);  // https://en.cppreference.com/w/c/string/byte/strtol

    
    // from here your code is unchanged
    
    
    div=base;
    while(num/div>div)
    {
        div=div*base;
    }
    long long int a=num/div;
    long long int b=num%div;
    long long int c=a+b;
    printf("The required integer is %lld and the sum is %lld \n ",num,c);
    free(A);
    return 0;
}