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

struct screen_pos{ int row, col } ;move_right(cursor)struct screen_pos *cursor;{ cursor.col++; } /* This statementhas a syntax error */What is the correct statement a) cursor.col = cursor.col + 1; b) col.cursor++; c) *cursor.col++; d) pointer

0 Answers  


Define a structure to store roll no, name and marks of a student. Using the structure of above write a ‘C’ program to create a file “student.dat”. There must be one record for every student in the file. Accept the data from the user.

0 Answers  


What is the difference between arrays and pointers?

0 Answers  


What is difference between %d and %i in c?

0 Answers  


How can I set an array's size at run time?

9 Answers  






What are header files and explain what are its uses in c programming?

0 Answers  


What is the use of keyword VOLATILE in C?

1 Answers  


accept character from keyboard untill the user presses the enter key.If the user enters any character other than upper case(A-Z)alphabets program should stop taking any input

1 Answers  


What are qualifiers?

0 Answers  


int a=2,b=3,c=4; printf("a=%d,b=%d\n",a,b,c); what is the o/p?

6 Answers   Verifone,


What is the use of bitwise operator?

0 Answers  


In C language, a variable name cannot contain?

0 Answers  


Categories