Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Finding out word occurence without c library calls (http://www.go4expert.com/forums/finding-word-occurence-c-library-calls-t10353/)

myinboxid 3May2008 08:06

Finding out word occurence without c library calls
 
Hi,

I got benifited a lot from this forum.Can you please have a look at my requirement and assist me in the same.


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.





Thanks in Advance
sri.

asadullah.ansari 7May2008 19:02

Re: Finding out word occurence without c library calls
 
This is just idea make it efficient when you are goin to write coode

1. Take two pointer ptr1 and ptr2 pointing to haystack and needle respectively.
2. if contain of ptr1 is equal to contained of ptr2 then
++ptr1 and ++ptr2
3. if ptr2 points to NULL and ptr1 also points to space then
print " you got that string"
return
4. if ptr2 points to NULL and ptr1 not points to space then
goto step 5
5. if contained of ptr1 is not equal to contained of ptr2 then
increment ptr1 untill space come and go to for next string
reset ptr2 (ptr2 should point to again needle
6. if contained of ptr1 is not equal to contained of ptr2 and ptr1==NULL
return -1

fgpaz13 14May2008 18:11

Re: Finding out word occurence without c library calls
 
Hi myinboxid,

did you manage to write the code?
if so can you share it with us. it would help alot! thanks!

mr.anandc 15May2008 16:05

Re: Finding out word occurence without c library calls
 
Try this......
Code:

#include <stdio.h>

int stripos(char *haystack, char *needle)
{
char *ptr1, *ptr2, *ptr3;

if( haystack == NULL || needle == NULL)
return -1;

for (ptr1 = haystack; *ptr1; ptr1++)
{
for (ptr2 = ptr1, ptr3 = needle; *ptr3 && (*ptr2 == *ptr3); ptr2++, ptr3++);
if (!*ptr3)
return ptr1 - haystack + 1;
}
return -1;
}

int main()
{
char str1[] = "Hello world";
char str2[] = "lo world";
char str3[] = "Bye";
printf("location of str2 in str1 is %d\n", stripos(str1, str2));
printf("location of str3 in str1 is %d\n", stripos(str1, str3));
return 0;
}

This is case-sensitive checking... if case-insensitive required, this can be done by changing the (*ptr2 == *ptr3) condition in for loop with a function which compares characters in case-insensitive way.

fgpaz13 15May2008 16:52

Re: Finding out word occurence without c library calls
 
thanks a lot mr.anandc..:D

shabbir 15May2008 17:12

Re: Finding out word occurence without c library calls
 
Please use code block when you have code snippets in posts

rebecca_lourdes86 26Jan2010 12:11

Re: Finding out word occurence without c library calls
 
Hello,

Please explain to me about this question. I seriously do not understand what this question is asking for. the question is as below:

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. The code should be developed entirely by you and not extracted from other's work.

xpi0t0s 28Jan2010 03:50

Re: Finding out word occurence without c library calls
 
It's fairly easy, except I do not know what "offset" is for; the question does not specify this. What it means is that it finds the existence of one string (let's say "lo reb") inside another (let's say "Hello Rebecca"), without considering case. So stripos("Hello Rebecca","lo reb",0) would return 3, and stripos("Hello Rabbit, hello Rebecca","lo reb",0) would return 18. This differs from the standard C library function strstr in that strstr is case sensitive (also iirc strstr returns a char* not an int). So one solution would be to find the source code for strstr() and replace "if (str1[i]==str2[j])" with "if (toupper(str1[i])==toupper(str2[j]))", although that would technically constitute "extracted from other's work"...

sahems87 9Jun2010 18:54

Re: Finding out word occurence without c library calls
 
i too got the same question for my technica test...
kindly pls let me know the answer for this question.. plz post the code

xpi0t0s 9Jun2010 19:11

Re: Finding out word occurence without c library calls
 
The idea is that *you* are supposed to write the code. The test is testing your programming skills, not mine.
Post what you've got so far and explain in detail where you're stuck, and we'll try to unstick you, but we won't do your homework for you.


All times are GMT +5.5. The time now is 20:28.