Answered step by step
Verified Expert Solution
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 Finding a Character in a Code String
Please make sure you read the writeup for Task 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 stringlengthchar 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 The function should not modify string.
This stringlengthis actually a helper function that is useful in implementing two other helper functions, indexofchar and charofindex 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 indexofcharchar string int shiftleft, 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 wraparound shift to the left ie characters that are shifted out on the left appear on the right illustrated with an example below.
Original string
String shifted by
For this example, indexofcharABCDED returns the value remember indexes start at Basically, we have the input string ABCDE shift it by to the left, and in this shifted string CDEAB we need to find the index of character D which is
This function needs to work for both positive and negative integer values of the shift where a negative value of shiftleft 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; ie 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 charatindex below.
The second key helper function is:
char charatindexchar string int shiftleft, 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 charatindexABCDE returns the character A see the example above. In a way, charatindex is the dual of indexofchar You do not need to implement charatindex 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 indexofchar 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 wraparound feature
In the starter code, we have also already included one extra function that takes care of this wraparound incrementing, and which we call in our implementation of charatindex:
int incrementint index, int maxvalue, int incrementvalue
It takes in an index and how much to increment the index incrementvalue by as well as the maximum value of the index maxvalue to take care of the wraparound. It returns the incremented index value. For example, increment will return You also use it to decrement by providing a negative increment value. As mentioned above, we used this function in our implementation of charatindex It can be similarly useful when you implement indexofchar 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: stringlength and indexofchar 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!
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started