Hi,
I need a C# function that takes 2 strings as an input and return an array of strings.
private string[] FunctionName (string string1, string string2)
{
//code
}
The strings input will be in the following format:
String1 eg -> basement
String2 eg -> **a*f**a
Now what I need is all combination of possible strings using the characters in String2, and keeping them in the same character position.
Eg: baaement, baaefent, baaefena, basefent, basemena, etc
any help? :)
Amit ChoudharyPosted Mar 9, 2010, 7:59 AM
I have attached the file here. Now i want you to mark this post as answer.
Amit ChoudharyPosted Mar 10, 2010, 12:20 AM
input string: botle
Substitute string: **1*2
use trim function for input string and substitute string...
like s1=s1.Trim(); and s2=s2.Trim() it'll remove all the leading and trailing spaces if comes by accident.
I think the last post was working for you.
Cheers coding
Sa ZaPosted Mar 9, 2010, 8:59 AM
I changes to the following part of the code:
temp = source;
for (int j = 0; j < Tokens[i].Length; j++)
{
string token = Tokens[i];
if (sourceSubst.IndexOf(token[j]) == loc[j])
{
temp = temp.Remove(loc[j], 1);
temp = temp.Insert(loc[j], token[j].ToString());
}
else
{
temp = temp.Remove(sourceSubst.IndexOf(token[j]), 1);
temp = temp.Insert(sourceSubst.IndexOf(token[j]), token[j].ToString());
}
}
and it seems to be working fine.... can you please confirm please??
Sa ZaPosted Mar 9, 2010, 8:10 AM
since you used 'AxxoSt' as a testing string, you did not captured the following error:
When I tried with the following I got the following result:
input string: botle
Substitute string: **1*2
============Output============
bo1l2 -> correct
bole2 -> should be botl2
bo1le -> correct
Sa ZaPosted Mar 9, 2010, 7:46 AM
I am seeing the code and the problem is in the following part:
for (int j = 0; j < Tokens[i].Length; j++)
{
string token = Tokens[i]; //get char to change
temp = temp.Remove(loc[j], 1);//remove it from orig string
temp = temp.Insert(loc[j], token[j].ToString()); //replace it with the new char
}
because when you are replacing one character, it is always taking the first position.
Note that s2 can have identical characters at different possitions (eg: **a*ac)
Amit ChoudharyPosted Mar 9, 2010, 7:39 AM
Sa ZaPosted Mar 9, 2010, 6:45 AM
I did not understood your last post, maybe I was not clear :)
When I run the program after the fixes you gave me the results are not good.
The code should give the following results:
but they should be as:
bo1tom
bott2m
bo1t2m
but you gave me the following:
bo1t2m -> correct
bo2tom -> '2' should be placed instead of the 'o'
bo1tom -> correct
thanks again
Amit ChoudharyPosted Mar 9, 2010, 6:26 AM
you did'nt mark my answer as i have give you as much help as i can... and you can customize the code according to your requirement.
Sa ZaPosted Mar 9, 2010, 5:36 AM
Thanks for your help .. it is really appreciated.
But there is still a problem:
The results I am getting now are the following:
bo1t2m
bo2tom
bo1tom
but they should be as:
bo1tom
bott2m
bo1t2m
Amit ChoudharyPosted Mar 9, 2010, 5:01 AM
Amit ChoudharyPosted Mar 9, 2010, 4:30 AM
yes you are right i found the bug. it was because of string.Replace funtion inside the insertSymbol function. well i have fixed it and now its working great.
Now please mark my answer.
Sa ZaPosted Mar 9, 2010, 3:58 AM
Thanks for your code.... it looks really cool, but I think that somewhere there is a small bug :(
Consider the following inputs:
string s1 = "bottom";
string s2 = "**1*2*";
The answer I am getting for your code is the following:
b2112m
bo22om
bo11om
but it should be as follows:
bo1tom
bott2m
bo1t2m
what do you think?
Amit ChoudharyPosted Mar 9, 2010, 3:01 AM
Your problem get solved. i have wrote the code for you download the attached program.zip.
Please don't forget to mark as answer i have spent lot time on solving this.
Raaj KumarPosted Mar 9, 2010, 12:29 AM
Try this,
public static string[] ArrayOfString(string string1, string string2)
{
return new string[] { string1, string2 };
}
Regards,
raaj