Hi
i have a function that multiplies a string n times.
the function is :
string Multiply(string input, int times)
{
StringBuilder sb = new StringBuilder(input.Length * times);
for (int i = 0; i < times; i++)
{
sb.Append(input);
}
return sb.ToString();
}
i would like to ask if there is any way to leave a blank space in the string
that i will multiply but without count it as the lenght of the word.
thanks .
Loading
gslamPosted Dec 30, 2009, 12:34 PM
public static void Main(string[] args)
{
List
guesses.Add('e');
guesses.Add('l');
guesses.Add('d');
Console.WriteLine("[" + MaskWord("hello", guesses) + "]");
Console.WriteLine("[" + MaskWord("goodbye", guesses) + "]");
Console.Write("Press any key to continue . . . ");
Console.ReadKey(true);
}
private static string MaskWord(string wordtoguess, List
{
StringBuilder sb = new StringBuilder(2 * wordtoguess.Length);
foreach (char letter in wordtoguess)
{
sb.Append(guesses.Contains(letter)? letter : '*').Append(' ');
}
return sb.ToString().Trim();
}
adsfaPosted Dec 30, 2009, 5:11 PM
works like charm
you have been voted
Santhosh NPosted Dec 30, 2009, 12:27 PM
sb.Append(" ");
sb.Append(input);
Please mark the ans as Accepted if satisfied!
adsfaPosted Dec 30, 2009, 11:55 AM
done it but with no spaces left .
if someone have an idea how to insert a space please assist me
thanks!!!!!
gslamPosted Dec 30, 2009, 11:47 AM
Are you trying to replace the "*"'s with correctly guessed letters as in a hangman game?
adsfaPosted Dec 30, 2009, 11:27 AM
well but something must i have miss in this function
when i m calling
public void newword()
{
string charLetter = "*";
string word="";
int strlen = wordtoguess.Length;//wordtoguess is the letter from txt file
string mult = Multiply(charLetter, strlen);
word = mult;
int result = 0;
string newChar = "";
for (int i = 0; i < strlen; i++)
{
newChar = wordtoguess.Substring(strlen - 1);
word = word.Remove(strlen - 1);
word = word.Insert(strlen - 1, newChar);
string newChar2 = wordtoguess.Substring(result, 1);
word = word.Remove(result, 1);
word = word.Insert(result, newChar2);
wordtxt.Text = word;
}
}
cause when i call the new word the letters still read the " " symbol as char
gslamPosted Dec 30, 2009, 10:36 AM
public static void Main(string[] args)
{
Console.WriteLine("[" + Multiply("*","hello".Length) + "]");
Console.Write("Press any key to continue . . . ");
Console.ReadKey(true);
}
private static string Multiply(string input, int times)
{
StringBuilder sb = new StringBuilder((input.Length + 1) * times); // Include the added space in the size calculation
for (int i=0;i
sb.Append(input).Append(" ");
}
return sb.ToString().Trim(); // Trim the trailing space
}
This generates [* * * * *]
Santhosh NPosted Dec 30, 2009, 9:29 AM
string Multiply(string input, int times)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < times; i++)
{
sb.Append(input);
sb.Append(" ");
}
return sb.ToString();
}
adsfaPosted Dec 30, 2009, 8:27 AM
----------------------------
string Multiply(string input, int times)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < times; i++)
{
sb.Append(input).Append(" ");
}
return sb.ToString();
}
-------------------------------------------
nothing happened it reads hello as * * * and not as * * * * * where each * is a letter
Santhosh NPosted Dec 30, 2009, 8:02 AM
string Multiply(string input, int times)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < times; i++)
{
sb.Append(input).Append(" ");
}
return sb.ToString();
}
adsfaPosted Dec 30, 2009, 7:52 AM
the thing i would like to achieve is to count
the word length (i read it from a txt file),
put it in a textbox and hide it with a char symbol(e.g. *)
then the user by pressing the corresponding letter
can guess the word. So if i put " " before sb read it like char (eg * * * if the word is hello cause it counts the lenght which is 5.
I want to put a blank space between these letters (eg * * * * * if the word is hello and count 5 like the string)
Kirtan PatelPosted Dec 30, 2009, 6:27 AM
Friend ,
as you dont want to Count length While inputing String into function then Add Space while Appending it :)
if you find my answer helpful to you then please check "Do you like this answer" check box :)
here is you solution
string Multiply(string input, int times)
{
StringBuilder sb = new StringBuilder(input.Length * times);
for (int i = 0; i < times; i++)
{
sb.Append(" "+input);
}
return sb.ToString();
}
Santhosh NPosted Dec 30, 2009, 6:26 AM
input.Trim();
Also, I did not understand what you are trying to achieve here...
let me know what you wanted exactly..