Thread: Find '&' character in char array?

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    11

    Find '&' character in char array?

    I need to loop through a char array containing a line read by fgets and check for the '&' character. I thought that fgetc or getc might do the trick, but it seems that they can only recieve a FILE *stream, not an array. This is what i tried:
    Code:
    void run (char cur[]) {
        char c;
        char flag;
        c = fgetc(cur); // or getc
        while(c != EOF){
            if(c == '&') {
                printf("you wrote & char\n");
                flag = 'y';
            } else {
                printf("you didn't write the & char\n");
            }
        }
    "Passing arg 1 of 'fgetc' from incompatible pointer type."

  2. #2
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    Assuming cur is NULL terimated, just run through a loop from 0 to < strlen(cur) checking each character in cur against '&'.

    If cur isn't NULL terminated, then you'd also need to know the number of chars in cur.

  3. #3
    Cat Lover
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    109
    Why not just something like
    Code:
    for(int n=0;n<length;n++)
    {
      if(arr[n] == '&')
        return true;
    }
    return false;
    That's assuming you know the length of your array, which you should.
    Alternatively, assuming it has a sentinel value e.g. \0, you can just loop while(arr[n] != '\0')

  4. #4
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    Code:
    for(int n=0;n<length;n++)
    In C code?

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    11
    Thanks a lot! Didn't know that it could be done that simple.
    Code:
        int d;
        for(d=0;d<strlen(cur);d++) {
            if(cur[d] == '&')
                printf("you wrote & sign\n");
            }

  6. #6
    Cat Lover
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    109
    Quote Originally Posted by SKeane
    Code:
    for(int n=0;n<length;n++)
    In C code?
    Well, close enough. I appear to have thought I was in the C++ forum.

  7. #7
    Register User andor's Avatar
    Join Date
    Aug 2006
    Location
    Novi Sad
    Posts
    42
    Quote Originally Posted by tidemann
    for(d=0;d<strlen(cur);d++)
    Instead of strlen in loop, calculate the length of cur in front of the loop.

  8. #8
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    If cur is NULL terminated...

    Code:
    if ( strchr(cur, '&') != NULL ) 
    {
        printf("You wrote a & sign\n");
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Copy character array to char * pointer
    By NuNn in forum C Programming
    Replies: 3
    Last Post: 02-20-2009, 12:33 AM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  4. Replies: 7
    Last Post: 06-16-2006, 09:23 PM
  5. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 12:36 PM