Comparing Arrays

This is a discussion on Comparing Arrays within the C Programming forums, part of the General Programming Boards category; hi, i'm having trouble sorting 2 arrays.. char array1[10]; char array2[10]; i want to print out all characters that exist ...

  1. #1
    Unregistered
    Guest

    Question Comparing Arrays

    hi, i'm having trouble sorting 2 arrays..

    char array1[10];
    char array2[10];


    i want to print out all characters that exist in both array1 + array2, but not the elements that are not common..
    Please Help!
    Dave

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    int main(void){
    
    
    char string1[] = "foobar";
    char string2[] = "flashdaddee";
    
    
    for(unsigned int y = 0; y < strlen(string1); y++){
    
    	for(unsigned int x = 0; x < strlen(string2); x++){
    		if(string1[y] == string2[x]){
    			printf("%c\n",string1[y]);
    			break;
    		}
    	}
    
    }
    return 0;
    
    }
    only 'f' an 'a' from string1 exitst in string2...these are all that are outputted.....

    Oh... I used char arrays, but the principle applies to other arrays too
    Last edited by Fordy; 11-22-2001 at 02:00 PM.

  3. #3
    Unregistered
    Guest

    Talking

    Thank you so very very much, i really appreciate this, i've been tormented with this one for months (bit of a newie)
    thanks!
    cprogramming.com rocks
    Dave

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Comparing Arrays question
    By taugust7 in forum C++ Programming
    Replies: 36
    Last Post: 04-14-2009, 01:29 PM
  2. Comparing elements of character pointer arrays
    By axe in forum C Programming
    Replies: 2
    Last Post: 11-14-2007, 12:20 AM
  3. Comparing 2 elements from 2 different arrays
    By Dan17 in forum C Programming
    Replies: 3
    Last Post: 11-15-2006, 02:43 PM
  4. comparing arrays
    By dustyrain84 in forum C Programming
    Replies: 1
    Last Post: 01-21-2004, 05:52 PM
  5. comparing character arrays
    By Neildadon in forum C++ Programming
    Replies: 2
    Last Post: 04-18-2003, 06:29 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21