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 / 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 |
Answer / amixcode
Hi "VadivelT" can u please why there is "-32" in the answer ??
i googled lot but didnt found the aswer.
Is This Answer Correct ? | 2 Yes | 0 No |
Answer / 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 |
What is an array in c?
Here is a neat trick for checking whether two strings are equal
What are pointers really good for, anyway?
What would be an example of a structure analogous to structure c?
What are pointers? What are stacks and queues?
Sir i want e-notes of C languge of BAlaguruswami book i.e scanned or pdf file of balaguruswamy book on c language.PLEASE SEND ME on my mail id ajit_kolhe@rediff.com
What is the difference between text and binary i/o?
What is difference between structure and union in c programming?
Is c still used in 2019?
Struct(s) { int a; long b; } Union (u) {int a; long b; } Print sizeof(s)and sizeof(u) if sizeof(int)=4 and sizeof(long)=4
Why & is used in scanf in c?
In header files whether functions are declared or defined?