-3
static string ReadPassword(int length, char c)

The method should allow you to enter a string as a password. length is the minimum length of the password and c is the character displayed on the screen for each digit of the password.

Enter a password with at least 8 characters for use in the Main:

string pwd = ReadPassword(8, '●')

The user types the letters of his / her password. On the screen, however, do not appear letters , but the character that is stored in c, For example the bullet '●'.

How do i build the program in such way that it writes * for each character typed in the console eaven though my method ends at return s;?

  • 4
    Ok.. but.. you forgot to ask a question! – B001ᛦ Oct 13 '17 at 15:14
  • https://softwareengineering.meta.stackexchange.com/questions/6166/open-letter-to-students-with-homework-problems – Paul Abbott Oct 13 '17 at 15:15
  • sorry, this is my assigment, and I dont really know where to begin. – Jack of Arad Oct 13 '17 at 15:16
  • 2
    _I dont really know where to begin_ By learning the basics or/and starting a research? – B001ᛦ Oct 13 '17 at 15:18
  • 2
    `"I dont really know where to begin"` by asking your instructor for clarification? – maccettura Oct 13 '17 at 15:18
  • 2
    You definitely shouldn't begin by outsourcing your homework to Stack Overflow. – xxbbcc Oct 13 '17 at 15:20
  • What do you want to do? – Pronoy999 Oct 13 '17 at 15:24
  • @PronoyMukherjee _What do you want to do?..._ Huh? – B001ᛦ Oct 13 '17 at 15:24
  • @maccettura we had 13 different string method assignments, 12 of witch were mandatory, this is an extra assigment made especially harder then the other ones. The professor dose not suply help to this assigment. Im not asking for the code just a place to begin. – Jack of Arad Oct 13 '17 at 15:25
  • 1
    Possible duplicate of [Password masking console application](https://stackoverflow.com/questions/3404421/password-masking-console-application) – NineBerry Oct 13 '17 at 15:27
  • yourTextBox.PasswordChar = '\u25CF'; this will give your desired bullet – AGrammerPro Oct 13 '17 at 15:27
  • @NineBerry I know how to do it like that, but everything must be programmed in a method that dose not write in the main. It must be declared in main like this "console.write(ReadPassword(8,'*');)" – Jack of Arad Oct 13 '17 at 15:34
  • @xxbbcc it's a program for witch we do not get extra credit, it's for those who are done with the first 12 assigments. I do have a broad idea how the program should look like, but I really have no idea how to buid it to write '*' instead of the pressed character without writing anything in main – Jack of Arad Oct 13 '17 at 15:42

2 Answers2

2

Here you go.

class Program {
        static void Main(string[] args) {
            char ch;int len;
            Console.WriteLine("Enter the Length of the Password: ");
            len = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Enter the Character for Password:");
            ch = Convert.ToChar(Console.Read());
            printPassword(len, ch);
            Console.Read();
        }
        public static void printPassword(int len,char ch) {
            char ch1;String pass="";
            int i;
            for (i = 0; i < len; i++) {
                pass += Console.ReadKey(true);
                Console.Write(ch);
            }
        }
    }
Pronoy999
  • 645
  • 6
  • 25
  • Thanks, could I ask how you came to this answer, since its more important or me as a student to be able to figure this out myself. Would you recomand any books on c#? – Jack of Arad Oct 13 '17 at 16:00
  • `Clean Code` from Martin and `Adaptive Code` from Gary McLean Hall are very nice books for beginners. – cSteusloff Oct 13 '17 at 16:06
  • @cSteusloff thank you so mutch man, thats really nice of you. – Jack of Arad Oct 13 '17 at 16:10
  • https://msdn.microsoft.com/en-us/library/x3h8xffw(v=vs.110).aspx Check this link. From here I came to the Answer. I used it for my own console project once. – Pronoy999 Oct 13 '17 at 16:59
  • @PronoyMukherjee thanks man, it was explained nicely. For some reason I got a lot of shit for asking for help with this, im new so im not sure if i did smth wrong. It's nice to get some helpful responses. – Jack of Arad Oct 13 '17 at 21:59
1

You can set the cursor position to input position or use ReadKey(true) Try this:

class Program
{
    static void Main(string[] args)
    {
        Console.Write("Set password: ");
        var password = ReadPassword(8, 'o');
        Console.WriteLine();
        Console.WriteLine($"Your password is: {password}");
        Console.ReadKey();
    }

    static string ReadPassword(int length, char c)
    {
        var left = Console.CursorLeft;
        var top = Console.CursorTop;
        var password = new StringBuilder();
        for (int i = 0; i < length; i++)
        {
            password.Append(Console.ReadKey().KeyChar);
            Console.SetCursorPosition(left + i, top);
            Console.Write(c);
        }
        return password.ToString();
    }
}

You can find the documentation here: https://msdn.microsoft.com/en-us/library/system.console.readkey(v=vs.110).aspx

cSteusloff
  • 2,487
  • 7
  • 30
  • 51
  • This works thanks, i've been trying for 2 hours and didn't manage to find the version of what I needed. The help is appreciated! – Jack of Arad Oct 13 '17 at 15:55