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

#include<stdio.h>

int stripos(char* haystack, char* needle, int offset );

void main()
{
char a1[200], a2[20];
int iPostn = 0;
printf("ENTER THE HAYSTACK STRING:\n");
gets(a1);
printf("\nENTER THE STRING - TO BE SEARCHED\n");
gets(a2);
iPostn = stripos(&a1[0], &a2[0], iPostn);

if(iPostn > 0)
printf("\nSTRING STARTS AT THE POSITION: %d\n", iPostn);

else
printf("\nSTRING NOT FOUND: %d\n", iPostn);

getch();
}

int stripos(char* haystack, char* needle, int offset )
{
char ch, *needle1;
int pos = 0, temp;

if(haystack != '\0' && needle != '\0')
{
while(*haystack != '\0')
{
needle1 = needle;
pos++;
temp = 0;
if((*haystack == *needle1) || (*haystack == *needle1 - 32)
|| (*haystack == *needle1 + 32))
{
offset = pos;
needle1++;
haystack++;
while(*needle1 != '\0')
{
if((*haystack == *needle1) || (*haystack ==
*needle1 - 32) || (*haystack == *needle1 + 32))
{
haystack++;
needle1++;
pos++;
}
else
{
temp = 1;
offset = -1;
break;
}
}
}
if(offset > 0)
{
return offset;
}

if(temp != 1)
haystack++;
}
}
return offset;
}

Is This Answer Correct ?    13 Yes 4 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is string in c language?

628


What are the applications of c language?

627


What functions are in conio h?

662


in ‘C’ language for Matrix Multiplication fails” Introspect the causes for its failure and write down the possible reasons for its failure.

7411


If a variable is a pointer to a structure, then which operator is used to access data members of the structure through the pointer variable?

781






What is array in C

711


Why string is used in c?

583


how much salary u want ? why u join in our company? your domain is core sector why u prefer software ?

1507


What is difference between structure and union with example?

598


What is null in c?

601


C program execution always begins with a) #include b) comment (/*-------*/) c) main() d) declaration instructions

613


A variable that is defined in a specified portion of a program but can be used throughout the program a) global variable b) local variable c) character d) none

734


Why is void main used?

622


How will you delete a node in DLL?

687


If null and 0 are equivalent as null pointer constants, which should I use?

582