hamzajaved

Frequency in string

Dec 11th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.90 KB | None | 0 0
  1.  
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace filing
  9. {
  10.     public struct alphabet
  11.     {
  12.         public char value;
  13.         public int frequency;
  14.  
  15.     }
  16.     class Program
  17.     {
  18.         static void Main(string[] args)
  19.         {
  20.            
  21.             string s = Console.ReadLine();
  22.             alphabet[] word = new alphabet[s.Length];
  23.             System.IO.FileStream fs = new System.IO.FileStream("C:\\Users\\ABD\\Desktop\\input.txt", System.IO.FileMode.OpenOrCreate);
  24.             System.IO.BinaryWriter bw = new System.IO.BinaryWriter(fs);
  25.            
  26.             bw.Write(s);
  27.             bw.Close();
  28.  
  29.             System.IO.FileStream fs1 = new System.IO.FileStream("C:\\Users\\ABD\\Desktop\\input.txt", System.IO.FileMode.Open);
  30.             System.IO.BinaryReader br = new System.IO.BinaryReader(fs1);
  31.             br.ReadChar();
  32.             string newS = "";
  33.             int j = 0;
  34.             for (int i = 0; i < s.Length; i++)
  35.             {
  36.                 char temp = br.ReadChar();
  37.                 if (contain(newS, temp) > -1)
  38.                 { word[contain(newS, temp)].frequency++;
  39.                    
  40.                 }
  41.                 else
  42.                 {
  43.                     word[j].value = temp;
  44.                     word[j].frequency = 1;
  45.                     newS += temp;
  46.                     j++;
  47.                 }
  48.             }
  49.  
  50.             for(int i = 0; i<j; i++)
  51.             {
  52.                 Console.WriteLine("Value = {0}\tFrequency = {1}", word[i].value, word[i].frequency);
  53.  
  54.             }
  55.         }
  56.         public static int contain(string s, char c)
  57.         {
  58.             int pos = -1;
  59.             for(int i = 0; i < s.Length; i++)
  60.             {
  61.                 if (s[i] == c)
  62.                     pos = i;
  63.  
  64.             }
  65.             return pos;
  66.  
  67.  
  68.  
  69.         }
  70.     }
  71. }
Add Comment
Please, Sign In to add comment