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.
Answer Posted / anand
int stripos ( char* haystack, char* needle, int offset )
{
char *ptr,X,Y;
int diff = 'A'-'a' ,pos=0;
ptr=haystack;
X=(*needle>='A' && *needls<='Z')?*needle-diff:*needle;
while ( *ptr!='\0' )
{ Y=(*ptr>='A' && *ptr<='Z')? *ptr-diff : *ptr )
if( Y == X )
return pos;
pos++;
ptr++;
}
return -1;
}
int offset is of no use in the function. however, the
question does not give any details of offset parameter. even
if provided the function may not require as all strings end
with NULL character ( same as '\0' ).
*needle is converted to small case letter and is compared
with converted small letter of the string.
| Is This Answer Correct ? | 0 Yes | 0 No |
Post New Answer View All Answers
Where register variables are stored in c?
Write a function that will take in a phone number and output all possible alphabetical combinations
What does %p mean?
what is associativity explain what is the precidence for * and & , * and ++ how the folloing declaration work 1) *&p; 2) *p++;
What is string length in c?
application attempts to perform an operation?
What is the default value of local and global variables in c?
Differentiate between a structure and a union.
What is the difference between #include
What is wrong with this initialization?
What is include directive in c?
Tell me the use of bit field in c language?
What is pass by reference in functions?
What are directives in c?
#include