strings as inputs to a function

  • Thread starter David W. Thorell
  • Start date
D

David W. Thorell

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?
 
A

Artie Gold

David said:
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
 
D

David Rubin

:

[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
 
M

Malcolm

David W. Thorell said:
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.
 
P

pete

David said:
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.
 
R

Richard Heathfield

pete said:
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);
}
 
P

pete

Richard said:
<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 *));
 
R

Richard Heathfield

pete said:
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;
}
 
P

pete

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.
 
P

pete

pete said:
Richard said:
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 */
 
M

Mark McIntyre

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.
 
P

pete

Mark said:
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?
 
M

Malcolm

pete said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top