title: how would i find the time and space complexity of this code?
body: <p><br>
  I am having difficulty finding space and time complexity for this code that i wrote to find number of palindromes in a string.  </p>
  
  <pre><code>/**
   This program finds palindromes in a string.
  */
  
  #include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include &lt;stdlib.h&gt;
  
  int checkPalin(char *str, int len)
  {
      int result = 0, loop;
  
      for ( loop = 0; loop &lt; len/2; loop++)
      {
  
          if ( *(str+loop) == *(str+((len - 1) - loop)) )
              result = 1;
          else {
              result = 0;
              break;
          }
      }
  
      return result;
  }
  
  int main()
  {
      char *string = "baaab4";
      char *a, *palin;
  
      int len = strlen(string), index = 0, fwd=0, count=0, LEN;
      LEN = len;
  
      while(fwd &lt; (LEN-1))
      {
          a = string+fwd;
          palin = (char*)malloc((len+1)*sizeof(char));    
  
          while(index&lt;len)
          {
              sprintf(palin+index, "%c",*a);
              index++;
              a++;
  
              if ( index &gt; 1 ) {
                  *(palin+index) = '\0';
                  count+=checkPalin(palin, index);
              }
          }
  
          free(palin);
          index = 0;
          fwd++;
          len--;
      }
  
      printf("Palindromes: %d\n", count);
      return 0;
  }
  </code></pre>
  
  <p>I gave it a shot and this what i think:<br>
  in main we have two while loops. The outer one runs over the entire length-1 of the string. Now here is the confusion, the inner while loop runs over the entire length first, then n-1, then n-2 etc for each iteration of the outer while loop. so does that mean our time complexity will be <code>O(n(n-1)) = O(n^2-n) = O(n^2)</code>?
  And for the space complexity initially i assign space for string length+1, then (length+1)-1, (length+1)-2 etc. so how can we find space complexity from this?
  For the checkPalin function its <code>O(n/2)</code>.<br>
  i am preparing for interviews and would like to understand this concept.<br>
  Thank you</p>