![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Nov 2005
Posts: 3
Rep Power: 0
![]() |
Hello, I am presented with the following problem and have tried desperately (over 12 hours) to solve it:
I have a string say "011010101" and I want to parse this string based on a table of values: i = 0 m = 011 k = 10 e = 101 I want to construct an algorithm that will go through the string above and parse it assigning each value to their corresponding table value. Looking at it, I know that the string would become "011" , "0" , "10" , "101" which for this example would be "mike" in that order. Since "i" is the first thing I check, my code tries to form "0", "11010101", and nothing else will get extracted. There can be more than one correct parsing and also the table of values includes all the letters (uppercase and lowercase) as well as ' ', '\n', and '\t'. Any help would be greatly appreciated. |
|
|
|
|
|
#2 | |
|
Hobbyist Programmer
Join Date: Oct 2005
Posts: 211
Rep Power: 3
![]() |
Quote:
|
|
|
|
|
|
|
#3 |
|
Expert Programmer
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4
![]() |
Google for bitshifting, you'll probably find some tuts.
|
|
|
|
|
|
#4 |
|
Newbie
Join Date: Nov 2005
Posts: 3
Rep Power: 0
![]() |
string ConvertCodes( string binary )
{
// what to return
string retval = "";
// what to restore the passed in value to just incase
string tempCopy = binary;
foreach( PairItem i in CodeList )
{
// if the CODE (i.e. "011") is a prefix of the passed in string
if( binary.StartsWith( i.CODE ) )
{
// add the symbol (i.e. "i") to the return value string
retval+=i.Symbol.ToString();
// update the passed in string for further checking
binary = binary.SubString( i.CODE.Length );
// check the update string, if it equals "", there were no further matches
if( ConvertCodes(binary) == "" )
{
// reset the return value
retval = "";
// restore the passed in string
binary = tempCopy;
}
}
}
return retval
}I ran through this code in debug mode for a simple string, and it found the correct return value, but it still returned "". |
|
|
|
|
|
#5 | |
|
Hobbyist Programmer
Join Date: Oct 2005
Posts: 211
Rep Power: 3
![]() |
Quote:
Look closer at your termination case, what's returned by : ConvertCodes("") ; // (should be "") now what's returned by ConvertCodes("011"); hint: if( ConvertCodes(binary) == "" )
{
// reset the return value
retval = "";
// restore the passed in string
binary = tempCopy;
}That should help out with one character 'strings'. Once you get that, you also have no ability to create multi character strings. You're never getting/saving / returning information from characters after the first. -MBirchmeier (feel free to ask questions) |
|
|
|
|
|
|
#6 |
|
Newbie
Join Date: Nov 2005
Posts: 3
Rep Power: 0
![]() |
I ran my code with those two values and I got the same return value. I know this is because when I get done with the algorithm, no matter what, at some point ConverCodes(binary) will equal "", so I return "" no matter what, even if I pass in one character string. How would I go about solving this?
|
|
|
|
|
|
#7 | |
|
Hobbyist Programmer
Join Date: Oct 2005
Posts: 211
Rep Power: 3
![]() |
Quote:
1.) You've reached the end of the string 2.) The rest of the string is a valid series of characters 3.) The rest of the string is not a valid series of characters -MBirchmeier |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|