Prerequisites



C: Strings and Functions PrerequisitesWrite a function.Pass strings (arrays of char) to a function and return a string from a function.Use pointers.Problem Strings in C are implemented as arrays of characters with a null character marking the end of the string. The string.h header file is used to include a library of useful string functions. We are going to write a few of these string functions from scratch. Write the following string functions:length(s)Accepts a string and returns the length. The length does not include the null pare(s1, s2)Accepts two strings and returns one of the following: (1) if s1 is less than s2, return a negative number, (2) if s1 is equal to s2, return 0, and (3) if s1 is greater than s2, return a positive number. Two strings are equal if they are both the same length and all corresponding characters are equal. A string s1 is less than string s2 if, in the first pair of characters that are not equal, the character in s1 is less than the character in s2. And a string s1 is greater than string s2 if, in the first pair of characters that are not equal, the character in s1 is greater than the character in s2.If two strings are equal up to the end of one string, the shorter string is considered less than the longer string. Note that when you get to the end of the shorter string, you will be comparing a null character with a non-null character, and the non-null character will always be greater than the null character.Examples:Compare "Bob" with "Bobby". The ASCII codes for each character are:Index:012345Bob:66111980Bobby:6611198981210The comparison would be: 66==66, 111==111, 98==98, 0<98, so "Bob" is less than "Bobby". If the comparison was compare("Bob", "Bobby"), a negative number should be returned. If the function call was compare("Bobby", "Bob"), a positive number should be returned.substring(str, start, length)Accepts a string (array of char), a starting position, and the number of characters to extract. The function will create a new string and return a substring of s1 beginning with the character at position start, and a length of length. There are some possible invalid arguments that could be passed to substring:The length is too big and takes you beyond the end of str. In this case, return everything up to the end of str.The length is 0 or negative. In this case, return an empty string.The starting position is a negative value. In this case return an empty string.The starting position is beyond the end of the string. In this case, return an empty string.Below is my main program:int main(){printf("Length of 'Hello!' (should be 6): %d\n", length("Hello!"));printf("Length of '' (should be 0): %d\n", length(""));printf("\n");printf(" \"a\"==\"a\" should be 0: %d\n", compare("a", "a"));printf(" \"a\"==\"b\" should be negative: %d\n", compare("a", "b"));printf(" \"b\"==\"a\" should be positive: %d\n", compare("b", "a"));printf(" \"Bob\"==\"Bobby\" should be negative: %d\n", compare("Bob", "Bobby"));printf(" \"Bobby\"==\"Bob\" should be positive: %d\n", compare("Bobby", "Bob"));printf("\n");printf("Should be 'world!': '%s'\n", substring("Hello, world!", 7,6));printf("Should be 'world!': '%s'\n", substring("Hello, world!", 7, 100));printf("Should be '': '%s'\n", substring("Hello, world!", 7, -1));printf("Should be '': '%s'\n", substring("Hello, world!", -1, 10));printf("Should be '': '%s'\n", substring("Hello, world!", 13, 1));}Below is the output for the above code:Length of 'Hello!' (should be 6): 6Length of '' (should be 0): 0 "a"=="a" should be 0: 0 "a"=="b" should be negative: -1 "b"=="a" should be positive: 1 "Bob"=="Bobby" should be negative: -98 "Bobby"=="Bob" should be positive: 98Should be 'world!': 'world!'Should be 'world!': 'world!'Should be '': ''Should be '': ''Should be '': '' ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download