Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

C# Hi. I am a beginer coder and I need some help. Can someone please explain to me exactly how this code works, line fore line explanation please. I

mjeraj

Coder
It's a code for figuring out how many times a letter is used in a sentence, written by the user( for example: Hi, I am Mark- H-1, i-2, ... so on )
I admit i found the code online and I thought I understood it, but it turns out i really don't, so any help will be appriecieted.


C#:
using System;
namespace LogicalProgram
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Vpišite poved : ");
            string message = Console.ReadLine();


            message = message.Replace(" ", string.Empty);

            while (message.Length > 0)
            {
                Console.Write(message[0] + " : ");
                int count = 0;
                for (int j = 0; j < message.Length; j++)
                {
                    if (message[0] == message[j])
                    {
                        count++;
                    }
                }
                Console.WriteLine(count);
                message = message.Replace(message[0].ToString(), string.Empty);
            }

            Console.ReadKey();
        }
    }
}
 
Solution
First 'Vpišite poved :' is printed to the console in line 8. Then, the input from the user is received as a string on line 9. On line 12, all spaces in the user's input are removed. On line 14, a while loop is initiated, if the user's input wasn't empty. On line 16, the first character of the user's input is printed to the console, followed with a " : ". On line 17, an integer called 'count' is initialized, and it's value is set to 0. On line 18, a for loop is initialized. It will run while j is less then the length of the user's input ('j++' means every iteration 'j' is incremented by 1). On line 20 - 22, if the first character of the user's input is equal to the 'j'th character of the user's inputed, count is incremented by 1. On line...
It's a code for figuring out how many times a letter is used in a sentence, written by the user( for example: Hi, I am Mark- H-1, i-2, ... so on )
I admit i found the code online and I thought I understood it, but it turns out i really don't, so any help will be appriecieted.


C#:
using System;
namespace LogicalProgram
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Vpišite poved : ");
            string message = Console.ReadLine();


            message = message.Replace(" ", string.Empty);

            while (message.Length > 0)
            {
                Console.Write(message[0] + " : ");
                int count = 0;
                for (int j = 0; j < message.Length; j++)
                {
                    if (message[0] == message[j])
                    {
                        count++;
                    }
                }
                Console.WriteLine(count);
                message = message.Replace(message[0].ToString(), string.Empty);
            }

            Console.ReadKey();
        }
    }
}
Hi @mjeraj,
unfortunately I don't know C#, but maybe @Antero360 will be able to help you. :)
 
First 'Vpišite poved :' is printed to the console in line 8. Then, the input from the user is received as a string on line 9. On line 12, all spaces in the user's input are removed. On line 14, a while loop is initiated, if the user's input wasn't empty. On line 16, the first character of the user's input is printed to the console, followed with a " : ". On line 17, an integer called 'count' is initialized, and it's value is set to 0. On line 18, a for loop is initialized. It will run while j is less then the length of the user's input ('j++' means every iteration 'j' is incremented by 1). On line 20 - 22, if the first character of the user's input is equal to the 'j'th character of the user's inputed, count is incremented by 1. On line 25, the amount of matching letters are outputed to the console. On line 26, every character in message that is the same as the first character in message is removed. The while loop either repeats, or if 'message.Length' is less than 1 then the next key the user presses is read (on line 29), and the program finishes. Lines 1 to 6 are just setting up the environment. This should be correct, however I haven't done C# for 2 years so I may be wrong.
 
Solution
First 'Vpišite poved :' is printed to the console in line 8. Then, the input from the user is received as a string on line 9. On line 12, all spaces in the user's input are removed. On line 14, a while loop is initiated, if the user's input wasn't empty. On line 16, the first character of the user's input is printed to the console, followed with a " : ". On line 17, an integer called 'count' is initialized, and it's value is set to 0. On line 18, a for loop is initialized. It will run while j is less then the length of the user's input ('j++' means every iteration 'j' is incremented by 1). On line 20 - 22, if the first character of the user's input is equal to the 'j'th character of the user's inputed, count is incremented by 1. On line 25, the amount of matching letters are outputed to the console. On line 26, every character in message that is the same as the first character in message is removed. The while loop either repeats, or if 'message.Length' is less than 1 then the next key the user presses is read (on line 29), and the program finishes. Lines 1 to 6 are just setting up the environment. This should be correct, however I haven't done C# for 2 years so I may be wrong.
Thank you for this great explanation, it helped me a lot.

I still have a question tho; I don't seem to understand the line "" to the "j"th character of the user's inputed "" can you maybe tell me in example what a j'th character of the users input would be?

thank you again for your help though :)
 
So basically 'j' is a number, so the if j was equal to 3, the 'j'th character of the user's input would be the 3rd character of the user's input. Sorry for the confusion, I just didn't know how else to put it.
 
So basically 'j' is a number, so the if j was equal to 3, the 'j'th character of the user's input would be the 3rd character of the user's input. Sorry for the confusion, I just didn't know how else to put it.
Oh okay now I get it, thank you. If you don't mind I'd like to pose another question: In line 16 when the first character from the user input is written, is it always the same? For example, if my text is "Hello", then is the first character always going to be "H" or does it move on to "e"?
 
Oh okay now I get it, thank you. If you don't mind I'd like to pose another question: In line 16 when the first character from the user input is written, is it always the same? For example, if my text is "Hello", then is the first character always going to be "H" or does it move on to "e"?
Not exactly. Every iteration of the while loop, on line 26, every character that is the same as the first character is removed (including the first character).
 
Hi @mjeraj,
unfortunately I don't know C#, but maybe @Antero360 will be able to help you. :)
It's a code for figuring out how many times a letter is used in a sentence, written by the user( for example: Hi, I am Mark- H-1, i-2, ... so on )
I admit i found the code online and I thought I understood it, but it turns out i really don't, so any help will be appriecieted.


C#:
using System;
namespace LogicalProgram
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Vpišite poved : ");
            string message = Console.ReadLine();


            message = message.Replace(" ", string.Empty);

            while (message.Length > 0)
            {
                Console.Write(message[0] + " : ");
                int count = 0;
                for (int j = 0; j < message.Length; j++)
                {
                    if (message[0] == message[j])
                    {
                        count++;
                    }
                }
                Console.WriteLine(count);
                message = message.Replace(message[0].ToString(), string.Empty);
            }

            Console.ReadKey();
        }
    }
}
Hey :).
If you are trying to count the instances of each letter, what you could do is store each letter in a dictionary, so something like
Dictionary<char, int> letterCount;.. where each letter would have an initial count of 0.

and as you iterate through the string, update each letter's count, and at the end, just print out the dictionary.. save yourself the heartache :)
 
It's a code for figuring out how many times a letter is used in a sentence, written by the user( for example: Hi, I am Mark- H-1, i-2, ... so on )
I admit i found the code online and I thought I understood it, but it turns out i really don't, so any help will be appriecieted.


C#:
using System;
namespace LogicalProgram
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Vpišite poved : ");
            string message = Console.ReadLine();


            message = message.Replace(" ", string.Empty);

            while (message.Length > 0)
            {
                Console.Write(message[0] + " : ");
                int count = 0;
                for (int j = 0; j < message.Length; j++)
                {
                    if (message[0] == message[j])
                    {
                        count++;
                    }
                }
                Console.WriteLine(count);
                message = message.Replace(message[0].ToString(), string.Empty);
            }

            Console.ReadKey();
        }
    }
}
also as far as the code goes...

you're getting user input from the console, removing the spaces between words in message. You are then iterating through the message, checking to see if the current character at position j is equal to the character in the 0th index position, if so, add a count. Afterwards, you are printing out the count and removing the current character at position 0. Once that is done, you are waiting for the user to press a key in order to exit the program.

this really is inefficient for what you want to do
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom