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.

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter7: User-defined Simple Data Types, Namespaces, And The String Type
Section: Chapter Questions
Problem 8PE: Write a program that reads in a line consisting of a students name, Social Security number, user ID,...
icon
Related questions
Question
100%

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.

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 2 images

Blurred answer
Knowledge Booster
Function Arguments
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr