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
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 |
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 |
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 |
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 |
the maximum length of a character constant can be a) 1 character b) 8 characters c) 256 chaacters d) 125 characters
What is meaning of "Void main" in C Language.
24 Answers Ford, GU, HCL, IBIBS, JUW, TCS,
What is function pointer and where we will use it
how to find a 5th bit is set in c program
What is dynamic variable in c?
a function gets called when the function name is followed by a a) semicolon (;) b) period(.) c) ! d) none of the above
difference between semaphores and mutex?
void swap(int a,int b) { a=a+b; b=a-b; a=a-b; } in this code always gives the same result for all case
Please write the area of a RIGHT ANGLED TRIANGLE.
What is calloc malloc realloc in c?
Write a C program to find the smallest of three integers, without using any of the comparision operators.
plz answer....A program that takes 3 variables e.g a,b,c in as seperate parameters and rotates the values stored so that value goes a to b, b to c and c to a .