473,769 Members | 6,697 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2712
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******@hotma il.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.pow ernet.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.pow ernet.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(cha r *, char *);

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

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

void spell_check(cha r *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_FAILU RE);
}
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(dictio nary, 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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
1192
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, and then I can split this string up to pass these arguments to a function like ftp_connect(ip, port, user, pw) etc...? I have no idea how to "break" the string up so I can get these out of it.. thanks for answers,
5
3403
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
10615
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. Is this possible? The idea is that I can create a reusable function that will take two arguments - a checkbox object and a group object and when the checkbox is ticked, the function will disable all inputs in the group.
6
1427
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 function ($number) {
7
1619
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 taking the input string as char* and returning
3
1549
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 4 output : 2 4 9 12
9
2203
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 the K & R book's chapter 5. But I am not able to get the answer.
2
3966
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 integer, and a character. Each of these inputs should be preceded by a prompt and stored using individual variable names. Have your program call a function that assembles the input data into a single string. Display the assembled string using the...
9
2208
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 necessarily spelled the same way in both tables (especially the given names). I would like some function that would help me make the best guess. Many thanks in advance!
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9999
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9866
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8876
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5310
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3967
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3570
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.