finding sequnce of letters increasing by one in a given string - c

I got a question on my HW that is finding sequences of letters increasing by one and replacing them by the character "-".
the first letter and last letter of the sequence wont be replaced only the letters between them.
For example: the input string "abcdhswxyz" will be changed to: "a-dhsw-z".
As u can see, the first sequence in the string is abcd and it has changed to a-d and the second increasing sequence is wxyz and it has changed to "w-z".
Still havent got an idea.

Related

Having hard time understanding how to us void printSp(int) in this given task,

I only really understand how to do equation in program I need help on how to approach this task, also I not really sure whats its asking of me to do.
Suppose you are given a function with the following declaration:
void printSp(int); /* prints specified number of spaces */
Write a function named printTri that takes a single argument, a character, and returns an integer value. If the character is not a capital letter (between 'A' and 'Z'), then the function simply returns 0. Otherwise, if it is a capital letter, the function will print a triangle of characters that looks like this: A ABA ABCBA ABCDCBA
NOTE: WIth a fixed-width font, the center letter in each row would line up. Write this out for yourself on paper, to figure out how many spaces should be printed on each line before the characters start. The bottom line has zero spaces. How many spaces should the top line have? The letter passed in becomes the highest letter in the triangle. For example, to print the triangle above, the caller passes in 'D'. After printing, the function returns the total number of non-space characters printed. For example, for the example triangle above, the function must return 16. You must call the printSp function, once per line, as part of your solution. (NOTE: Call printSp for every line, even when the number of spaces to print is zero.) History:
This is what I have so far I know its not much but its all I understand how to do.
if (x >='A' && x <= 'Z') printf(" A\n ABA\n ABCBA\nABCDCBA")
else return 0;
The function printSp() prints spaces.
You currently are outputting a hard coded number of spaces instead of using printSp(). Swap printf(" ") for printSp(3)
Reading the question, they want you to output a variable number of rows based on the letter provided. So for D you print the pattern you have hard coded which contains four rows and enough letters to output the D. For E you add another row.
I generally suggest students approach problems like this by starting with hardcoding, as you have. Ensure that you incorporate the required features, like printSP(). Then make the code more generic to handle other inputs.

How do I unscramble a scrambled word?

Supposed I have an array of char {A,B,C,D,E,F} and the order number is 3
Then, the scrambled word is as follow :
The first 3rd (order number) character, C is removed and saved.
{A,B,D,E,F} , {C}
Start from the following character which is D, and the first third character would be F. So, F is removed and saved.
{A,B,D,E} , {C,F}
Start from the following character (since it reached end of array index, we go back to the beginning). So start from character A, and the first third character is D.
{A,B,E} , {C,F,D}
and so on.
The resulting will be an empty original char array, and the scrambled array
{ } , {C,F,D,B,E,A}
The above algorithm can be easily implemented. I have no problems with that. What I do have problem with is unscrambling it. I am given some unscrambled char array, and its ordering, and I have to find its original char array.
I have been trying for hours now and I can't seem to find a "formula" for it. I'm guessing I'm missing something crucial. Can anyone give me a clue or hint on how to approach this problem?
Make "map string" the same length as your original, but with all the letters in sequence, like "ABCDEFGH.."
Scramble it according to the given ordering
Now, iterate through your problem string and the map string at the same time. For each character in the problem string, the map string character will say where to put it. If you have map="SQEG..." and problem="DSEG...", then 'D' goes in the 'S' position, 'S' goes in the 'Q' position, etc.
That's it. If you prefer, or if your problem string is too long, use an array of integers instead of a string for the map.

Find Duplicate characters in String ( Solution must be less than O(n^2)

If we traverse the string array for every character and compare with all others,we would find duplicates,but that is O(n^2)
I need some idea to do it in less than O(n^2)
Lets say input string is: nice book, then
output will be: o
Iterate over all characters and store them into a HashMap with key is the character itself and value is nay thing (the character, true, integer,..). Before adding the character to the hash map check if it already exists, if exists then it is a duplicate if not insert it
Here is a pesudo code
for character char in String
if charactersMap.get(char) == null
charactersMap.put(char, char)
else
print C
This solution is O(n) as it iterates over the characters once and looking up for a key into a map takes constant time.

Search the longest substring in two strings

The task is this: Find the longest substring found in two lines. The peculiarity of the problem is that these lines are very long (contents of the file, that is to 400,000 characters each), and the alphabet from which they are composed of short - 4 characters.
Strings can be of different length.
I invented and implemented the following algorithm:
To get the contents of the first file and write to a string str1, removing the line breaks
To get the contents of the second file and write to a string str2, removing the line breaks
We shall consider all substrings the string str1, from the longest to the shortest. To do this, define the cycle while (i>0), at each iteration, which after the main content decreases the length of the string by one. And so to the strings of length 1.
Inside the while loop: All substring of length N differ only in the beginning position.
Let have a string of length N:
It is one substring of length N, which contains, starting at position 0.
There are two substring of length N-1 that start inside positions 0 and 1
In it for three substring of length N-2, which starts inside positions 0, 1, and 2
...
K+1 substring of length N-K, which start from the position 0,1,...,K
The starting position of the count in the for loop(z=0; z<=g-i; z++), within which the function is called getSubstring receiving the substring. And then running the standard function strstr with this substring of a string str2
But does this algorithm long enough. Is there no way to make it faster?
P.S. Write in C
There are at least two classical options to solve longest common substring efficiently
Build a generalized suffix array or suffix tree of the two strings. One can show that the LCS is a prefix of two adjacent suffixes in the suffix array that have different colors (belong to the different strings). I once wrote an answer that describes a simple O(n log n) suffix array construction algorithm
Build a suffix automaton of one string and feed the other string into it. At every point check how "deep" you are in the automaton and report the maximum over all those depths. You can find a C++ implementation in my GitHub.

How do I load a string of numbers seperated by space to an array?

I need to create a program where in the first like of input I would put the number of numbers in the string separated by space, and in the second line I would enter them (for example 1 2 3 4 5 6). So I tried using Val, but it can't help me because there are spaces, also I can't use for because the numbers are in one line. Also numbers don't have to be one figure, they are from 1 to 10^9 .
This is the principle: Let s be your string. In a loop to the following
Remove leading spaces in s
while (length(s)>0) and (s[1]=' ') do delete(s,1,1);
Find the first space in the remaining string
p := pos(' ',s);
Copy the non-space part leading in a second string t and remove that part from s
t := copy(s,1,p-1); delete(s,1,p);
get the next array element element[i] with
val(t,element[i],code);
Of course you have to code checks (Is the string s empty after some manipulation? If val gives an error, you have no valid number,...), array indexing etc.

Resources