Thread: Need logic for this function

  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,708
    >> Need logic for this function

    It's generally a good idea to post the code that you're working with.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  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,708
    >> 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:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  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,708
    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:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

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, 11: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, 07:05 PM

Tags for this Thread