Monday, May 24, 2010

How do i write a c language program that gives the output as all possible combinations of a string input by?

user.

How do i write a c language program that gives the output as all possible combinations of a string input by?
As amazing as this may sound, this is something you can actually write in approximately 20 lines of code. You can use a FOR loop inside of a recursive function to do most of the work. Use the FOR loop to break down a string into smaller sub-sections for each recursion. This example will hopefully give you an idea of what happens.





eg.





[abcd]





├ [a] - [bcd]





├┬ [ab] - [cd]


││


│├┬ [abc] - [d]


│││


││└─ [abcd] - [ ]


││


│└┬ [abd] - [c]


│   │


│   └─ [abdc] - [ ]





├ [b] - [acd]








Pay close attention to these two lines.





├ [a] - [bcd]





├┬ [ab] - [cd]





In the second string, [bcd], the first character, [b], is appended to the first string, [a]. In the next step,





├┬ [ab] - [cd]


││


│├┬ [abc] - [d]








the same thing occurs. In the last step,





│├┬ [abc] - [d]


│││


││└─ [abcd] - [ ]





the same thing occurs. However, this time the second string has no characters to add to the first string, so you would add the first string to an array or output it. In the next step,





├┬ [ab] - [cd]


││


│├┬


│││


││└─


││


│└┬ [abd] - [c]








you'll notice the second character of the second string is appended to the first string. This is where you want to use a FOR loop to cycle through the string. I'll leave it up to you on how to actually implement this. If you need any more help or are confused, send me an email.
Reply:could u explain your problem statement better...


No comments:

Post a Comment