How to implement word spell checking with c? [closed] - c

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I need to create spell checker in c for my assessment. I manage to start the work - I have text file which content all the dictionary, I code how to read the files, how to compare chosen file with the dictionary file and now I need to print out to new text file where the misspelled words been found, what they are, and their correct version. The big problem is, that I have no idea how to do this. My code right now can say that there is a difference between the files. But I don't know how make strcmp check string by string, word by word, if something is wrong.
In dictionary files are all the words, so of course, if my program is reading other file, compare, then writes all the words which aren't in the file to the new output file with errors, these output-error words will be also just random words, which aren't even in the text file, or connected with the text file.
I hope I explained my problem well and there is somebody who would tell me how to fix this problem. I don't even ask for the code, I just need some idea how I would need to code the rest of the program. And sorry, for my English, it's my second language, so I still make grammatical mistakes.

Here are some steps you can follow:
read the dictionary into a memory structure, for example an array of strings, which you will sort in lexicographical order (with strcmp).
read the file line by line, and for each line iterate these steps:
initialize a highlight line with spaces, same length as the line read.
skip characters that cannot be part of a word with strcspn(), save the index i.
scan the characters that can be part of a word with strspn() save this number n.
if n is 0, this is the end of line
look up the word at index i with n chars in the dictionary (potentially ignoring the case)
if the word cannot be found, set the corresponding characters in the warning line with ^ characters.
update the index i += n and iterate.
if at least one word was not found in the line, output the line and the warning line.
Study these standard functions:
strspn()
strcspn()
qsort()
bsearch()

Related

Find the number of characters from the first character of nth occurrence of a character in a text file without using an array and strchr function? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Fore example lets assume the following is a text in a file:
"I want to find number of characters from the first character is the nth occurrence of a character in a text file, but, I want to do this without declaring an array, storing the text file in it and applying the strchr function."
Lets say I want to find the position of the second new line character in the text? How many number of characters is the new line from the first character in the text? For e.g. The first occurrence character 't' is the 6th character of the text file.
If it is possible can someone please explain how? If no, can someone please explain why?
That can be done in an operating system that supports memory mapped files. This includes Windows and POSIX operating systems such as Linux.
In POSIX it is done using mmap(); there is example code in the documentation.
For Windows there is an API for for memory mapped files. Again, sample code is included in the documentation.

Find shortest prefix in a string in C [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I am new to C and would appreciate your help in a code.
I need to allocate dynamically an array and check which prefix in the string is the shortest that it's concatenation builds the string, and print it and it's length.
Here are a few examples for the outputs:
for "ababab", the output should be: "ab" of length 2
for "aaaa", the output should be: "a" of length 1
for "abcaabca", the output should be: "abca" of length 4
for "abcdefg", the output should be: "abcdefg" of length 7
for "acacaac" the output should be "acacaac" of length 7
My problem is that I don't know how to build the function that supposed to to it. The cases in which the string contains only the same letter, or all letters are different from each other are fine, but I don't know how to take care of all the other cases.
I can't use another string for this code, but I'm allowed to use other pointers
to help me.
Thanks
I will not write the effective code, but the idea itself.
First, this "prefix" of yours has a length that is a divisor of the big length. So if the length i of the prefix doesn't divide the length n of the string then it should be skipped.
To check if a length i is valid, then you need to compare the characters from positions j and j+i, for all possible values of j.
It can be pretty simple, two nested loops, the outer one being a for, from 1 to length/2, and the inner a while, using a pointer increased in each step by the value of the iteration variable of the outer loop. Inside the loop you can use strncmp() to compare the a proper substring, and break the loop if comparison fails. If all comparisons are OK, you have found the "shortest prefix", so you can break the (for) loop. The for loop can be optimized too, by skipping the lengths that are not divisors of the input string length.
This doesn't require making a copy or copies.
And yes, I will leave you actually write the code.
You said you cannot use another string, hopefully You are allowed to use a small temporary array of booleans to count the number of different characters in the string.
1- Find the number of different characters in the string, using an array of boolean. Let this number be x.
2- check if the first x characters constitute a prefix. (check each pointer position p against p+x, p+2x, etc). If unsuccessful, the shortest prefix is the whole string.
In between, you can make it faster by quickly checking if x divides the length of the string..

C: How to read variables from a file [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
What is the best way to create a function that reads each part of a file like described in the following picture and saves it to arrays and integers, it must read and save the second part (above word_count): word; orientation; row; col; points; jogador until a number(Turn) is read.
Start by creating 3 struct's. One for people. One for words and one to aggregate the full structure. For the third, you will need to decide which arrays can be sized at compile time and which need to use malloc or calloc to allocate space for the people or word structures.
Next write a function to populate a person from a one line string and one to populate a word from a one line string. You could use strchr to find the semicolons or for less error durability you might look at sscanf.
Finally write your loading function to read the file line by line detecting 'mode' changes by (strchr(line, ';') == -1), and calling the appropriate convert function. You can then return the aggregate structure as a pointer to a malloc'ed struct.
Don't forget to write a function, that takes that pointer, to dispose of everything you malloc'ed so that a caller does not need to know your allocation details and can just say "get me one from that file" followed by "throw this away".
Unfortunately, C is unlike Java or C# in that the heavy lifting is not built in or covered by copious included libraries. You need to find libraries or write low level code yourself.
Good luck with you project.

Converting assembly code to machine code in C manually [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I've been given a practise homework to do from college which requires me to manually convert assembly code to machine code. I will display the images of what the before and after looks like. It basically requires my to remove the comments (after semi colon) and white spaces after the instruction. I found this to be puzzling since im a C noob and ive been thrown into assembly code. I am planning to do this in notepad++. The assembly language im using is meant to be very simplified since x86 would be ridiculous to start off on. Any help would be appreciated!
As stated in the comment, this has nothing to do with machine code, it's just trivial string manipulation (essentially, it's sed 's/[[:space:]]*\(;.*\)\?$//'):
Read a line
Walk the string one character at time; each time you find a non-whitespace character, write down its position in a variable.
When you find either the end of the string (character 0) or a semicolon (character ';'), truncate the string to the character following the last non-whitespace one you found (effectively, set it to zero).
Write down the updated string to the other file.
Repeat until the input file is finished.

In what format should I write a vocabulary with synonyms into a file? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Iv'e been given a mission on my homework on c language - file I/O. The programm should accept two files, one of them should be a text(*.txt) with a sentence in it, and the other file should contain some words, and for each word it will contain unknown number of synonym.The programm should write a new sentence to the text file, while replacing all of the posiible words with a synonym word from the vocabulary file. What, in your opinion, will be the best format to the vocabulary file(txt or other)? in what way should I insert the data(if it's a txt, should it like a word and then on the same line all of the synonym?)? and how do I need to handle with the file in the program?
"The program should write a new sentence to the text file, while replacing all of the possible words with a synonym word from the vocabulary file... What, in your opinion, will be the best format to the vocabulary file(txt or other)?"
Format of the file doesn't really matter that much. Your program will most likely load its contents to the memory (some data structure) and what really matters is how you're going to use these synonyms afterwards.
"...should it like a word and then on the same line all of the synonym?"
That sounds reasonable if your program will load it with push approach, i.e. process the file line by line. You will most likely need a data structure to allow you retrieve list of synonyms when given a word, thus:
word1 synonym1 synonym2 ... synonymN
word2 synonym1 synonym2 ... synonymN
...
wordM synonym1 synonym2 ... synonymN
will do just fine. Now go and try to write some codes :)

Resources