Thread: Adding values to array in a for loop

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    12

    Adding values to array in a for loop

    Am very close to working out all the kinks and need help with one last thing. This code is a simplified version of what am going to do.

    Code:
    int main()
    {
        char string1[] = "abc";
        char string2[] = "abd";
        int i,k;
        for(i=0; i<3; i++){
        for(k=0; k<3; k++){
        if(string1[i] == string2[k]){
    
        printf("%c", string2[k]);
                                              <----------------------- THIS IS WHERE I want the array to take in the values that pass the if statement. 
    
        }
        }
    
    
        }
    
        return 0;
    }

    Now I can't just place a simple array in there since its in a for loop, every time a new value passes the if statement the array will only get overwritten. Basically I need to keep stacking values into an array in a loop but not every time the loop is run. How can this be done?

  2. #2
    Registered User
    Join Date
    Oct 2010
    Posts
    28
    Your question is not clear.

    but if, as far i understood,you want to enter values when ever your if statement is true.
    for eg.
    Code:
     
    for(i=0;i<n;i++)
      { if(statement)
          { scanf("%c",a[i]);
    
            }
    
        }
    above when your if is true you enter a value, else loop continues.

    I tried to tell what i thought you are looking for.

    if not helpful paste your code and tell what exactly you want to do.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 09-23-2010, 11:49 AM
  2. Replies: 3
    Last Post: 08-16-2010, 10:00 AM
  3. putting values into an array
    By zdream8 in forum C Programming
    Replies: 15
    Last Post: 05-21-2008, 11:18 PM
  4. Inputted values into an array
    By Panther in forum C Programming
    Replies: 6
    Last Post: 04-11-2003, 10:15 AM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM