title: Returning the length of a char array in C
body: <p>I am new to programming in C and am trying to write a simple function that will normalize a char array. At the end i want to return the length of the new char array. I am coming from java so I apologize if I'm making mistakes that seem simple. I have the following code:</p>
  
  <pre><code>/* The normalize procedure normalizes a character array of size len 
     according to the following rules:
       1) turn all upper case letters into lower case ones
       2) turn any white-space character into a space character and, 
          shrink any n&gt;1 consecutive whitespace characters to exactly 1 whitespace
  
       When the procedure returns, the character array buf contains the newly 
       normalized string and the return value is the new length of the normalized string.
  
  */
  int
  normalize(unsigned char *buf,   /* The character array contains the string to be normalized*/
                      int len     /* the size of the original character array */)
  {
      /* use a for loop to cycle through each character and the built in c functions to analyze it */
      int i;
  
  if(isspace(buf[0])){
      buf[0] = "";
  }
  if(isspace(buf[len-1])){
      buf[len-1] = "";
  }
  
      for(i = 0;i &lt; len;i++){
          if(isupper(buf[i])) {
              buf[i]=tolower(buf[i]);
          }
          if(isspace(buf[i])) {
              buf[i]=" ";
          }
          if(isspace(buf[i]) &amp;&amp; isspace(buf[i+1])){
              buf[i]="";
          }
      }
  
      return strlen(*buf);
  
  
  }
  </code></pre>
  
  <p>How can I return the length of the char array at the end? Also does my procedure properly do what I want it to?</p>
  
  <p>EDIT: I have made some corrections to my program based on the comments. Is it correct now?</p>
  
  <pre><code>/* The normalize procedure normalizes a character array of size len 
     according to the following rules:
       1) turn all upper case letters into lower case ones
       2) turn any white-space character into a space character and, 
          shrink any n&gt;1 consecutive whitespace characters to exactly 1 whitespace
  
       When the procedure returns, the character array buf contains the newly 
       normalized string and the return value is the new length of the normalized string.
  
  */
  int
  normalize(unsigned char *buf,   /* The character array contains the string to be normalized*/
                      int len     /* the size of the original character array */)
  {
      /* use a for loop to cycle through each character and the built in c funstions to analyze it */
      int i = 0;
      int j = 0;
  
      if(isspace(buf[0])){
          //buf[0] = "";
          i++;
      }
      if(isspace(buf[len-1])){
          //buf[len-1] = "";
          i++;
      }
      for(i;i &lt; len;i++){
          if(isupper(buf[i])) {
              buf[j]=tolower(buf[i]);
              j++;
          }
          if(isspace(buf[i])) {
              buf[j]=' ';
              j++;
          }
          if(isspace(buf[i]) &amp;&amp; isspace(buf[i+1])){
              //buf[i]="";
              i++;
          }
      }
  
      return strlen(buf);
  
  
  }
  </code></pre>