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
What are the valid places to have keyword “break”?
What is #define used for in c?
Write a program for finding factorial of a number.
What are structural members?
in iso what are the common technological language?
What is ponter?
What is an array? What the different types of arrays in c?
Explain the advantages and disadvantages of macros.
What is a keyword?
cin.ignore(80, _ _);This statement a) ignores all input b) ignores the first 80 characters in the input c) ignores all input till end-of-line d) iteration
Write a c program to build a heap method using Pointer to function and pointer to structure ?
What is string in c language?
Explain how can I make sure that my program is the only one accessing a file?
plz let me know how to become a telecom protocol tester. thank you.
Differentiate between new and malloc(), delete and free() ?