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

Can you return null in c?

0 Answers  


main() { printf(5+"good morning"); printf("%c","abcdefgh"[4]); }the o/p is morning and e...how someone explain

1 Answers  


How is a null pointer different from a dangling pointer?

0 Answers  


What are called c variables?

0 Answers  


Write a program using two-dimensional array that lists the odd numbers and even numbers separately in a 12 input values.

1 Answers  






Main must be written as a.the first function in the program b.Second function in the program c.Last function in the program d.any where in the program

19 Answers   CTS, HCL, TCS,


how to add our own function in c library please give details.?

1 Answers   TCS,


Design a program using an array that searches a number if it is found on the list of the given input numbers and locate its exact location in the list.

4 Answers  


write a program in C to swap two variables

7 Answers   Attrabyte, Marlabs,


Read the following data in two different files File A: aaaaaaaadddddddd bbbbbbbbeeeeeeee ccccccccffffffff File B: 11111111 22222222 33333333 By using the above files print the following output or write it in the Other file as follows aaaaaaaa11111111dddddddd bbbbbbbb22222222eeeeeeee cccccccc33333333ffffffffffff

0 Answers  


What are the characteristics of arrays in c?

0 Answers  


Is r written in c?

0 Answers  


Categories