title: Count Number of Functions in c programme using C#
body: <p>How can i count number of functions in C program File using C# program? I have  create a simple C# class to count the LOC in C file.</p>
  
  <pre><code>private bool IsInMultipleComment = false; 
  
  private int getNumberOFFuncions(FileInfo fs)
  {
  
      StreamReader rdr;
      int count = 0;
      string tempStr;
  
      // initialize
      rdr = fs.OpenText();
      tempStr = rdr.ReadLine();
  
      while (true)
      {
          if (tempStr == null)
              break;
          if (IsFunction(tempStr))
              count++;
          tempStr = rdr.ReadLine();
      }
      return count;
  
  }
  </code></pre>
  
  <p>Supportive method:</p>
  
  <pre><code>private bool IsFunction(string line)
      {
          if (line.Contains("//"))
              return false;
          if (line.Contains("/*"))
              IsInMultipleComment = true;
          if (line.Contains("*/"))
              IsInMultipleComment = false;
  
          if (!IsInMultipleComment)
          {
              if (line.Contains("void") || line.Contains("int") || line.Contains("short") || line.Contains("long") || line.Contains("float") || line.Contains("char") || line.Contains("double"))
              {
                  if (!line.Contains(";"))
                  {
                      return true;
                  }
              }
          }
          return false;
      }
  </code></pre>
  
  <p>This is how I count variables:</p>
  
  <pre><code>private int getNumberOfVariables(FileInfo fs)
      {
          StreamReader rdr;
          int count = 0;
          string tempStr;
  
          // initialize
          rdr = fs.OpenText();
          tempStr = rdr.ReadLine();
  
          while (true)
          {
              if (tempStr == null)
                  break;
              count += getVariblesOfLine(tempStr);
              tempStr = rdr.ReadLine();
          }
          return count;
      }
  </code></pre>
  
  <p>Supportive method:</p>
  
  <pre><code>private int getVariblesOfLine(string line)
      {
          line = line.Trim();  // trim the lines
  
          if (line.Contains("#"))     // remove preprocessive declarations
              return 0;
          if (line.Contains("//"))
              return 0;
          if (line.Contains("/*"))
              IsInMultipleComment = true;
          if (line.Contains("*/"))
              IsInMultipleComment = false;
          if (!IsInMultipleComment)
          {
              if (line.Contains("unsigned") || line.Contains("signed") || line.Contains("int") || line.Contains("short") || line.Contains("long") || line.Contains("float") || line.Contains("char") || line.Contains("double"))
              {
                  if (!line.Contains("("))    // remove if this is function
                  {
                      Console.WriteLine(line);
                      if (line.Contains(","))     // count at multiple declarations
                      {
                          int y = line.Count(f =&gt; f == ',');
                          return y + 1;
                      }
                      return 1;
                  }
              }
          }
          return 0;
      }
  </code></pre>