Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Task 1 - Finding a Character in a Code String Please make sure you read the writeup for Task 1 completely before you start writing

Task 1- Finding a Character in a Code String
Please make sure you read the writeup for Task 1 completely before you start writing code (as the later parts of the writeup provide crucial implementation information). The goal of this task is to create helper functions that will be extremely useful when implementing your encryption algorithms.
The first helper function you need to implement is:
int string_length(char string[])
It should return the length of a string. Remember, the length of the string is the number of symbols not including the null terminator. For example, the length of the string CATS is 4. The function should not modify string.
This string_length()is actually a helper function that is useful in implementing two other helper functions, index_of_char() and char_of_index(), which will be explained next. It is in fact these latter two functions that will be key building blocks of your encryption algorithms.
The first one is:
int index_of_char(char string[], int shift_left, char character)
It returns the position (index) at which you can find a particular character in a string after the string has been shifted. This shifting is a wrap-around shift to the left (i.e., characters that are shifted out on the left appear on the right), illustrated with an example below.
Original string
String shifted by 2
For this example, index_of_char(ABCDE,2,D) returns the value 1(remember indexes start at 0). Basically, we have the input string ABCDE, shift it by 2 to the left, and in this shifted string (CDEAB) we need to find the index of character D, which is 1.
This function needs to work for both positive and negative integer values of the shift (where a negative value of shift_left essentially means a shift to the right). The shift values can be any number (as long as it is an integer, since it is a parameter of type int). The function should not modify string. You may assume that the string is not empty and contains unique characters; i.e., no character is repeated. Also, you may assume that the character that you are finding the index for is present in the string and is present exactly once. Before you start implementing this function, please continue reading about char_at_index() below.
The second key helper function is:
char char_at_index(char string[], int shift_left, int index)
It looks at a specific position (index) in the string after the string has been shifted and returns that character at that position. This means that char_at_index(ABCDE,2,3) returns the character A, see the example above. In a way, char_at_index() is the dual of index_of_char(). You do not need to implement char_at_index(). We have already implemented it for you in the starter code. However, please have a look at our implementation, as it will be very helpful when you consider how to implement index_of_char(). Specifically, you will notice that we didnt actually need to shift all the symbols around. Instead the shift is basically something you should imagine being applied and it manifests itself as an incrementing or decrementing of the indexes (while taking into account the wrap-around feature).
In the starter code, we have also already included one extra function that takes care of this wrap-around incrementing, and which we call in our implementation of char_at_index():
int increment(int index, int max_value, int increment_value)
It takes in an index and how much to increment the index (increment_value) by, as well as the maximum value of the index (max_value) to take care of the wrap-around. It returns the incremented index value. For example, increment(3,4,2) will return 0. You also use it to decrement by providing a negative increment value. As mentioned above, we used this function in our implementation of char_at_index(). It can be similarly useful when you implement index_of_char(), using a similar idea of not doing a physical shifting of the string but renumbering the index instead.
Summary: For this task, you need to implement two functions: string_length() and index_of_char(). To get a better understanding of these functions, you can also run the associated demo programs, as explained in Creating Test Data. The functions are already included in the starter code (in ciphers.c); you just need to write the code that goes inside.
Please follow the format that is shown in the picture!
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2016 Riva Del Garda Italy September 19 23 2016 Proceedings Part 1 Lnai 9851

Authors: Paolo Frasconi ,Niels Landwehr ,Giuseppe Manco ,Jilles Vreeken

1st Edition

3319461273, 978-3319461274

More Books

Students also viewed these Databases questions