Click here to Skip to main content
15,913,341 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
for example if i enter the text in textbox as 'Code Project' (not only code project what ever the text is)

i want the output as like
c-2
o-2
d-1
e-2
p-1
r-1
j-1
t-1

how to achieve this in c#

What I have tried:

i dont know how to do it please help with the code thanks
Posted
Updated 9-Dec-18 3:26am

You'll get no code - this is your homework so it's up to you to do it!

But if you think about it, it's pretty simple.
Start by converting the input to all lowercase (that's simple, there's a string method for that called ToLower) Now create two arrays, both the same size as the string he entered, and a integer that says how much is used. One array holds characters, the other holds integers. (If you know about classes, this can be a List of a class containing an integer and a character, or a Dictionary<char, int> - but I'll assume you don't)
Now look at each character in the input in turn.
If it's in your character array, increment the value in the corresponding integer array.
If it isn't, add it to your characters array, set the corresponding integer array element to 1, and increment the "how much is used" variable.

After the loop, you have the characters and corresponding counts in yoru arrays and it's easy to print them.
 
Share this answer
 
v2
Quote:
i dont know how to do it please help with the code thanks

Just giving you the code will not help you to learn something.
HomeWork problems are simplified versions of the kind of problems you will have to solve in real life, their purpose is learning and practicing.

Quote:
i want the output as like
c-2
o-2
d-1
e-2
p-1
r-1
j-1
t-1

Giving result in alphabetic order may be easier to program.
You know how to solve the problem by hand, think about it in a mechanical fashion, this is basically your algorithm.

We do not do your HomeWork.
HomeWork is not set to test your skills at begging other people to do your work, it is set to make you think and to help your teacher to check your understanding of the courses you have taken and also the problems you have at applying them.
Any failure of you will help your teacher spot your weaknesses and set remedial actions.
Any failure of you will help you to learn what works and what don't, it is called 'trial and error' learning.
So, give it a try, reread your lessons and start working. If you are stuck on a specific problem, show your code and explain this exact problem, we might help.

As programmer, your job is to create algorithms that solve specific problems and you can't rely on someone else to eternally do it for you, so there is a time where you will have to learn how to. And the sooner, the better.
When you just ask for the solution, it is like trying to learn to drive a car by having someone else training.
Creating an algorithm is basically finding the maths and make necessary adaptation to fit your actual problem.
 
Share this answer
 
thank you so much for your encouragement finally I got it using of while loop

string inputtext = textBox1.Text;
string result = string.Empty;
while (inputtext .Length > 0)
{
int count = 0;
for (int i = 0; i < inputtext.Length; i++)
{
if (inputtext[0] == inputtext[i])
{
count++;
}
}
result += inputtext[0] + " = " + count + "\n";
inputtext = inputtext.Replace(inputtext[0].ToString(), string.Empty);
}
label1.Text = result;
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900