i need to split a decimal number in such a way that each unit is between 1 and 26

ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Welcome to LinuxQuestions.org, a friendly and active Linux Community.

You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!

Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.

If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.

Having a problem logging in? Please visit this page to clear all LQ-related cookies.

Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.

Exclusive for LQ members, get up to 45% off per month. Click here for more info.

i need to split a decimal number in such a way that each unit is between 1 and 26

i need to split a decimal number in such a way that each unit is between 1 and 26. for eg if i input a number 2346 it should split it into 23 , 4 , 6 i've been trying for hours to do this but in vain. i've tried this code

I'm assuming you want to translate a number into letters of the alphabet.... the problem is that the data can be interpreted in too many different ways if it's not structured somehow

i need to split a decimal number in such a way that each unit is between 1 and 26. for eg if i input a number 2346 it should split it into 23 , 4 , 6

My first reaction is WHY?? Is this a homework problem?

First, as already stated, you need to be clear about the criteria. Your example implies that the rule is:
pick the largest number of digits that fall in the range 1-26, clip them, repeat. Thus--eg--124234216 would be split:
12,4,23,4,21,6

Also, does it need to be done in C? BASH or Python might be easier.

I have thrown something together in c for you which will do this. I have just use a temporary array of 100 ints rather than dynamically allocating and re-allocating memory as the size of the argument grows. If you want more, either increase the array size or go the dynamic route. It will always pick the largest number first (ie, for 123 it would pick 12 then 3 rather than 1 then 23). It works on the assumption that there will never be more than two characters making up a number less than 26 - if you have zeros then 02 is assumed to be the number two. It will also assume that 00 is the number zero.
Yes, there are complications and cases where it may not work, but for basic stuff this will do the job. It's not the cleanest of code because I have just thrown it together. I know the tmp pointers could have been done without malloc'ing but I had done it a different way first and couldn't be bothered re-writing it!

Code:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(int argc, char **argv) {
int i=0;
int results[100];
int n=0;
char *tmpone, *tmptwo;
tmpone=(char*)malloc(sizeof(char));
tmptwo=(char*)malloc(sizeof(char)*2);
while (i<strlen(argv[1])) {
int one, two;
/*One Digit*/
strncpy(tmpone,&argv[1][i],1);
one=atoi(tmpone);
/*Two digits*/
strncpy(tmptwo,&argv[1][i],2);
two=atoi(tmptwo);
if (two<=26) {
results[n++]=two;
i+=2;
}
else {
results[n++]=one;
i++;
}
} /*While Loop*/
free(tmpone);
free(tmptwo);
/*Loop through the results array*/
for (i=0;i<n;i++) {
printf("Result %i = %i\n",i,results[i]);
}
return 0;
}

A few example runs:

Code:

$ ./x 2346
Result 0 = 23
Result 1 = 4
Result 2 = 6
$ ./x 1843154848976318646848615313573575
Result 0 = 18
Result 1 = 4
Result 2 = 3
Result 3 = 15
Result 4 = 4
Result 5 = 8
Result 6 = 4
Result 7 = 8
Result 8 = 9
Result 9 = 7
Result 10 = 6
Result 11 = 3
Result 12 = 18
Result 13 = 6
Result 14 = 4
Result 15 = 6
Result 16 = 8
Result 17 = 4
Result 18 = 8
Result 19 = 6
Result 20 = 15
Result 21 = 3
Result 22 = 13
Result 23 = 5
Result 24 = 7
Result 25 = 3
Result 26 = 5
Result 27 = 7
Result 28 = 5
$ ./x 0125451012104
Result 0 = 1
Result 1 = 25
Result 2 = 4
Result 3 = 5
Result 4 = 10
Result 5 = 12
Result 6 = 10
Result 7 = 4
$ ./x 01254510012104
Result 0 = 1
Result 1 = 25
Result 2 = 4
Result 3 = 5
Result 4 = 10
Result 5 = 1
Result 6 = 21
Result 7 = 4

Looks like an attempt to decode numbers into words. If so, I'd get the word list by reading the "/usr/share/dict/words" file, translate each one to its numeric equivalent, and build a decoding map with that. There's going to be some ambiguity; e.g. 13135 could be either "acme" or "mace".

LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.