C++ School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a C++ Expert!

Join 300,329 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,835 people online right now. Registration is fast and FREE... Join Now!




How to implement simple "stop-word/skip-list" function?

 

How to implement simple "stop-word/skip-list" function?

agent_logic

28 Mar, 2009 - 08:36 AM
Post #1

New D.I.C Head
*

Joined: 21 Jul, 2008
Posts: 29


My Contributions
Hi guys,

I have a C programming homework where I have to implement a function that will "stop/skip" words entered by a user that are less than 3 characters in length (example: a, the, on, if, lol, wtf etc.). There are no specific "target" words that need to be ignored. Anything equal to or less than 3 characters in length should be ignored. Any ideas how I should go about it? I googled skip lists and word stopping implementation, and each one of them I came across mostly went way over my head! blink.gif The requirement they expect of me is very simple, yet I'm too disillusioned by the codes I've been reading and other codes I've been writing that I'm simply not able to think of a simple implementation of said function. crazy.gif Any ideas would be greatly appreciated!

Thanks in advance.

This post has been edited by agent_logic: 28 Mar, 2009 - 08:36 AM

User is offlineProfile CardPM
+Quote Post


PsychoCoder

RE: How To Implement Simple "stop-word/skip-list" Function?

28 Mar, 2009 - 08:43 AM
Post #2

Dyslexics Untie!
Group Icon

Joined: 26 Jul, 2007
Posts: 14,701



Thanked: 501 times
Dream Kudos: 11450
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net, jQuery

My Contributions
Dream.In.Code has a policy by which we prefer to see a good faith effort on your part before providing source code for homework assignments. Please post the code you have written in an effort to resolve the problem, and our members would be happy to provide some guidance. Be sure to include a description of any errors you are encountering as well.

Please post like this:

Thank you for helping us helping you.
User is offlineProfile CardPM
+Quote Post

agent_logic

RE: How To Implement Simple "stop-word/skip-list" Function?

28 Mar, 2009 - 08:52 AM
Post #3

New D.I.C Head
*

Joined: 21 Jul, 2008
Posts: 29


My Contributions
Hi PsychoCoder,

That's my dilemma, I haven't been able to think of a workable soultion for my problem. I'm not asking anyone to write code for me, I'm just asking for ideas and a few pointers in the right direction in terms of logic for my problem. I hope this is not against D.I.C policy, asking for pointers rather than code.

Cheers.
User is offlineProfile CardPM
+Quote Post

bsaunders

RE: How To Implement Simple "stop-word/skip-list" Function?

28 Mar, 2009 - 09:59 AM
Post #4

D.I.C Addict
****

Joined: 18 Jan, 2009
Posts: 554



Thanked: 42 times
My Contributions
Can I ask what you mean by "skip" a word?
User is offlineProfile CardPM
+Quote Post

agent_logic

RE: How To Implement Simple "stop-word/skip-list" Function?

28 Mar, 2009 - 06:08 PM
Post #5

New D.I.C Head
*

Joined: 21 Jul, 2008
Posts: 29


My Contributions
For example, if the user enters "This is a query", return "This query." "is" and "a" have been skipped because they are less than 3 characters in length. Something like what search engines do, but this one is a lot simpler.
User is offlineProfile CardPM
+Quote Post

bsaunders

RE: How To Implement Simple "stop-word/skip-list" Function?

28 Mar, 2009 - 06:18 PM
Post #6

D.I.C Addict
****

Joined: 18 Jan, 2009
Posts: 554



Thanked: 42 times
My Contributions
Do you know how to read a string from standard input?
User is offlineProfile CardPM
+Quote Post

agent_logic

RE: How To Implement Simple "stop-word/skip-list" Function?

28 Mar, 2009 - 06:23 PM
Post #7

New D.I.C Head
*

Joined: 21 Jul, 2008
Posts: 29


My Contributions
Yeah, I know the getting-input-from-user part of it. It is just the working-with-the-input part I'm baffled with. I'm probably wrong, but I have a vague feeling that a combination of substr() and strlen() might do the job...

This post has been edited by agent_logic: 28 Mar, 2009 - 06:26 PM
User is offlineProfile CardPM
+Quote Post

bsaunders

RE: How To Implement Simple "stop-word/skip-list" Function?

28 Mar, 2009 - 06:29 PM
Post #8

D.I.C Addict
****

Joined: 18 Jan, 2009
Posts: 554



Thanked: 42 times
My Contributions
You're using C++ and not C, right?
User is offlineProfile CardPM
+Quote Post

NickDMax

RE: How To Implement Simple "stop-word/skip-list" Function?

28 Mar, 2009 - 06:49 PM
Post #9

Can grep dead trees!
Group Icon

Joined: 18 Feb, 2007
Posts: 5,215



Thanked: 285 times
Dream Kudos: 1175
Expert In: Java/C++

My Contributions
generally I would say "RegEx" but well we are in C.

Basically if you are just removing any words less then 3 chars long this is not hard, just set up a "result" buffer and scan over the input string and copy over any words longer than 3 letters.

CODE
!!! Not compiled -- just off the top of my head to show you what I am thinking !!!
char input[] = "I had a tiny turtle his name was tiny tim";
char buffer[sizeof(input)];
char *bufPtr = buffer;
char *basePtr = input;
char *ptr = input;
while(*ptr != 0) {
    if (*ptr = 32) {
        if (ptr - basePtr > 3) { //if it is long copy it over
            while (basePtr <  ptr) { *bufPtr++ = *basePtr++; }
        } else {
            basePtr = ptr; //ignore the short little word
        }
    ptr++;
}
*bufPtr = 0; // null terminate string
printf("result is: %s", buffer);
-- Note that I can almost guarantee that the above code has bugs, but it should show you a direction to go.
User is offlineProfile CardPM
+Quote Post

agent_logic

RE: How To Implement Simple "stop-word/skip-list" Function?

28 Mar, 2009 - 07:14 PM
Post #10

New D.I.C Head
*

Joined: 21 Jul, 2008
Posts: 29


My Contributions
@bsaunders: No mate, I'm using C.

@ NickDMax: Thanks for the logic mate, I have something to work towards now! biggrin.gif I'll work on it and get back. By the way, I did read about RegEx in C, but it looked way complicated than the ones I've come across in php and perl, so I dropped the idea lol.

Cheers.
User is offlineProfile CardPM
+Quote Post

bsaunders

RE: How To Implement Simple "stop-word/skip-list" Function?

28 Mar, 2009 - 07:55 PM
Post #11

D.I.C Addict
****

Joined: 18 Jan, 2009
Posts: 554



Thanked: 42 times
My Contributions
NickDMax's solution should work. The only way I can think of in C is:

CODE
    char line[80];
    char *linePtr;

    linePtr = line;

    /* Read a line from standard input */
    gets(line);

    while(*linePtr) {
        char word[20];

        /* Extract a word from the line string */
        sscanf(linePtr, "%s", word);

        if(strlen(word) > 3) { /* Is the word longer than three letters? */
            static int firstWord = 1;

            /* Print the word to standard output */

            /* Print a space before the word if it isn't the first word to
               seperate it from the previous word */
            if(!firstWord)
                printf(" ");

            printf(word);

            firstWord = 0;
        }

        linePtr += strlen(word);
    }


This post has been edited by bsaunders: 28 Mar, 2009 - 08:04 PM
User is offlineProfile CardPM
+Quote Post

agent_logic

RE: How To Implement Simple "stop-word/skip-list" Function?

29 Mar, 2009 - 09:55 AM
Post #12

New D.I.C Head
*

Joined: 21 Jul, 2008
Posts: 29


My Contributions
That is pure genius, bsaunders! I was just asking around for directions but you took the time to slap down the entire code! I feel guilty now, lol. biggrin.gif Thanks man, really appreciate it! biggrin.gif
User is offlineProfile CardPM
+Quote Post

NickDMax

RE: How To Implement Simple "stop-word/skip-list" Function?

29 Mar, 2009 - 10:04 AM
Post #13

Can grep dead trees!
Group Icon

Joined: 18 Feb, 2007
Posts: 5,215



Thanked: 285 times
Dream Kudos: 1175
Expert In: Java/C++

My Contributions
lol, sscanf() should do the inner loop of my example without all the pointer work. Sorry I had been working in assembly language and was not really thinking about the tools available in C
User is offlineProfile CardPM
+Quote Post

bsaunders

RE: How To Implement Simple "stop-word/skip-list" Function?

29 Mar, 2009 - 10:07 AM
Post #14

D.I.C Addict
****

Joined: 18 Jan, 2009
Posts: 554



Thanked: 42 times
My Contributions
QUOTE(NickDMax @ 29 Mar, 2009 - 10:04 AM) *

lol, sscanf() should do the inner loop of my example without all the pointer work. Sorry I had been working in assembly language and was not really thinking about the tools available in C


Your code was very good from something off the top of the head. smile.gif
User is offlineProfile CardPM
+Quote Post

NickDMax

RE: How To Implement Simple "stop-word/skip-list" Function?

29 Mar, 2009 - 10:37 AM
Post #15

Can grep dead trees!
Group Icon

Joined: 18 Feb, 2007
Posts: 5,215



Thanked: 285 times
Dream Kudos: 1175
Expert In: Java/C++

My Contributions
I am sorry to say that I don't quite feel so warm and fuzzy about your example:

Just a few security/stability notes:
gets(line); -- gets really should not be used in general since it makes no attempt to ensure the input does not exceed the end of the buffer. Use fgets, or scanf instead (or roll your own).

printf(word); -- Never let a user control the format string of a printf! If the word was "%s%s" then a bunch of random memory would be output and it would more than likely crash the program. use printf("%s",word);


I realize that these probably don't make a differance in student code, but one might as well start out learning good habits rather than bad ones.
User is offlineProfile CardPM
+Quote Post

agent_logic

RE: How To Implement Simple "stop-word/skip-list" Function?

29 Mar, 2009 - 10:46 AM
Post #16

New D.I.C Head
*

Joined: 21 Jul, 2008
Posts: 29


My Contributions
Yeah, I use fgets, and a custom function (made especially for wise guys who like to break programs!) that "eats away" any extra input that may remain in the buffer. I don't care if it doesn't make a difference in student code, I like to follow good programming practices. biggrin.gif Thanks for the heads up, NickDMax!
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic

Time is now: 11/7/09 03:49PM

Live C++ Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month