What's Here?
- Members: 340,098
- Replies: 920,353
- Topics: 154,917
- Snippets: 4,854
- Tutorials: 1,257
- Total Online: 4,831
- Members: 137
- Guests: 4,694
|
Welcome to Dream.In.Code |
|
|
Become an Expert!
Join 340,098 Programmers for FREE! Get instant access to thousands  of experts, tutorials, code snippets, and more! There are 4,831 people online right now. Registration is fast and FREE... Join Now!
Chat LIVE With a Expert
|
How to implement simple "stop-word/skip-list" function?
How to implement simple "stop-word/skip-list" function?
Rate Topic:
   
Posted 28 March 2009 - 08:36 AM
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!  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.  Any ideas would be greatly appreciated!
Thanks in advance.
This post has been edited by agent_logic: 28 March 2009 - 08:36 AM

- apt-get install DIC.bin
-
-
Group:
Admins
-
Posts:
16,211
-
Joined:
26-July 07
Dream Kudos: 12400
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net, jQuery
Posted 28 March 2009 - 08:43 AM
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.
Posted 28 March 2009 - 08:52 AM
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.
Posted 28 March 2009 - 09:59 AM
Can I ask what you mean by "skip" a word?
Posted 28 March 2009 - 06:08 PM
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.
Posted 28 March 2009 - 06:18 PM
Do you know how to read a string from standard input?
Posted 28 March 2009 - 06:23 PM
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 March 2009 - 06:26 PM
Posted 28 March 2009 - 06:29 PM
You're using C++ and not C, right?
Posted 28 March 2009 - 06:49 PM
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.
!!! 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.
Posted 28 March 2009 - 07:14 PM
@bsaunders: No mate, I'm using C.
@ NickDMax: Thanks for the logic mate, I have something to work towards now!  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.
Posted 28 March 2009 - 07:55 PM
NickDMax's solution should work. The only way I can think of in C is:
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 March 2009 - 08:04 PM
Posted 29 March 2009 - 09:55 AM
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.  Thanks man, really appreciate it!
Posted 29 March 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
Posted 29 March 2009 - 10:07 AM
NickDMax, on 29 Mar, 2009 - 10:04 AM, said:
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.
Posted 29 March 2009 - 10:37 AM
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.
Posted 29 March 2009 - 10:46 AM
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.  Thanks for the heads up, NickDMax!
1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users
|
Be Social
Programming
Web Development
Reference Sheets
Bye Bye Ads
Monthly Drawing
Top Contributors
Top 10 Kudos This Month
|