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

#include<stdio.h>
int stripos(char* haystack, char* needle, int offset );

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

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

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

int is_match(char a, char b)
{
char c1, c2;
if( a >= 'A' && a <= 'Z')
c1 = a + 32;
else
c1 = a;

if( b >= 'A' && b <= 'Z')
c2 = b + 32;
else
c2 = b;

if(c1 == c2)
return 1;
else
return 0;
}

int stripos( char* haystack, char* needle, int offset )
{
int i = 0, j = 0, pos = -1;

if(*haystack == '\0' || *needle == '\0')
return -1;

while(haystack[i + j] != '\0')
{
if(is_match(haystack[i + j],needle[j]))
{
pos = i + 1;
j++;
}
else
{
i++;
pos = -1;
j = 0;
}
if(needle[j] == '\0')
return pos;
}
return -1;
}

Is This Answer Correct ?    1 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

what is bit rate & baud rate? plz give wave forms

1523


What are actual arguments?

650


Where we use clrscr in c?

711


Explain what is the difference between functions abs() and fabs()?

627


What does c mean before a date?

595






What do you mean by a local block?

632


What are void pointers in c?

576


what is a constant pointer in C

683


hello freinds next week my interview in reliance,nybody has an idea about it intervew questions..so tell

1675


Explain null pointer.

626


Explain what is the general form of a c program?

627


How many bytes are occupied by near, far and huge pointers (dos)?

679


What is FIFO?

678


Why is event driven programming or procedural programming, better within specific scenario?

1955


which of the following is not a character constant a) 'thank you' b) 'enter values of p, n ,r' c) '23.56E-o3' d) all of the above

1421