By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,433 Members | 1,470 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,433 IT Pros & Developers. It's quick & easy.

strings as inputs to a function

P: n/a
I am trying to write a basic spell check function, one which has as
its parameters two strings arrays, one is an article from a file
source which needs to be checked for valid words, in this case the
word needs to be at least 2 characters long with no intervening
punctuation, numbers or other non-letters.
This word then needs to be lower case and then compared to the
dictionary input array. the end result being the printing out of all
the misspelled words in the article. All of this needs to be done with
few or no pointers.
Any suggestions out there?
Nov 13 '05 #1
Share this Question
Share on Google+
12 Replies


P: n/a
David W. Thorell wrote:
I am trying to write a basic spell check function, one which has as
its parameters two strings arrays, one is an article from a file
source which needs to be checked for valid words, in this case the
word needs to be at least 2 characters long with no intervening
punctuation, numbers or other non-letters.
This word then needs to be lower case and then compared to the
dictionary input array. the end result being the printing out of all
the misspelled words in the article. All of this needs to be done with
few or no pointers.
Why?
Any suggestions out there?


Some functions you'll need are tolower(), fgets(), strcmp(), printf().
Try to write the code (it's pretty simple) and repost if you run into
problems.

HTH,
--ag

Nov 13 '05 #2

P: n/a
"David W. Thorell" wrote:

[snip]
This word then needs to be lower case and then compared to the
dictionary input array. the end result being the printing out of all
the misspelled words in the article. All of this needs to be done with
few or no pointers.
Any suggestions out there?


Use a hash table.

Also, what is your definition of "few" pointers? And do you have a plan
for prefixed and suffixed words (un-, re-, -s, -ing, -ed, etc)?

/david

--
Andre, a simple peasant, had only one thing on his mind as he crept
along the East wall: 'Andre, creep... Andre, creep... Andre, creep.'
-- unknown
Nov 13 '05 #3

P: n/a

"David W. Thorell" <dt******@hotmail.com> wrote in message
I am trying to write a basic spell check function, one which has as
its parameters two strings arrays, one is an article from a file
source which needs to be checked for valid words, in this case the
word needs to be at least 2 characters long with no intervening
punctuation, numbers or other non-letters.
This word then needs to be lower case and then compared to the
dictionary input array. the end result being the printing out of all
the misspelled words in the article. All of this needs to be done with
few or no pointers.
Any suggestions out there?

I don't understand the "few or no pointers" restriction. The C language is
built around pointers (in C++ there is a move to get away from using
pointers and towards higher-level substitutes, but this is comp.lang.c).

You need a function

int spellcheck(char **dictionary, int N, char *word)

Assume the dictionary contains all allowed words in alphabetical order.

The lazy way is simply to step right through the array using strcmp(). If
you find a match, return 1, if you get to the end, return 0.

However we can speed things up a lot by doing a binary search. You maintain
three numbers, low, high, and middle. At the beginning low is the first word
in the dictionary, high is the last word, and middle is the middle word. Say
your word is alphabetically after middle. Then low becomes middle, high
stays the same, and middle becomes the new mid point, that is, the three
quarters mark.
You repeat this technique until either you find a match or low and high
converge to the same value, in which case you know the word isn't in the
dictionary.
Nov 13 '05 #4

P: n/a
David Rubin wrote:

Malcolm wrote:

[snip]
Assume the dictionary contains
all allowed words in alphabetical order.

The lazy way is simply to step right
through the array using strcmp().
If you find a match, return 1, if you get to the end, return 0.

However we can speed things up a lot by doing a binary search.
You maintain

[snip - how to do a binary search]

Or you use bsearch (stdlib.h).


bsearch, requires an array of objects.
I'm thinking that the dictionary is more like a sequence of strings.

--
pete
Nov 13 '05 #5

P: n/a
pete wrote:
David Rubin wrote:
<snip>
Or you use bsearch (stdlib.h).


bsearch, requires an array of objects.
I'm thinking that the dictionary is more like a sequence of strings.


<shrug> array of char *

#include <string.h>

int compare(const void *vs1, const void *vs2)
{
char * const * s1 = vs1;
char * const * s2 = vs2;
return strcmp(*s1, *s2);
}

--
Richard Heathfield : bi****@eton.powernet.co.uk
CLINT v1.0b - now with stretchy strings!
Nov 13 '05 #6

P: n/a
Richard Heathfield wrote:

pete wrote:
David Rubin wrote:
<snip>
Or you use bsearch (stdlib.h).


bsearch, requires an array of objects.
I'm thinking that the dictionary is more like a sequence of strings.


<shrug> array of char *

#include <string.h>

int compare(const void *vs1, const void *vs2)
{
char * const * s1 = vs1;
char * const * s2 = vs2;
return strcmp(*s1, *s2);
}


What value are you going to give to the "size" parameter
when you call bsearch?

void *bsearch(const void *key, const void *base,
size_t nmemb, size_t size,
int (*compar)(const void *, const void *));

--
pete
Nov 13 '05 #7

P: n/a
pete wrote:
Richard Heathfield wrote:

pete wrote:
> David Rubin wrote:
>>

<snip>
>>
>> Or you use bsearch (stdlib.h).
>
> bsearch, requires an array of objects.
> I'm thinking that the dictionary is more like a sequence of strings.


<shrug> array of char *

#include <string.h>

int compare(const void *vs1, const void *vs2)
{
char * const * s1 = vs1;
char * const * s2 = vs2;
return strcmp(*s1, *s2);
}


What value are you going to give to the "size" parameter
when you call bsearch?

void *bsearch(const void *key, const void *base,
size_t nmemb, size_t size,
int (*compar)(const void *, const void *));


Here, have a complete, compiled, tested program:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int compare(const void *vs1, const void *vs2)
{
char * const * s1 = vs1;
char * const * s2 = vs2;
return strcmp(*s1, *s2);
}

int main(void)
{
char *array[] =
{
"now", "is", "the", "time", "for", "all",
"good", "men", "to", "go", "to", "the", "party"
};
char *key = "party";
char **result = NULL;

qsort(array,
sizeof array / sizeof array[0],
sizeof array[0],
compare);

result = bsearch(&key,
array,
sizeof array / sizeof array[0],
sizeof array[0],
compare);

if(result != NULL)
{
printf("%s\n", *result);
}
return 0;
}
--
Richard Heathfield : bi****@eton.powernet.co.uk
http://www.rjgh.co.uk - stretchy string support
in CLINT v1.0b - the library with no name.
Nov 13 '05 #8

P: n/a
Richard Heathfield wrote:
Here, have a complete, compiled, tested program: int main(void)
{
char *array[] =
{
"now", "is", "the", "time", "for", "all",
"good", "men", "to", "go", "to", "the", "party"
};


It's too pointy.

"I am trying to write a basic spell check function, one which has as
its parameters two strings arrays, one is an article from a file
source which needs to be checked for valid words, in this case the
word needs to be at least 2 characters long with no intervening
punctuation, numbers or other non-letters.
This word then needs to be lower case and then compared to the
dictionary input array. the end result being the printing out of all
the misspelled words in the article. All of this needs to be done with
few or no pointers."

The impresion that I get, is that the input file and the dictionary
are both just supposed to be text files, not arrays of pointers.

--
pete
Nov 13 '05 #9

P: n/a
pete wrote:

Richard Heathfield wrote:
Here, have a complete, compiled, tested program:

int main(void)
{
char *array[] =
{
"now", "is", "the", "time", "for", "all",
"good", "men", "to", "go", "to", "the", "party"
};


It's too pointy.

"I am trying to write a basic spell check function, one which has as
its parameters two strings arrays, one is an article from a file
source which needs to be checked for valid words, in this case the
word needs to be at least 2 characters long with no intervening
punctuation, numbers or other non-letters.
This word then needs to be lower case and then compared to the
dictionary input array. the end result being the printing out of all
the misspelled words in the article.
All of this needs to be done with few or no pointers."

The impresion that I get, is that the input file and the dictionary
are both just supposed to be text files, not arrays of pointers.


/* BEGIN spell.c */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#define DICTIONARY "now is the time for all good men "\
"to come to the aid of the party"
#define TEXT "Party pooka Aid Two"

void spell_check(char *, char *);

int main(void)
{
char *dictionary;
char *key;

dictionary = DICTIONARY;
key = TEXT;
spell_check(key, dictionary);
return 0;
}

void spell_check(char *key, char* dictionary)
{
char *copy;
size_t k_byte;
size_t k_size;
size_t length;

k_size = 1 + strlen(key);
copy = malloc(k_size);
if (!copy) {
fputs("Forget about it\n", stderr);
exit(EXIT_FAILURE);
}
for (k_byte = 0; key[k_byte]; ++k_byte) {
copy[k_byte] = (char)(isalpha(key[k_byte])
? tolower(key[k_byte]) : '\0');
}
copy[k_byte] = '\0';
for (k_byte = 0; k_size; k_size -= length) {
if (!strstr(dictionary, copy + k_byte)) {
puts(copy + k_byte);
}
length = strlen(copy + k_byte) + 1;
k_byte += length;
}
free(copy);
}

/* END spell.c */

--
pete
Nov 13 '05 #10

P: n/a
On Tue, 08 Jul 2003 22:42:47 -0400, in comp.lang.c , pete
<pf*****@mindspring.com> wrote:
All of this needs to be done with
few or no pointers."
thats a pretty much impossible assignment, if you're using strings. I
guess you could write it all in one function, but as soon as you pass
your string to another function, *presto*, you have a pointer/
The impresion that I get, is that the input file and the dictionary
are both just supposed to be text files, not arrays of pointers.


A text file is an array of pointers to chars, written to disk with
intervening whitespace.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 13 '05 #11

P: n/a
Mark McIntyre wrote:

On Tue, 08 Jul 2003 22:42:47 -0400, in comp.lang.c , pete
<pf*****@mindspring.com> wrote:
All of this needs to be done with
few or no pointers."
thats a pretty much impossible assignment, if you're using strings.


Why is it pretty much impossible?
I posted a solution over five hours ago.
I guess you could write it all in one function,
but as soon as you pass
your string to another function, *presto*, you have a pointer/


Between the parameters and the automatic variables,
the function has only three objects which are pointers.
I believe that satisfies the
"All of this needs to be done with few or no pointers."
specification.

Do you disaprove of my posted solution or have you not seen it?

--
pete
Nov 13 '05 #12

P: n/a

"pete" <pf*****@mindspring.com> wrote in message

Between the parameters and the automatic variables,
the function has only three objects which are pointers.
I believe that satisfies the
"All of this needs to be done with few or no pointers."
specification.
The problem is that we don't understand what your tutor is getting at. The C
language is built around pointers, and C programmers use them all the time
without thinking. Writing a C program using "few or no pointers" is a bit
like trying to write a pop song using "few or no electric guitars" - it can
be done but the result is very strange, and it seems a pointless exercise.

If your dictionary is small you can get away by passing the whole thing in
as one string and using strstr, however this isn't a natural way to approach
the problem.

You also of course need a function to extract words from your text file and
clean them up, getting rid of the punctuation.
Do you disaprove of my posted solution or have you not seen it?

I haven't tested it. The main problem is that it won't scale to a real
application.
Nov 13 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.