Question
Question: Write a C program that contains two functions, one to input two nonempty strings and the other to compare them. The comparison returns an
Question: Write a C program that contains two functions, one to input two nonempty strings and the other to compare them. The comparison returns an integer that indicates the first position (array index) where the two strings differ.
Implementation
The program to be submitted is named lab4a . c. Use the given template lab4a. c Below and fill in your code.
The first function to be implemented is mystrInput (). See file lab4a . c for its specification. Use getchar and a loop to read a line of characters, and store the input characters into the array. The loop terminates when a new line character ' is entered. The new line character ' is NOT part of the line (i.e., discard the new line character ' ).
The second function to be implemented is myStrCmp (). See file lab4a . c for its specification. The function returns an integer that indicates the first position (array index) where the two strings
differ. Consider the following two special cases:
-- Two strings are equal. In that case the return value is -1.
-- One string is a substring of the other (e.g., CSE2031 and CSE2031E3.0). In that case, the return value is the length of the shorter string (i.e., the index of the null character in the shorter string).
In both functions, do not use array indexing such as s [i] . Use only pointers and address arithmetic to manipulate the array elements. If you use array indexing in your code, your program will not be marked and given zero point. Hint: Write and test your program using array indexing first, then translate array accesses to pointers.
Do not modify the function definitions in file lab4a . c. ( BELOW )
------------------------------------------------------------------------------------------------------
Sample Inputs/Outputs
EECSZO31
indigo 51 % a.out This is CSE2031. CSE2031E3.0
0 indigo 54 % a.out Ill go now.
I will go soon.
1
indigo 49 % a.out This is 2031. This is 2011.
10
indigo 50 % a.out abc
abc
1
indigo 52 % a.out CSE2031 CSE2031E3.0
7
indigo 53 % a.out
It is going to snow tomorrow. It is going to rain tonight.
15
-------------------------------------------------------------------------------------------------------------
lab4a . c.
#include #define MAX_SIZE 500 void myStrInput ( char *s ); int myStrCmp( char *s1, char *s2 ); main() { char strg1[ MAX_SIZE ], strg2[ MAX_SIZE ]; /* Input strings strg1 and strg2. */ /* Assume that the length of each input string is less than 100 characters. */ myStrInput( strg1 ); myStrInput( strg2 ); printf( "%d ", myStrCmp( strg1, strg2 )); } /************* DO NOT MODIFY ANYTHING ABOVE THIS LINE, *************/ /************* EXCEPT THE HEADER CONTAINING YOUR INFO **************/ /* Function myStrInput Input: an array of char pointed to by pointer s. Output: the same array that stores the user's input string. Note: The length of each input string is less than the array size. So no error checking for the string length is required. Do not modify the function definition. */ void myStrInput ( char *s ) { /* Add your code here. */ /* Do not use array indexing. */ /* You may define additional variables, except arrays. */ /* Do not use any C library functions, except getchar(). */ } /* Function myStrCmp Input: two strings pointed to by pointers s1 and s2. Output: returns the first position (array index) where the two strings differ. Special cases: 1. Two strings are equal: returns -1. 2. One string is a substring of the other (e.g., "CSE2031" and "CSE2031E3.0"): returns the length of the shorter string. Do not modify the function definition. */ int myStrCmp( char *s1, char *s2 ) { /* Add your code here. */ /* Do not use array indexing. */ /* You may define additional variables, except arrays. */ /* Do not use any C library functions. */ }
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