Thread: clean array of blanks, array subscripting

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    99

    clean array of blanks, array subscripting

    I've been trying to understand why I'm getting wrong output with this function. It must be somewhere where I count the chars in the array.
    Here it goes.
    Code:
    int clean_array(char array[], char clean[], int size)
    {
        int i, j = 0;
    
    /* j is to count index of the array cleaned out of      */
    /*blanks and punct marks. i is for the original array */ 
    
        for(i = 0; i < size; i++)
        {
           if( isalpha(array[i]) )
           {                                 /* if char is a letter, put it in the */
              clean[j] = array[i];   /* other array. Increment its index */
              j++;                
           }                      
        }
     
        clean[j] = '\0';      /* as index is incremented already, just */     
                                    /* close the string */
     
        return (j-1);         /* return the number of chars in string */       
    }
    So, when I input a random choice of chars, e.g
    jhy ut!
    and store it in the original array, the program cleans it up correctly and my result is (using puts):
    jhyutP
    The last letter I believe is something random, maybe from outside of the clean string.
    What did I do wrong with the subscripting?

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Can you post the context of the call?
    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    int clean_array(char array[], char clean[], int size)
    {
        int i, j = 0;
        for(i = 0; i < size; i++)
        {
           if( isalpha(array[i]) )
           {
              clean[j++] = array[i];
           }                      
        }
        clean[j] = '\0';
        return (j-1);
    }
    
    int main( void )
    {
       char original[] = "abc 123 QRS !@# xyz";
       char result [ sizeof original ];
       puts(original);
       clean_array(original, result, sizeof original);
       puts(result);
       return 0;
    }
    
    /* my output
    abc 123 QRS !@# xyz
    abcQRSxyz
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User Sake's Avatar
    Join Date
    Jan 2005
    Posts
    89
    >>return (j-1);
    This is wrong. You need to return j, not j-1. Most likely the problem your having is that you are passing a size to clean_array that is one too much.
    Kampai!

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    99
    Whether it returns j or j-1, the result is exactly the same...
    And the call for this function is this:
    Code:
    clean_number = clean_array(message, clean_message, LEN);
    I'm have no idea what is going on. Watever change I have made so far with indexing, the result is the same...

  5. #5
    Registered User Sake's Avatar
    Join Date
    Jan 2005
    Posts
    89
    What is LEN defined as?
    Kampai!

  6. #6
    Registered User
    Join Date
    Sep 2004
    Posts
    99
    #define LEN 40

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    How hard is this to figure out, really? Dave already showed you that the function itself works as expected. Therefore, you're either:
    a) Using it incorrectly.
    b) Declaring your variables incorrectly.
    c) Doing something else stupid.
    d) All of the above.

    The function works, your use of it is wrong. Now figure out why. It's allready been demonstrated that the function itself gives the expected output. Therefore, it is not the problem.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  2. Replies: 7
    Last Post: 11-25-2008, 01:50 AM
  3. Replies: 4
    Last Post: 11-02-2006, 11:41 AM
  4. 1-D array
    By jack999 in forum C++ Programming
    Replies: 24
    Last Post: 05-12-2006, 07:01 PM
  5. mode of an array
    By Need Help in forum C Programming
    Replies: 15
    Last Post: 09-17-2001, 08:03 AM