Need logic for this function

This is a discussion on Need logic for this function within the C Programming forums, part of the General Programming Boards category; Write the following function in C. stripos Find position of first occurrence of a case-insensitive string int stripos ( ...

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    2

    Question Need logic for this function

    Write the following function in C.

    stripos Find position of first occurrence of a case-insensitive string
    int stripos ( char* haystack, char* needle, int offset )

    Returns the numeric position of the first occurrence of needle in the haystack string. Note that the needle may be a string of one or more characters. If needle is not found, stripos() will return -1.

    The function should not make use of any C library function calls

  2. #2
    C++11 User Tux0r's Avatar
    Join Date
    Nov 2008
    Location
    Sweden
    Posts
    135
    Quote Originally Posted by Sunil4u View Post
    Write the following function in C.

    stripos Find position of first occurrence of a case-insensitive string
    int stripos ( char* haystack, char* needle, int offset )

    Returns the numeric position of the first occurrence of needle in the haystack string. Note that the needle may be a string of one or more characters. If needle is not found, stripos() will return -1.

    The function should not make use of any C library function calls
    Are you telling me I'm supposed to do it?

    At least you could have added something yourself.

  3. #3

  4. #4
    Registered User
    Join Date
    Jul 2009
    Posts
    2
    Quote Originally Posted by Tux0r View Post
    Are you telling me I'm supposed to do it?

    At least you could have added something yourself.


    Need logic only!!!!!!!!!!!

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,643
    >> Need logic for this function

    It's generally a good idea to post the code that you're working with.
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  6. #6
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    Need logic only!!!!!!!!!!!
    What else is there ?

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,643
    >> Need logic only!!!!!!!!!!!

    Advance to 'offset'. If it's past the end, bail out. Starting there (let's call it position X), traverse haystack and needle in step until you reach the end of either one or the match fails. If you've reached the end of needle, then you've got a match, so return X. Otherwise, move on to the next character in haystack (X+1) and restart the search.
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  8. #8
    Registered User Cooloorful's Avatar
    Join Date
    Feb 2009
    Posts
    59
    Code:
    #define OFFSET ('a' - 'A')
    
    int value(int c)
    {
      if(c >= 'a' &&  c <= 'z')
        return c - OFFSET;
    
      return c;
    }
    
    /*
     * While I am sure Professor X at your school has not gone over
     * const I am still going to use them for my own sanity.
     */
    
    int stripos(const char* haystack, const char* needle, int offset)
    {
      int pos, i, j;
    
      for(pos = 0; offset-- && haystack[pos]; pos++)
        ;
    
      if(!haystack[pos])
        return -1;
    
      do
      {
        for(i = pos, j = 0; value(haystack[i]) == value(needle[j]) && haystack[i] && needle[j]; i++, j++)
          ;
    
        if(needle[j] == '\0')
          return pos;
    
        if(haystack[i] == '\0')
          break;
      } while(haystack[++pos]);
    
      return -1;
    }

  9. #9
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,643
    Just keep in mind that is generally better to post advise rather than code solutions. That way, the OP can learn to work out the problem himself rather than simply copy the code of others and, potentially, submit it as his own.
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 06:05 PM

Tags for this Thread


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