Using Python write a string-comparison function named strNcompare that compares up to n characters of a string s1 with the other string s2. The function must return 0, -1, or 1 if the first n-character portion of s1 is equal to, less than, or greater than the corresponding first n-character portion of s2, respectively. (1) You must use the following function definition: def strNcompare(s1, s2, n): (2) String comparison must be case-insensitive. For example, the strNcompare function must return 0 for the s1 value of ‘tommy’ and the s2 value of ‘TOMMY’. (3) You should not use a loop such as the while loop and the for loop inside the strNcompare function. Then write a Python program that receives two words and the number of characters for comparison, calls the strNcompare function, and then shows the result. Your program keeps doing this until the user enters the value of -1. Your program displays the message ‘String comparison [1(play), -1(quit)]: ’ and receives the value 1 or -1 from the user. (2) If the user enters the value -1, your program finishes immediately. (3) If the user enters the value 1, your program displays the message ‘Enter the first string: ’ and receives the first string (s1) for comparison from the user. (4) Then your program displays the message ‘Enter the second string: ’ and receives the second string (s2) for comparison from the user. (5) Also your program displays the message ‘Number of characters: ’ and receives the number (n) of characters for comparison from the user. (6) Finally, your program displays the result from comparing the first n characters of two strings s1 and s2 which the user entered.
Using Python write a string-comparison function named strNcompare that compares up to n characters of a string s1 with the other string s2. The function must return 0, -1, or 1 if the first n-character portion of s1 is equal to, less than, or greater than the corresponding first n-character portion of s2, respectively.
(1) You must use the following function definition:
def strNcompare(s1, s2, n):
(2) String comparison must be case-insensitive. For example, the strNcompare function must return 0 for the s1 value of ‘tommy’ and the s2 value of ‘TOMMY’.
(3) You should not use a loop such as the while loop and the for loop inside the strNcompare function.
Then write a Python program that receives two words and the number of characters for comparison, calls the strNcompare function, and then shows the result. Your program keeps doing this until the user enters the value of -1.
Your program displays the message ‘String comparison [1(play), -1(quit)]: ’ and receives the value 1 or -1 from the user.
(2) If the user enters the value -1, your program finishes immediately.
(3) If the user enters the value 1, your program displays the message ‘Enter the first string: ’ and receives the first string (s1) for comparison from the user.
(4) Then your program displays the message ‘Enter the second string: ’ and receives the second string (s2) for comparison from the user.
(5) Also your program displays the message ‘Number of characters: ’ and receives the number (n) of characters for comparison from the user.
(6) Finally, your program displays the result from comparing the first n characters of two strings s1 and s2 which the user entered.
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 2 images