Pulling Digits Out of String to Sum Them

Pulling Digits Out of String to Sum Them

How do I pull out every second digit from a string and sum them?

This is a rather unusual question, but it’s not that hard to accomplish.

What must iterate through each character of the string and”grab” the digits whose position is a multiple of two (2). There are acouple of ways to do this, but I took what I felt was the easierroute. Since this is more or less a binary problem, setting a Boolean valuewith each iteration works nicely. Here’s the logic:

  1. For all “odd” positions, set Boolean value to False;
  2. For all “even” positions, set Boolean value to True;
  3. If the Boolean value is true, grab that character and add it to a temporary buffer.
  4. Iterate through the buffer, and convert each character to an Integer while adding the converted value to an integer variable.

Here’s the code that accomplishes the above:

function AddEvenOrOddChars(S : String; OddOrEven : Boolean) : Integer;var  I   : Integer;  evn : Boolean;  buf : String;begin  Result := 0;  {If OddOrEven was passed as True, then the all odd positions   will be grabbed and summed. If False, then all even positions   will be grabbed and summed.}  evn := EvenOdd;  {First grab the even position characters}  for I := 1 to Length(S) do begin    if evn then      buf := buf + S[I];    {Set boolean to its opposite regardless of its current value.     If it’s currently true, then we’ve just grabbed a character.     Setting it to False will make the program skip the next one.}    evn := NOT evn;  end;  {Now, iterate through the buffer variable to add up the individual   values}  for I := 1 to Length(buf) do    Result := Result + StrToInt(buf[I]);end;

Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes