Click here to Skip to main content
15,881,380 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i need help from the community here. My problem is as follows:

I have some string arrays like
arr1[][]="p","ph"; are the confusions for 'p'
arr2[][]="a","A"; are the confusions for 'p'
arr3[][]="s',"sh"; are the confusions for 's'
arr4[][]="o","O"; are the confusions for 'o'
arr5[][]="l","n"; are the confusions for 'l'
arr6[][]="i","I"; are the confusions for 'i'
etc...

the above arrays shows the confusions for each of them. Now I have to build the original string "pasoli" as multiple strings like
pasoli
phasoli
pAsoli
phAsoli
pasholi
pAsholi 
etc...

Here all the different forms of strings to be generated by using: for 'p' we can have 'p' or 'ph', for 'a' we have 'a' or 'A'. This way we have to go on substituting and find the string.

"patal" as "phatal" or "pAtal" or "pathal" etc


Any help is greatly appreciated, thanks.
Posted
Updated 11-Apr-12 23:56pm
v2
Comments
Slacker007 12-Apr-12 5:56am    
Edited for readability and formatting.

1 solution

For an efficient solution, you can work as follows:

- declare an array of strings that is indexed by all possible character values (128 entries will do for ASCII characters); let it be Confusions;

- every entry in the array gives the list of possible confusions; the string representation such as "p|ph", "a|A"... uses a reserved separator (such as vertical bar); this allows you to specify no confusion 'x' -> "x", binary confusion 'a' -> "a|A", or multiple confusions 'o' -> "o|O|0".

- now declare a substitution function that will be called recursively. It takes on input the Nth character of the input string, and a corresponding output string; lookup the confusion string for this character and perform as many recursive calls as there are chunks in the confusion string, while appending the chunck to the current output string.

In pseudo-code:

C++
Subsitute(int N, char In[], char Out[])
    if In[N] == '\0'
        print Out
    else
        for every Chunk in Confusion[In[N]]
            Substitue(N+1, In, Concatenete(Out, Chunk));


You will call it with

C++
Substitue(0, In, "")


Example of what recursion will do:

C++
(0, "pasoli", "")
    (1, "pasoli", "p")
        (2, "pasoli", "pa")
            (3, "pasoli", "pas")
                (4, "pasoli", "paso")
                    (5, "pasoli", "pasol")
                    ...
                    (5, "pasoli", "paso1")
                    ...
                (4, "pasoli", "pasO")
                    (5, "pasoli", "pasOl")
                    ...
                    (5, "pasoli", "pasO1")
                    ...
                (4, "pasoli", "pas0")
                    (5, "pasoli", "pas0l")
                    ...
                    (5, "pasoli", "pas01")
                    ...
        (2, "pasoli", "pA")
        ...
    (1, "pasoli", "ph")
    ....


Here is Python code that does it (sorry, not C):
C++
Confusions= { "p": ["p", "ph"], "a": ["a", "A"], "o": ["o", "O", "0"], "l": ["l", "1"], "i": ["i", "1"] }

def Substitute(N, In, Out):
    if N == len(In):
        # Done with the input string
        print Out
    else:
        if In[N] in Confusions:
            # Substitute every chunk
            for Chunk in Confusions[In[N]]:
                Substitute(N + 1, In, Out + Chunk)
        else:
            # No rule for this letter, treat as a chunk
            Substitute(N + 1, In, Out + In[N])
         
   
Substitute(0, "pasoli", "")


pasoli
pasol1
paso1i
paso11
pasOli
pasOl1
pasO1i
pasO11
pas0li
pas0l1
pas01i
pas011
pAsoli
pAsol1
pAso1i
pAso11
pAsOli
pAsOl1
pAsO1i
pAsO11
pAs0li
pAs0l1
pAs01i
pAs011
phasoli
phasol1
phaso1i
phaso11
phasOli
phasOl1
phasO1i
phasO11
phas0li
phas0l1
phas01i
phas011
phAsoli
phAsol1
phAso1i
phAso11
phAsOli
phAsOl1
phAsO1i
phAsO11
phAs0li
phAs0l1
phAs01i
phAs011
 
Share this answer
 
v4
Comments
Sandeep Mewara 23-Apr-12 11:01am    
My 5!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900