# Thread: Need logic for this function

1. ## 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. Originally Posted by Sunil4u
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. Originally Posted by Tux0r
Are you telling me I'm supposed to do it?

At least you could have added something yourself.

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

4. >> Need logic for this function

It's generally a good idea to post the code that you're working with.

5. Need logic only!!!!!!!!!!!
What else is there ?

6. >> 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.

7. 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;
}```

8. 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.