Frequency of Characters in visual c# windows form application - c#

I am developing a program in visual c# windows form application.
I need to find out the number of characters in a text box and display each characters' frequency in a list box. I have the following code:
private void btnCheckFrequency_Click(object sender, EventArgs e)
{
lstFreqMeter.Items.Clear();
string str;
int c = 1;
int strlen;
str = txtString.Text;
strlen = txtString.TextLength;
int[] counter = new int[strlen];
for (int i = 0; i < strlen; i++)
{
for (int j = i + 1; j < strlen; j++)
{
if (str[i] == str[j])
{
c += 1;
}
}
counter[i] = c;
c = 1;
}
for (int k = 0; k < counter.Length; k++)
{
lstFreqMeter.Items.Add(counter[k]);
}
}
In this code when I click the "Check Frequency" button the program gives, as output, the frequency of each character and repeated characters, and also spaces which we do not want.

3 loops to accomplish this is where you are going wrong. I think you are trying to make it too complicated.
Its frowned upon to give answers to homework questions, but some hints as to direction should help.
Declare a Dictionary<char, int>
Make only one loop. For each char in your string.
Inside the loop, populate a Dictionary<char, int> with your results.
If the char exists in the dictionary, set the int to int++, if not, add the char to the dictionary with int of 1.
Outside the loop, AddRange to your lstFreqMeter.Items.

You can get the number of chars in a string by using Count on the string.
Getting the frequency of each char you can iterate through the string and increment an integer. A dictionary (key/value list) would be a great datatype to hold this data.
const string textString = "aaabbbcccaaattteeevvvooo";
var numberOfChars = textString.Count();
var dictionary = new Dictionary<char,int>();
foreach (var letter in textString)
{
if (dictionary.ContainsKey(letter))
dictionary[letter]++;
else
dictionary[letter] = 1;
}
The Dictionary will contain a key (the char) and a value (the count).

Try something like this:
var letters = Enumerable.Range('A', 26).Select(i => (char)i);
var sourceChars = letters.Concat(myString.ToUpperInvariant());
var results = from c in sourceChars
group c by c into g
where char.IsLetter(g.Key)
orderby g.Key
select new { Char = g.Key, Count = g.Count() - 1 };
foreach (var result in results)
{
Console.WriteLine("There are {0} {1}'s.", result.Count, result.Char);
}
This will return you a class of how many times each letter appears in myString

Maintain a dictionary of the matched characters and increment each character count as it is matched.
Dictionary<char, int> count = new Dictionary<char,int>();
string str = textString.Text;
int len = str.Length;
for(int i = 0; i < len; i ++)
{
if (str[i] == ' ') continue;
if (count.ContainsKey(str[i]))
{
count[str[i]] += 1;
} else {
count.Add(str[i], 1);
}
}
foreach(char key in count.Keys)
{
Console.WriteLine("{0} : {1}", key, count[key]);
}

Related

Generating all variations of certain length out of string

I've seen few implementations of variations of string in C#, but none of them had any limitation on their length. Unfortunately, I cannot modify them to achieve my goal which is e.g.
for:
string = "ABCD" and variationLength = 2
generate new strings:
AB, AC, AD, BA, BC, BD, CA, CB, CD, DA, DB, DC
I'm looking for exactly this Python's itertools.permutations implementation but in C#. (https://docs.python.org/3/library/itertools.html#itertools.permutations)
Is there anything similar to its in C#? If not, then what is the easiest way to implement it?
Edit_2:
so far I came up with an idea to list all unique chars of given string and then get variations out of them
static void PrintAllKLengthPerm(string str, int k)
{
int n = str.Length;
PrintAllKLengthPermRec(str, "", n, k);
}
// The main recursive method to print all possible strings of length k
static void PrintAllKLengthPermRec(string str, String prefix, int n, int k)
{
// Base case: k is 0, print prefix
if (k == 0)
{
Console.WriteLine(prefix);
return;
}
// One by one add all characters from str and recursively
// call for k equals to k-1
for (int i = 0; i < n; ++i)
{
// Next character of input added
String newPrefix = prefix + str[i];
// k is decreased, because we have added a new character
PrintAllKLengthPermRec(str, newPrefix, n, k - 1);
}
}
static void Main(string[] args)
{
string str = "ABCD";
int permLen = 2;
//get all unique characters in string
string uniqStr = new String(str.Distinct().ToArray());
// Print all possible strings of length permLen out of uniqStr characters
PrintAllKLengthPerm(uniqStr, permLen);
}
However I am looking for more optimal and effective solution
Here's a truly recursive permutation method:
public IEnumerable<string> Permutate(string source, int count)
{
if (source.Length == 1)
{
yield return source;
}
else if (count == 1)
{
for (var n = 0; n < source.Length; n++)
{
yield return source.Substring(n, 1);
}
}
else
{
for (var n = 0; n < source.Length; n++)
foreach (var suffix in Permutate(
source.Substring(0, n)
+ source.Substring(n + 1, source.Length - n - 1), count -1))
{
yield return source.Substring(n, 1) + suffix;
}
}
}
It can be called with Permutate("ABCD", 2) and returns this:
I made the following recursive function which accomplishes your task:
static void Permutations(List<string> output, string str, int n, string curr)
{
if(curr.Length == n)
{
output.Add(curr);
return;
}
foreach(char c in str)
if(!curr.Contains(c.ToString()))
Permutations(output, str, n, curr + c.ToString());
}
and then you call it like this:
string str = "ABCD";
int length = 2;
List<string> perms = new List<string>();
Permutations(perms, str, length, "");
// now the list "perms" will contain the permutations of "str" in length "n"
List<string> newPermutations = new List<string>();
for(int a = 0; a!=inString.Count; a++)
for((int b = 0; b!=inString.Count; b++)
if(noRepetitions && a == b) continue;
newPermutations.Add(""+inString[a] + inString[b]);
I think that that should work; I am still trying to figure out a way to not only have 2 letters.
Edit: Edited it to work, the old one just didn't work... lol
Edit: Thanks to #Bloopy, they helped me spot some errors in my for loops
Here's a solution using modulo and division. There are 4² possible strings of length 2 using the letters ABCD. Number them from 0 to 4²-1 and repeatedly divide each number by 4. Use the resulting remainders as array indexes on the ABCD string.
This has the advantage of allowing you to keep the strings with repeated elements (AA, BB, CC, DD) when needed – just skip the discard step.
string alphabet = "ABCD";
int length = 2;
int[] indexes = new int[length];
char[] item = new char[length];
// loop through all possible strings for the given alphabet and length
for (int i = 0; i < Math.Pow(alphabet.Length, length); i++) {
int dividend = i;
for (int j = length - 1; j >= 0; j--) {
indexes[j] = dividend % alphabet.Length;
dividend /= alphabet.Length;
}
// discard any that use the same alphabet element more than once
if (indexes.Distinct().Count() < length)
continue;
for (int k = 0; k < length; k++) {
item[k] = alphabet[indexes[k]];
}
Console.WriteLine(item);
}
Alternatively, here's a really brief solution using LINQ. Note that this one doesn't work correctly if there are duplicate elements in the string (unless you want to remove the call to Where and keep the AA, BB etc.). I'd need to track the indexes like I did in my method above.
IEnumerable<string> prm = alphabet.Select(c => c.ToString());
for (int a = 1; a < length; a++)
prm = prm.SelectMany(s => alphabet.Where(t => !s.Contains(t)), (x, y) => x + y);
foreach (string s in prm)
Console.WriteLine(s);

How to find all possible strings of a certain length using only the chars out of an array [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Listing all permutations of a string/integer
For example,
aaa .. aaz .. aba .. abz .. aca .. acz .. azz .. baa .. baz .. bba .. bbz .. zzz
Basically, imagine counting binary but instead of going from 0 to 1, it goes from a to z.
I have been trying to get this working to no avail and the formula is getting quite complex. I'm not sure if there's a simpler way to do it.
Edit
I have something like this at the moment but it's not quite there and I'm not sure if there is a better way:
private IEnumerable<string> GetWordsOfLength(int length)
{
char letterA = 'a', letterZ = 'z';
StringBuilder currentLetters = new StringBuilder(new string(letterA, length));
StringBuilder endingLetters = new StringBuilder(new string(letterZ, length));
int currentIndex = length - 1;
while (currentLetters.ToString() != endingLetters.ToString())
{
yield return currentLetters.ToString();
for (int i = length - 1; i > 0; i--)
{
if (currentLetters[i] == letterZ)
{
for (int j = i; j < length; j++)
{
currentLetters[j] = letterA;
}
if (currentLetters[i - 1] != letterZ)
{
currentLetters[i - 1]++;
}
}
else
{
currentLetters[i]++;
break;
}
}
}
}
For a variable amount of letter combinations, you can do the following:
var alphabet = "abcdefghijklmnopqrstuvwxyz";
var q = alphabet.Select(x => x.ToString());
int size = 4;
for (int i = 0; i < size - 1; i++)
q = q.SelectMany(x => alphabet, (x, y) => x + y);
foreach (var item in q)
Console.WriteLine(item);
var alphabet = "abcdefghijklmnopqrstuvwxyz";
//or var alphabet = Enumerable.Range('a', 'z' - 'a' + 1).Select(i => (char)i);
var query = from a in alphabet
from b in alphabet
from c in alphabet
select "" + a + b + c;
foreach (var item in query)
{
Console.WriteLine(item);
}
__EDIT__
For a general solution, you can use the CartesianProduct here
int N = 4;
var result = Enumerable.Range(0, N).Select(_ => alphabet).CartesianProduct();
foreach (var item in result)
{
Console.WriteLine(String.Join("",item));
}
// Eric Lippert’s Blog
// Computing a Cartesian Product with LINQ
// http://blogs.msdn.com/b/ericlippert/archive/2010/06/28/computing-a-cartesian-product-with-linq.aspx
public static IEnumerable<IEnumerable<T>> CartesianProduct<T>(this IEnumerable<IEnumerable<T>> sequences)
{
// base case:
IEnumerable<IEnumerable<T>> result = new[] { Enumerable.Empty<T>() };
foreach (var sequence in sequences)
{
var s = sequence; // don't close over the loop variable
// recursive case: use SelectMany to build the new product out of the old one
result =
from seq in result
from item in s
select seq.Concat(new[] { item });
}
return result;
}
You have 26^3 counts for 3 "digits". Just iterate from 'a' to 'z' in three loops.
Here's a very simple solution:
for(char first = 'a'; first <= (int)'z'; first++)
for(char second = 'a'; second <= (int)'z'; second++)
for(char third = 'a'; third <= (int)'z'; third++)
Console.WriteLine(first.ToString() + second + third);

How to count word occurrences in an array of strings using c#?

I'm new to programming, and I am trying to write a program that take in an array of strings (each index of the array being a word) and then count the occurrences of each word in the string. This is what I have so far:
string[] words =
{
"which",
"wristwatches",
"are",
"swiss",
"wristwatches"
};
Array.Sort (words);
for (int i = 0; i < words.Length; i++)
{
int count = 1;
for(int j = 1; j < words.Length; j++)
{
if (words [i] == words [j])
{
count++;
}
}
Console.WriteLine ("{0} {1}", words[i], count);
}
Ideally, I would like the output to be something like:
are 1
swiss 1
which 1
wristwatches 2
The problems with your code are (1) double-counting and (2) skipping the initial element in the nested loop.
You double-count because you ignore situations when i == j; you skip the initial element because you set int j = 1.
The shortest solution is to use LINQ, like this:
var counts = words
.GroupBy(w => w)
.Select(g => new {Word = g.Key, Count = g.Count()})
.ToList();
Now you can print the results like this:
foreach (var p in counts) {
Console.WriteLine("Word '{0}' found {1} times", p.Word, p.Count);
}
There are certainly more efficient ways of handling this (take a look at dasblinkenlight's answer for an extremely good one) but asssuming you'd like to keep relatively the same code, you should change your second for loop to something along these lines:
for(int j = i+1; j < words.Length; j++)
{
if (words [i] == words [j])
{
count++;
}
else break;
}
Here are the two changes I made:
1) You should initialize j to i+1; You want to check if any of the rest of the Strings are equal to words[i], and the rest of the strings will start at i+1, not 1 (unless i=0).
2) For the sake of efficiency, you'll want to break out of the second loop if the two string aren't equal; since you sorted the array alphabetically, if the word you're currently looking at isn't equal, none of the ones after it will be either.
var occrs = words.GroupBy(x => x.ToLower())
.ToDictionary(g => g.Key, g => g.Count());
foreach(var pair in occrs)
Console.WriteLine(pair.Key + " " +pair.Value);
For your understanding purpose use String.Compare()
int Duplicate = words.Lenth + 1; //any value not in the range of the string array
for (int i = 0; i < words.Length; i++)
{
int count = 1;
for(int j = 0; j < words.Length; j++)
{
if(i != j) //to avoid same string comparison
{
if (string.Compare(words [i],words [j]) == 0) //or else .Equals(0)
{
count++;
Duplicate = j;
}
}
}
if(i != Duplicate)
{
Console.WriteLine ("{0} {1}", words[i], count);
}
}
This will not print again the same value.
Make use of dictionary data structure. Here the dictionary will store key as word and value as word count. Insert all the words in dictionary. If the word inserted word is new, set the value of the word key to 1 , otherwise increment the word-key value by 1.
Dictionary<string, int> wordCount = new Dictionary<string, int>();
// Insert a word in the dictionary if it exits, otherwise increment
//the count of the word
for (int i = 0; i < words.Length; i++)
{
try
{
wordCount.Add(words[i], 1);
}
catch (Exception)
{
wordCount[words[i]] += 1;
}
}
// display word and it's corresponding word count
foreach (var item in wordCount)
{
Console.WriteLine ("{0} {1}", item.Key, item.Value);
}

Counting how many times a certain char appears in a string before any other char appears

I have many strings. Each string is prepended with at least 1 $. What is the best way to loop through the chars of each string to count how many $'s there are per string.
eg:
"$hello" - 1
"$$hello" - 2
"$$h$ello" - 2
You could use the Count method
var count = mystring.Count(x => x == '$')
int count = myString.TakeWhile(c => c == '$').Count();
And without LINQ
int count = 0;
while(count < myString.Length && myString[count] == '$') count++;
The simplest approach would be to use LINQ:
var count = text.TakeWhile(c => c == '$').Count();
There are certainly more efficient approaches, but that's probably the simplest.
You could do this, it doesn't require LINQ, but it's not the best way to do it(since you make split the whole string and put it in an array and just pick the length of it, you could better just do a while loop and check every character), but it works.
int count = test.Split('$').Length - 1;
var str ="hello";
str.Where(c => c == 'l').Count() // 2
int count = yourText.Length - yourText.TrimStart('$').Length;
int count = Regex.Matches(myString,"$").Count;
public static int GetHowManyTimeOccurenceCharInString(string text, char c)
{
int count = 0;
foreach(char ch in text)
{
if(ch.Equals(c))
{
count++;
}
}
return count;
}
just a simple answer:
public static int CountChars(string myString, char myChar)
{
int count = 0;
for (int i = 0; i < myString.Length; i++)
{
if (myString[i] == myChar) ++count;
}
return count;
}
Cheers! - Rick
One approach you could take is the following method:
// Counts how many of a certain character occurs in the given string
public static int CharCountInString(char chr, string str)
{
return str.Split(chr).Length-1;
}
As per the parameters this method returns the count of a specific character within a specific string.
This method works by splitting the string into an array by the specified character and then returning the length of that array -1.
//This code worked for me
class CountOfLettersOfString
{
static void Main()
{
Console.WriteLine("Enter string to check count of letters");
string name = Console.ReadLine();
//Method1
char[] testedalphabets = new char[26];
int[] letterCount = new int[26];
int countTestesd = 0;
Console.WriteLine($"Given String is:{name}");
for (int i = 0; i < name.Length - 1; i++)
{
int countChar = 1;
bool isCharTested = false;
for (int j = 0; j < testedalphabets.Length - 1; j++)
{
if (name[i] == testedalphabets[j])
{
isCharTested = true;
break;
}
}
if (!isCharTested)
{
testedalphabets[countTestesd] = name[i];
for (int k = i + 1; k < name.Length - 1; k++)
{
if (name[i] == name[k])
{
countChar++;
}
}
letterCount[countTestesd] = countChar;
countTestesd++;
}
else
{
continue;
}
}
for (int i = 0; i < testedalphabets.Length - 1; i++)
{
if (!char.IsLetter(testedalphabets[i]))
{
continue;
}
Console.WriteLine($"{testedalphabets[i]}-{letterCount[i]}");
}
//Method2
var g = from c in name.ToLower().ToCharArray() // make sure that L and l are the same eg
group c by c into m
select new { Key = m.Key, Count = m.Count() };
foreach (var item in g)
{
Console.WriteLine(string.Format("Character:{0} Appears {1} times", item.Key.ToString(), item.Count));
}
Console.ReadLine();
}
}
This is a similar Solution to find how many email addresses included in a string. This way is more efficient`
int count = 0;
foreach (char c in email.Trim())
if (c == '#') count++;

Generating an array of letters in the alphabet

Is there an easy way to generate an array containing the letters of the alphabet in C#? It's not too hard to do it by hand, but I was wondering if there was a built in way to do this.
I don't think there is a built in way, but I think the easiest would be
char[] alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
C# 3.0 :
char[] az = Enumerable.Range('a', 'z' - 'a' + 1).Select(i => (Char)i).ToArray();
foreach (var c in az)
{
Console.WriteLine(c);
}
yes it does work even if the only overload of Enumerable.Range accepts int parameters ;-)
for (char letter = 'A'; letter <= 'Z'; letter++)
{
Debug.WriteLine(letter);
}
char[] alphabet = Enumerable.Range('A', 26).Select(x => (char)x).ToArray();
I wrote this to get the MS excel column code (A,B,C, ..., Z, AA, AB, ..., ZZ, AAA, AAB, ...) based on a 1-based index. (Of course, switching to zero-based is simply leaving off the column--; at the start.)
public static String getColumnNameFromIndex(int column)
{
column--;
String col = Convert.ToString((char)('A' + (column % 26)));
while (column >= 26)
{
column = (column / 26) -1;
col = Convert.ToString((char)('A' + (column % 26))) + col;
}
return col;
}
Assuming you mean the letters of the English alphabet...
for ( int i = 0; i < 26; i++ )
{
Console.WriteLine( Convert.ToChar( i + 65 ) );
}
Console.WriteLine( "Press any key to continue." );
Console.ReadKey();
You could do something like this, based on the ascii values of the characters:
char[26] alphabet;
for(int i = 0; i <26; i++)
{
alphabet[i] = (char)(i+65); //65 is the offset for capital A in the ascaii table
}
(See the table here.) You are just casting from the int value of the character to the character value - but, that only works for ascii characters not different languages etc.
EDIT:
As suggested by Mehrdad in the comment to a similar solution, it's better to do this:
alphabet[i] = (char)(i+(int)('A'));
This casts the A character to it's int value and then increments based on this, so it's not hardcoded.
Note also, the string has a operator[] which returns a Char, and is an IEnumerable<char>, so for most purposes, you can use a string as a char[]. Hence:
string alpha = "ABCDEFGHIJKLMNOPQRSTUVQXYZ";
for (int i =0; i < 26; ++i)
{
Console.WriteLine(alpha[i]);
}
foreach(char c in alpha)
{
Console.WriteLine(c);
}
Surprised no one has suggested a yield solution:
public static IEnumerable<char> Alphabet()
{
for (char letter = 'A'; letter <= 'Z'; letter++)
{
yield return letter;
}
}
Example:
foreach (var c in Alphabet())
{
Console.Write(c);
}
var alphabets = Enumerable.Range('A', 26).Select((num) => ((char)num).ToString()).ToList();
char alphaStart = Char.Parse("A");
char alphaEnd = Char.Parse("Z");
for(char i = alphaStart; i <= alphaEnd; i++) {
string anchorLetter = i.ToString();
}
//generate a list of alphabet using csharp
//this recurcive function will return you
//a string with position of passed int
//say if pass 0 will return A ,1-B,2-C,.....,26-AA,27-AB,....,701-ZZ,702-AAA,703-AAB,...
static string CharacterIncrement(int colCount)
{
int TempCount = 0;
string returnCharCount = string.Empty;
if (colCount <= 25)
{
TempCount = colCount;
char CharCount = Convert.ToChar((Convert.ToInt32('A') + TempCount));
returnCharCount += CharCount;
return returnCharCount;
}
else
{
var rev = 0;
while (colCount >= 26)
{
colCount = colCount - 26;
rev++;
}
returnCharCount += CharacterIncrement(rev-1);
returnCharCount += CharacterIncrement(colCount);
return returnCharCount;
}
}
//--------this loop call this function---------//
int i = 0;
while (i <>
{
string CharCount = string.Empty;
CharCount = CharacterIncrement(i);
i++;
}
4 ways get English alphabet in Console:
public void ShowEnglishAlphabet()
{
var firstLetter = 'a';
var endLetter = 'z';
for (var letter = firstLetter; letter <= endLetter; letter++)
{
Console.WriteLine($"{letter}-{letter.ToString().ToUpper()}");
}
}
public void ShowEnglishAlphabetFromUnicodeTableDecNumber()
{
var firstLetter = 97;
var endLetter = 122;
for (var letterNumberUnicodeTable = firstLetter;
letterNumberUnicodeTable <= endLetter; letterNumberUnicodeTable++)
{
Console.WriteLine($"{(char)letterNumberUnicodeTable}-
{((char)letterNumberUnicodeTable).ToString().ToUpper()}");
}
}
public void ShowEnglishAlphabetUnicodeTableEscapeSequence()
{
var firstLetter = '\u0061';
var endLetter = '\u007A';
for (var letterNumberUnicodeTable = firstLetter;
letterNumberUnicodeTable <= endLetter; letterNumberUnicodeTable++)
{
Console.WriteLine($"{letterNumberUnicodeTable}-
{letterNumberUnicodeTable.ToString().ToUpper()}");
}
}
public void ShowEnglishAlphabetUnicodeTableLinq()
{
var alphabets = Enumerable.Range('a', 26).Select(letter =>
((char)letter).ToString()).ToList();
foreach (var letter in alphabets)
{
Console.WriteLine($"{letter}-{letter.ToUpper()}");
}
}
Unfortunately there is no ready-to-use way.
You can use;
char[] characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();

Categories