Thread: help with changing array

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    1

    help with changing array

    Hi,

    I have a problem that has been bugging me for days

    i have 2 arrays
    A=[GGGGG GG GGGG GG GGGGG G G G ]
    B=[10101100010101010101010101010110]

    both are same in length

    i need to change Array B in such a way that when Array A has a blank, the same position in Array B changes into character 'P'
    below are my codes,this is a subroutine and arrays are predefined

    Code:
    int ABC()
    {
     int i;
    
     for (i=0;i<=32;i++)
    {if A[i] == ' '
     (B[i] = "P";)
    }
    
    }
    Is there anything wrong with the logic or syntax? Pls help. thanks.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quite a few little things I'd change, along with some bigger ones.
    Code:
    #include <stdio.h>
    
    char A[] = "GGGGG GG GGGG  GG   GGGGG G G G ";
    char B[] = "10101100010101010101010101010110";
    
    void ABC()
    {
       size_t i;
       for ( i = 0; i < sizeof A / sizeof *A - 1; ++i )
       {
          if ( A[i] == ' ' )
          {
             B[i] = 'P';
          }
       }
    }
    
    int main(void)
    {
       printf("[%s]\n", A);
       printf("[%s]\n", B);
       puts("change:");
       ABC();
       printf("[%s]\n", A);
       printf("[%s]\n", B);
       return 0;
    }
    
    /* my output
    [GGGGG GG GGGG  GG   GGGGG G G G ]
    [10101100010101010101010101010110]
    change:
    [GGGGG GG GGGG  GG   GGGGG G G G ]
    [10101P00P1010PP10PPP01010P0P0P1P]
    */
    [edit]Perhaps I'd even use this for the loop condition instead.
    Code:
       int i;
       for ( i = 0; A[i] != '\0'; ++i )
    [edit=again, can't I do things right the first time?]
    Is your array a char array? And is it a C-string? Such things make a difference in some of the choices I've made above.
    Last edited by Dave_Sinkula; 11-14-2005 at 11:12 AM. Reason: Added color.
    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
    Join Date
    Mar 2005
    Posts
    140
    ^ beat me

    the syntax of your if statement is wrong

    P is a single character, so you should use single quotes instead of double quotes

    Code:
    if(expression) 
    {
       //do stuff
    }
    Code:
    for (i=0;i<=32;i++)
    {
       if (A[i] == ' ')
       {
          B[i] = 'P';
       }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Changing size of array of pointers
    By carrotcake1029 in forum C Programming
    Replies: 1
    Last Post: 11-15-2008, 10:24 PM
  2. array of pointers/pointer arithmetic
    By tlpog in forum C Programming
    Replies: 18
    Last Post: 11-09-2008, 07:14 PM
  3. 1-D array
    By jack999 in forum C++ Programming
    Replies: 24
    Last Post: 05-12-2006, 07:01 PM
  4. changing value in an array by using a function
    By nunnu in forum C Programming
    Replies: 3
    Last Post: 05-06-2004, 03:25 AM
  5. two dimensional dynamic array?
    By ichijoji in forum C++ Programming
    Replies: 6
    Last Post: 04-14-2003, 04:27 PM