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 |
Total of how many functions are available in c?
What is structure padding in c?
what is meant by the "equivalence of pointers and arrays" in C?
what are non standard function in c
What does the file stdio.h contain?
Explain what is wrong with this statement? Myname = ?robin?;
If I have a char * variable pointing to the name of a function ..
Is it better to use malloc() or calloc()?
What are the restrictions of a modulus operator?
Explain what does the format %10.2 mean when included in a printf statement?
why Language C is plateform dependent
What is the concatenation operator?