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


Please Help Members By Posting Answers For Below Questions

Explain the difference between null pointer and void pointer.

674


Why c language?

649


What is the process to generate random numbers in c programming language?

611


Is it better to bitshift a value than to multiply by 2?

661


‘SAVEPOINT’ and ‘ROLLBACK’ is used in oracle database to secure the data comment. Give suitable examples of each with sql command.

1880






Which is more efficient, a switch statement or an if else chain?

584


What is optimization in c?

569


Add Two Numbers Without Using the Addition Operator

356


can any one provide me the notes of data structure for ignou cs-62 paper

1707


What is formal argument?

653


What is #define in c?

622


What is exit() function?

562


What does c mean?

589


Is linux written in c?

602


What is Dynamic memory allocation in C? Name the dynamic allocation functions.

559