# Runlength Decoding (

1. Mar 20, 2013

### sandy.bridge

Hey. I am working on an algorithm that receives an input in the form "4A3B2H9P" (depending on the users choice of input), and I want it to output "AAAABBBHHPPPPPPPPP". I have tried searching online for ways to "scan" the string, but I cannot seem to find anything. Same goes for me textbook.

Is there a way to scan, from left to right, the string, such that it stops scanning the number once a letter is reached? For example "12A4B", stop scanning after the two, detects 12. Then, scans further and scans until 4, detects A.

Or is there some easier method I can employ? Thanks in advance.

2. Mar 20, 2013

### phinds

It's trivial. It DOES of course depend, in the specifics, on what language you are using as to what specific system function(s) you use.

3. Mar 20, 2013

### AlephZero

You can do this is any language by reading one character at a time. If it is a digit, use it to update the number of repetitions. If not, write some output.

In C or C++, the functions scanf() will do what you want to read the input. It stops reading at the first character that doesn't match the fiormat you specify, so the format "%d%c" will read an integer (with any number of digits) and then a single character. It also returns the amount of data it read, so you can check for invalid input like "12AB" (with no integers before the B).

4. Mar 20, 2013

### sandy.bridge

I decided to stay away from using scanf(). I am now using cin with a loop.

PHP:
int N; char ch; char dy;
cin >> N;
cin >> dy; // Read the colon
cin >> ch;
for (int k=0; k<N; k++)
{
cout << ch;
}

This code ends up getting the first set of characters displayed. For example, if 5:B6:G was input, BBBBB was output. Can I employ a loop to get the rest of string to display?

Last edited: Mar 20, 2013
5. Mar 20, 2013

### d3mm

In school you might be taught to do this by reading the characters one at a time then doing what you will with them

Code (Text):
>>> ip = "12A3B"
>>> for x in range (len (ip)):
...   print ip [x]
...
1
2
A
3
B

So here you would look at the number, read it, convert it to int, emit that many of the next character. As was said before me, scanf() will do this too without needing to convert to int, but it's about as secure and robust as leaving the door open at burglars convention.

In a commercial environment you would be slapped hard by your boss for not using regex.

Code (Text):

lisa@veeshan:$cat er.py import re ip = "4A3B2H9P" while ip: match = re.match ("(\d+)([A-Z])", ip) if match: print int (match.group (1)) * match.group (2) ip = ip [len (match.group(0)):] lisa@veeshan:$
lisa@veeshan:$python er.py AAAA BBB HH PPPPPPPPP lisa@veeshan:$

(\d+) matches one or more digits ... (\d) is digit and + means "or more"

[A-Z] matches a letter (case sensitive)

The brackets break the expression into groups. Group 0 is the whole matched part, group 1 is the digit and group 2 is the letter.

6. Mar 20, 2013

### sandy.bridge

I am using c++, btw. I updated my second post.

7. Mar 20, 2013

### d3mm

You're not "supposed" to use scanf() in C++. It's not depreciated but it is no longer recommended. Your compiler probably provides a "safe" version of scanf() so that hackers don't pwn your computer with dodgy inputs :-)

However, in C++ you are "supposed" to use <istringstream> and/or <regex> from the standard library.

8. Mar 20, 2013

### AlephZero

In any "serious" C++ code, I would never use the >> and << I/O operators for anythng whatever. I don't have time to learn how to avoid all the 32767 "features" caused by the standards comittee overdosing on operator overloading and automatic conversions. (and the number of features probably goes up to 2147483647 on 64-bit hardware)

9. Mar 20, 2013

### sandy.bridge

I ended up posting what I have so far in the homework section (engineering and computer science). I am really close to having this working to perfection, however, I am having an error somewhere in my assignment of the 2D array that I am using.

10. Mar 21, 2013

### harborsparrow

This belongs in Homework. Also, using Regex (as was suggested by one reader) is overkill in this case. Regular expressions, while powerful and sometimes needed, also make code more difficult to read later, and so should probably be avoided for simple problems like this one. At least IMHO.

11. Mar 21, 2013