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



Write the following function in C. stripos — Find position of first occurrence of a case- inse..

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

Write the following function in C. stripos — Find position of first occurrence of a case- inse..

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

Write the following function in C. stripos — Find position of first occurrence of a case- inse..

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

Post New Answer

More C Interview Questions

int a[3][5]={ {1,2,3,4,5],{2,3,4,5,6},{10,11,12,13,14}}; int *p=&a; printf(“%d”,*(*(x+1)+3));

2 Answers   Wipro,


WHAT IS RTGS N MINIMUM AMT TO B TRANSFERD N WHAT R THE CHARGES ON MINIMUM AMT N IN WHICH BANK WE CAN DO IT?

1 Answers  


#include<stdio.h> main() {int i=1;j=1; for(;;) {if(i>5) break; else j+=1; printf("\n%d",j) i+=j; } }

7 Answers   HCL,


how can i sort numbers from ascending order and descending order using turbo c..

1 Answers  


Can we use visual studio for c?

0 Answers  






What are the key features in c programming language?

0 Answers  


What is string in c language?

0 Answers  


char ch=10;printf("%d",ch);what is the output

14 Answers   Accenture,


How is a structure member accessed?

0 Answers  


Ca some one please help me with aC code to allow user enter numbers from 1 to 20 without repeating and prnt the sum of those numbers thnx

4 Answers  


write a c program to print a given number as odd or even without using loop statements,(no if ,while etc)

10 Answers  


How can I automatically locate a programs configuration files in the same directory as the executable?

0 Answers  


Categories