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
Explain the difference between null pointer and void pointer.
Why c language?
What is the process to generate random numbers in c programming language?
Is it better to bitshift a value than to multiply by 2?
‘SAVEPOINT’ and ‘ROLLBACK’ is used in oracle database to secure the data comment. Give suitable examples of each with sql command.
Which is more efficient, a switch statement or an if else chain?
What is optimization in c?
Add Two Numbers Without Using the Addition Operator
can any one provide me the notes of data structure for ignou cs-62 paper
What is formal argument?
What is #define in c?
What is exit() function?
What does c mean?
Is linux written in c?
What is Dynamic memory allocation in C? Name the dynamic allocation functions.