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 the difference between single charater constant and string constant?

729


What is array in C

809


When do we get logical errors?

725


#include main() { int *p, *c, i; i = 5; p = (int*) (malloc(sizeof(i))); printf(" %d",*p); *p = 10; printf(" %d %d",i,*p); c = (int*) calloc(2); printf(" %d ",*c); }

712


what are the advantages of a macro over a function?

743






How can I change their mode to binary?

789


program to find out date after adding 31 days to a date in the month of febraury also consider the leap year

2698


What is malloc and calloc?

677


What are void pointers in c?

670


How many main () function we can have in a project?

721


What is the process to generate random numbers in c programming language?

722


What is the purpose of clrscr () printf () and getch ()?

689


What are the features of c languages?

721


How can you tell whether a program was compiled using c versus c++?

711


Explain what is #line used for?

702