473,385 Members | 1,610 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

strings as inputs to a function

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
12 2675
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
"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

"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
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
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
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
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
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
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
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
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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: mercuryprey | last post by:
Hi, I'm pretty new to Python, to programming overall...so how would I make something where the user inputs multiple words in a string - like "connect 123.123.123.123 21 user password" or similar,...
5
by: Dennieku | last post by:
Does anybody have an idea how to compare two strings? What I want to know is the percentage of the differences between the strings Thx, Dennieku
2
by: Chris Beach | last post by:
Hi there. I'd like to be able to group together inputs on an HTML form inside, say, a <div> or <fieldset> and then use javascript to iterate through the controls in the group and disable them....
6
by: chris | last post by:
im pretty ne to java script - so please be gentle :) basically what i want to do is construct an action in a function using a variable sent to that function ok - what i want to do is ...
7
by: David Jacques | last post by:
I have to reformat a string from the form "SRID=4269;POINT(-90.673 69.4310000006199)" to GeometryFromText('POINT (-141.095 68.5430000006417)',4269) ); I have a function to do this by...
3
by: Mars | last post by:
I don't know whether the inputs are all numbers or all strings... if they are numbers, they are sorted as numbers, if they are strings, they are sorted as strings.... e.g. input : 9 12 2...
9
by: fool | last post by:
Dear group, if I want to allocate a string with a help of malloc then, shall I use the following in a loop? char *s; s = malloc(strlen(s)+1); Any pointer reference is help full. I went through...
2
by: karafire2003 | last post by:
I've been tasked to do 2 questions. I think i got the majority of it done, but i'm having trouble. Question #1: Write a C program that accepts as input from the keyboard a floating point number, an...
9
by: agenkin | last post by:
Are there any Python libraries implementing measurement of similarity of two strings of Latin characters? I'm writing a script to guess-merge two tables based on people's names, which are not...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.