0

I'm working on a program in c# where I'm supposed to tell the user to insert random characters and then divide those random characters into letters and numbers and count how many i have of each.

anyone has any idea how to do so? thanks! ps: i'm new to c# and programming alltogether :)

3

3 Answers 3

1

You could use char.IsDigit or char.IsNumber to check if it's a "number"(digit):

string input = Console.ReadLine(); int digitCount = input.Count(char.IsDigit); int letterCount = input.Length - digitCount; 

You need to add using System.Linq to be able to use Enumerable.Count.


Since you now have asked for counting vowels. Vowels are a, e, i, o, u and their uppercase version. So you could use this method:

private static readonly HashSet<char> Vowels = new HashSet<char> { 'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U' }; public static bool IsVowel(char c) => Vowels.Contains(c); 

and this code to count the vowels:

int vowelCount = input.Count(IsVowel); 

If you don't just want to count them but show them to the user:

string vowelsInInput = new String(input.Where(IsVowel).ToArray()); string noVowels = new String(input.Where(c => !IsVowel(c)).ToArray()); 

which also gives you the count(for example vowelsInInput.Length).

Sign up to request clarification or add additional context in comments.

6 Comments

The thing is, the user is supposed to enter a line of characters like "djhbviuewh874h3fb" and i'm supposed to split it and organize it then
yes that's what I meant, but i use visual studio, and there is already a textbox... do i still need to write Console.Readline() if not... what should i write?
what if i wanna sort the letter in two categories... vocals and normal ones for example
yes, vowels, well... I'm sorry I guess, but what should I do now?
I marked it as answered, and tried to post another qs, but I can only post once every 90 min, and I really need this now, so it'd be really nice of you if you could help me!
|
0

Get the input and loop through every character within the string

string testStr = "abc123"; foreach (char c in testStr) { Console.WriteLine(c.ToString( )); } 

2 Comments

I think you'll need to explain more :))
a string consists of characters so in the above example we are taking a string and then loop through each and every character. you decide what you want to do with it.
0

You can use a dictionary to count the number of times a letter appears.

 char letter = //your letter; var charFrequencies = new Dictionary<char, int>(); if (!charFrequencies.ContainsKey(letter)) { charFrequencies.Add(letter, 1); } else { charFrequencies[letter]++; } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.