Thread: checking an array for a value

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    16

    checking an array for a value

    Hello. I have a small piece of code here, allowing the user to enter 16 characters from 'a' to
    'z'. What would be the best way to make the code check if the character hasn't been entered before (is in the array) ?

    Code:
    for (i=0; i<16; i++){
    printf("%d: ",i+1);
    while(((virsotne[i]=getch())<97) || (virsotne[i]>122));
    printf("%c\n",virsotne[i],virsotne[i]);
    }

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    You will have to cycle thru the array and compare each number. Write a function to make repeating this easier and pass the array in:

    Code:
    int checkArray (char *array, char ch) {
          int i, len = strlen(array);
          for (i=0;i<len;i++) {
               if (array[i] == ch) return 1;
          }
          return 0;
    }
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    16
    Actually I knew the algorithm to use for this array check, but the problem was to include it in the while statement. Somehow I forgot about the possibility of a function there - now everything works, thanks MK27

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 1-D array
    By jack999 in forum C++ Programming
    Replies: 24
    Last Post: 05-12-2006, 07:01 PM
  2. 2d array question
    By gmanUK in forum C Programming
    Replies: 2
    Last Post: 04-21-2006, 12:20 PM
  3. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  4. Checking maximum values for dynamic array...
    By AssistMe in forum C Programming
    Replies: 1
    Last Post: 03-21-2005, 12:39 AM
  5. Replies: 5
    Last Post: 05-30-2003, 12:46 AM