Find shortest prefix in a string in 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 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..

Related

Comparing a char to the char '\"' [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I'm working on C and need to recieve a string from the user in the format of "abcd", and to diagnose it to retrieve it as a string of "abcd"(int the code).
For some reason when I try to check if the first char in the string (that I've read using sscanf) is " it doesn't return it is, as you can see in the picture below. The watch says that data[0] is '"', but that data[0] == '"' is false, which is absurd.
The character in data[0] is probably a special quotation mark with the ASCII (or rather Windows-1252) code 147/0x93. It is a number in which the highest bit is 1, and as such is outside the 7 bit ASCII range. While the 7 bit ASCII codes are interpreted identically across many character sets this is not so for 8 bit values (> 127). The "glyph" a given terminal or printer will show for 8 bit values depends on the char set is assumes (in your case, as mentioned, Windows-1252).
Last not least, because on your system chars are signed the debugger interprets the highest bit as a minus sign and shows a negative value. I think you can cast it in the debugger watch expression to unsigned char to obtain the positive equivalent.
That character cannot be entered directly with the keyboard; on Windows you can try to use the Alt+Number block trick. When you enter the normal quotation mark you create a char with the ASCII code 34/0x22, which the compiler and debugger correctly claim is not identical.

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.

How to implement word spell checking with 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 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()

Program that count all characters appearing consecutively [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
Problem: Character count
INPUT: A word and count all characters appearing consecutively.
OUTPUT: The number of characters appearing consecutively and the letters
Sample Run:
Enter string: Mississippi
3
ssp
Enter string: Committee
3
mte
Enter string: mmmmrrnzzz
4
mmrz
Im a first year college student taking Bachelor of Science in Computer Science. Im hoping you can help me for this problem Thank you :)
There are many ways to solve this problem, but probably the best way for a first year student is to use a loop to solve it.
I don't want to give to many details as that defeats the purpose of the exercise, but the basic process might look something like this:
Treat the string as an array of characters. So the word committee is really c o m m i t t e e. Create a variable to hold the max repeat count, current repeat count and repeat letters. Also create a variable for the current letter and initialize it to "".
for each letter in the input string
does this letter match the value in current_letter?
if yes
increment the current_repeat_count variable
append the current_letter to the repeat_letters array
if current_repeat_count > max_repeat_count
max_repeat_count = current_repeat_count
else
current_letter = the letter you just tested
current_repeat_count = 0
I hope that gets you started.

Printing range of number upward or backward according to users first input [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
I'm trying to print a range of integers (all the positive integers that to be found between the user's 2 input integers) and I need to do it by the order the user typed it.
for example:
for the input: 4,9 output would be: 4 5 6 7 8 9
and for the input: 9,4 the output would be: 9 8 7 6 5 4
I'm not allowed to use any array/strings/functions, just basic C commands.
Anybody have any ideas?
Since this is most likely a learning exercise, here are some points to think about:
You need a loop.
The loop starts at the first number the user enters, and ends upon reaching the second number
The step of the loop depends on the order of the numbers entered by the user
If the first number is smaller than the second one, the step of the loop is one
If the first number is larger than the second one, the step of the loop is negative one
Therefore, the structure of your program is going to be as follows:
Prompt the user for the two numbers
Read numbers one and two
Compute the step using an if
Print out the range using a loop (for, while, or do/while, it's up to you).
This should be enough to complete your assignment. Good luck!

Resources