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 |
c program to subtract between two numbers without using '-' sign and subtract function.
How do I initialize a pointer to a function?
Who is the founder of c language?
i want to asked a question about c program the question is: create a c program that displays all prime numbers less than 500? using looping statement
why the execution starts from main function
swap 2 numbers without using third variable?
There are N egg baskets and the number of eggs in each basket is a known quantity. Two players take turns to remove these eggs from the baskets. On each turn, a player must remove at least one egg, and may remove any number of eggs provided they all belong to the same basket. The player picking the last egg(s) wins the game. If you are allowed to decide who is going to start first, what mathematical function would you use to decide so that you end up on the winning side? Upload a C program to demonstrate the behaviour of the game.
main() { int i,n=010; int sum=0; for(i=1;i<=n;i++) {s=s+i; } printf("%d",&s); getch(); }
main use of recursive function a) processing speed high b) reduce program length/reduce repeated statements c) if you do not, use iterative methods like, for, while or do-while d) all the above
Should I use symbolic names like true and false for boolean constants, or plain 1 and 0?
int main() { unsigned char a = 0; do { printf("%d=%c\n",a,a); a++; }while(a!=0); return 0; } can anyone please explain me output????
write a program in reverse the string without using pointer,array,global variable declaration,lib fun only using a function?