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.

Answers were Sorted based on User's Feedback



Write the following function in C. stripos — Find position of first occurrence of a case- inse..

Answer / varun vithalani

I am working on the answer now but i can say that answer 1
and 2 are absolutely wrong. I haven't checked the 3rd yet.
In 1 and 2 solutions, it only looks for the first character
of the needle and does not care about the remaining.

For example:
haystack = "sweetsugar"
needle = "sugar"

It will return value '0' since it matches the first 's' of
'sweetsugar' and 'sugar'. The answer should be '5'.

Is This Answer Correct ?    3 Yes 1 No

Write the following function in C. stripos — Find position of first occurrence of a case- inse..

Answer / anand

int stripos ( char* haystack, char* needle, int offset )
{
char *ptr;
ptr=haystack;
int pos=0;
while ( *ptr!='\0' )
{
if( *ptr == *needle )
return pos;
pos++;
ptr++;
}

return -1;
}

this function written for exact match of the charecter and
dosent bother for whatever is offset.

Is This Answer Correct ?    3 Yes 2 No

Write the following function in C. stripos — Find position of first occurrence of a case- inse..

Answer / 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

Write the following function in C. stripos — Find position of first occurrence of a case- inse..

Answer / anand kanawally

int stripos ( char* haystack, char* needle, int offset )
{
char *ptr;
ptr=haystack;
int pos=0;
while ( *ptr!='\0' )
{
if( *ptr == *needle )
return pos;
pos++;
ptr++;
}

return -1;
}

Is This Answer Correct ?    4 Yes 6 No

Post New Answer

More C Interview Questions

Total of how many functions are available in c?

3 Answers  


What is structure padding in c?

0 Answers  


what is meant by the "equivalence of pointers and arrays" in C?

3 Answers   Satyam,


what are non standard function in c

0 Answers  


What does the file stdio.h contain?

0 Answers  






Explain what is wrong with this statement? Myname = ?robin?;

0 Answers  


If I have a char * variable pointing to the name of a function ..

0 Answers  


Is it better to use malloc() or calloc()?

0 Answers   Aspire, Infogain,


What are the restrictions of a modulus operator?

0 Answers  


Explain what does the format %10.2 mean when included in a printf statement?

0 Answers  


why Language C is plateform dependent

3 Answers   Siemens, Wipro,


What is the concatenation operator?

0 Answers  


Categories