Skip to main content

Notifications

Finance | Project Operations, Human Resources, ...
Answered

How to find Sum of odd and even characters of string

(0) ShareShare
ReportReport
Posted on by
Hi, 
 
I have a requirement where I have a string input where I have to find the sum of all even characters and multiply it with 3 and similarly find sum of all odd characters and multiply it with 1 and then sum all of it together and find mod of 10(i.e. if sum of odd + even = 44 then 44 mod 10 = 6, i should get 6 as the output)
How do i achieve the same using x++ code?
 
 
  • Martin Dráb Profile Picture
    Martin Dráb 230,853 Most Valuable Professional on at
    How to find Sum of odd and even characters of string
    I'm sorry, but I still don't understand what you want from me. You'll have to be much more specific if you want to get an answer.
  • CU02071804-0 Profile Picture
    CU02071804-0 on at
    How to find Sum of odd and even characters of string
    i understand here the calculation looks identical but if we consider the scenario of bar code calculation then the output might differ, so considering that i wanted to know the x++ code
  • Martin Dráb Profile Picture
    Martin Dráb 230,853 Most Valuable Professional on at
    How to find Sum of odd and even characters of string
    Please explain the difference. It looks identical to me.
  • CU02071804-0 Profile Picture
    CU02071804-0 on at
    How to find Sum of odd and even characters of string
    Yes Martin the code you gave did the job all i am asking you if we have scenario like the one below how ill we proceed with the code, can you please let me know the x++ code for the scenario.
    if strinput = 2143658790
     
     even str1 = 1*1 =1
     even str2 = 3*1 = 3
    even str3 = 5*1 = 5
    even str4 = 7*1 = 7
    3v3n str5 = 0*1 = 0
    then evenSum = 2+3+5+7+0 = 16
     
    odd str1 = 2*3 = 6
    odd str2 = 4*3 = 12
    odd str3 = 6*3 = 18
    odd str4 = 8*3 = 24
    odd str5 = 9*3 = 27
     
      then oddSum = 6+12+18+24+27 = 87
           
    then,
     
           sum = ((16+87) mod 10)   =    (103 mod 10) = 7
       hence sum = 7
     
    This s the mathematical overview of my requirement how do i achieve it through x++  
  • Martin Dráb Profile Picture
    Martin Dráb 230,853 Most Valuable Professional on at
    How to find Sum of odd and even characters of string
    I'm sorry, but it's not clear to me what problem you have.
     
    It seems to be the same thing that we discussed before and you confirmed that the code I gave you did the job.
  • CU02071804-0 Profile Picture
    CU02071804-0 on at
    How to find Sum of odd and even characters of string
    Hi Martin how would i achieve it if 
     
    if strinput = 2143658790
     
     even str1 = 1*1 =1
     even str2 = 3*1 = 3
    even str3 = 5*1 = 5
    even str4 = 7*1 = 7
    3v3n str5 = 0*1 = 0
    then evenSum = 2+3+5+7+0 = 16
     
    odd str1 = 2*3 = 6
    odd str2 = 4*3 = 12
    odd str3 = 6*3 = 18
    odd str4 = 8*3 = 24
    odd str5 = 9*3 = 27
     
      then oddSum = 6+12+18+24+27 = 87
           
    then,
     
           sum = ((16+87) mod 10)   =    (103 mod 10) = 7
       hence sum = 7
     
    This s the mathematical overview of my requirement how do i achieve it through x++  
  • CU02071804-0 Profile Picture
    CU02071804-0 on at
    How to find Sum of odd and even characters of string
    Thanks Martin
     
    this solves the problem
  • Verified answer
    Martin Dráb Profile Picture
    Martin Dráb 230,853 Most Valuable Professional on at
    How to find Sum of odd and even characters of string
    I already showed how to get the characters, therefore there are just a few small things to add:
    str input = '2143658790';
    str numChars = strKeep(input, '0123456789');
    
    if (strLen(input) != strLen(numChars))
    {
        throw error("Input contains invalid characters.");
    }
    
    int oddSum;
    int evenSum;
    
    for (int i = 1; i <= strLen(numChars); i++)
    {
        int digit = str2Int(subStr(numChars, i, 1));
    
        if (i mod 2)
        {
            evenSum += digit;
        }
        else
        {
            oddSum += digit;
        }
    }
    
    int checksum = (evenSum + oddSum * 3) mod 10;
  • CU02071804-0 Profile Picture
    CU02071804-0 on at
    How to find Sum of odd and even characters of string
    Hi Martin my requirement says that 
     
    if strinput = 2143658790
     
    then evenSum = 1+3+5+7 +0 = 16
            evenmultiply = 16*1 = 16
     
            oddSUm = 2+4+6+8+9=  29 
            oddmultiply = 29*3 = 87
    then,
     
           sum = ((16+87) mod 10)   =    (103 mod 10) = 7
       hence sum = 7
     
    This s the mathematical overview of my requirement how do i achieve it through x++     
     
  • Martin Dráb Profile Picture
    Martin Dráb 230,853 Most Valuable Professional on at
    How to find Sum of odd and even characters of string
    What do you mean by a sum of characters? Do you expect numbers only and no other characters? Or do you want to support other characters too (e.g. letter, punctuation or so) and somehow convert them to numbers?
     
    Regarding how to iterate characters, this is a native X++ way:
    for (int i = 1; i <= strLen(inputString); i++)
    {
        subStr(inputString, i, 1);
    }
    Another option is using .NET classes. You could use String.ToCharArray() to convert the string to an array of characters, which then allows you to write logic for indexes that are divisible by two (mod 2) and the rest.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Congratulations to the January Top 10 leaders!

Check out the January community rock stars...

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,996 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 230,853 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans