c# - Need help on deciding right way to store the each character of a text -


so here deal. suppose have ".txt" file contains kind of text(any). need program read each character in text including symbols, numbers, white space , etc. after need count amount of occurrence of each character read.

now reading text char char easy:

 string text = file.readalltext("text.txt");  foreach (char c in text) console.writeline(c); 

what best way save each character .txt file , work them after?

since need characters number of occurrence of appearing characters. consider of using dictionary<char,int> you:

dictionary<char,int> dict = new dictionary<char,int>(); (char c in text){     if (dict.containskey(c)) //exist add count of existing item         dict[c] = dict[c] + 1;     else //does not exist, create new item         dict.add(c,1); } 

by using dictionary, things these, seem fit needs best:

dict.keys; //to characters (keys) store in dictionary before dict.containskey('a'); //to check if dictionary has 'a' 1 of keys dict['a']; //to value dictionary item character 'a' key dict.add('a',1); //add new key 'a' dictionary value of 1 

Comments