20.5 LAB: Longest stringWrite a program that takes two strings and outputs the longer string, followed by "is longer than", and the shorter string. If the strings have the same length then output the strings followed by "have the same length".Ex. If the input is:almond pistachiothe output is:pistachio is longer than almond
Question
20.5 LAB: Longest stringWrite a program that takes two strings and outputs the longer string, followed by "is longer than", and the shorter string. If the strings have the same length then output the strings followed by "have the same length".Ex. If the input is:almond pistachiothe output is:pistachio is longer than almond
Solution
Here is a step-by-step guide to solve this problem in Python:
Step 1: Take two strings as input from the user.
str1 = input()
str2 = input()
Step 2: Compare the lengths of the two strings.
Step 3: If the length of the first string is greater than the second string, print the first string is longer than the second string.
if len(str1) > len(str2):
print(str1, "is longer than", str2)
Step 4: If the length of the second string is greater than the first string, print the second string is longer than the first string.
elif len(str2) > len(str1):
print(str2, "is longer than", str1)
Step 5: If the lengths of both strings are equal, print both strings have the same length.
else:
print(str1, "and", str2, "have the same length")
Here is the complete code:
str1 = input()
str2 = input()
if len(str1) > len(str2):
print(str1, "is longer than", str2)
elif len(str2) > len(str1):
print(str2, "is longer than", str1)
else:
print(str1, "and", str2, "have the same length")
Similar Questions
Longest Substring Without Repeating CharactersGiven a string s find the length of the longest substring without repeating characters.
Given a string s, find the length of the longest substring without repeating characters. Example 1:Input: s = "abcabcbb"Output: 3Explanation: The answer is "abc", with the length of 3.Example 2:Input: s = "bbbbb"Output: 1Explanation: The answer is "b", with the length of 1.Example 3:Input: s = "pwwkew"Output: 3Explanation: The answer is "wke", with the length of 3.Notice that the answer must be a substring, "pwke" is a subsequence and not a substring. Constraints:0 <= s.length <= 5 * 104s consists of English letters, digits, symbols and spaces.
Strings - Longest Common PrefixGiven the array of strings A, you need to find the longest common string S which is the prefix of all the strings present in the array.For example, you are given an array of two strings S1 and S2.S1 = "abcdefgh"S2 = "abcefgh"Longest common prefix of S1 and S2 is "abc"Note: Assume that there always exists some common prefix among the stringsConstraints:0 <= length of array <= 104Input Format:First line of input contains an integer N representing the size of the arrayNext N lines contains strings of the array A, each in a line.Output Format:Print the longest common prefix of all strings of ASample Test CasesTest Case 1:Expected Output:5programmerprogrammingprogramprogramaticprogramableprogramTest Case 2:Expected Output:4appleapplepieapple tree apple phoneapple
Your goal is to write a program that will ask the user for a long string and then a substring of that long string. The program then will report information on the long string and the substring. Finally it will prompt the user for a replacement string and show what the long string would be with the substring replaced by the replacement string. Each of the lines below can be produced by using one or more String methods - look to the String method slides on the course website to help you with this project.A few sample transcripts of your code at work are below - remember that your code should end with a newline as in the textbook examples and should produce these transcripts exactly if given the same input. Portions in bold indicate where the user has input a value.One run of your program might look like this:Enter a long string: The quick brown fox jumped over the lazy dogEnter a substring: jumpedLength of your string: 44Length of your substring: 6Starting position of your substring: 20String before your substring: The quick brown fox String after your substring: over the lazy dogEnter a position between 0 and 43: 18The character at position 18 is xEnter a replacement string: leapedYour new string is: The quick brown fox leaped over the lazy dogGoodbye!A second run with different user input might look like this:Enter a long string: Friends, Romans, countrymen, lend me your earsEnter a substring: tryLength of your string: 46Length of your substring: 3Starting position of your substring: 21String before your substring: Friends, Romans, counString after your substring: men, lend me your earsEnter a position between 0 and 45: 21The character at position 21 is tEnter a replacement string: catchYour new string is: Friends, Romans, councatchmen, lend me your earsGoodbye!
Have the function LongestWord(sen) take the sen parameter being passed and return the longest word in the string. If there are two or more words that are the same length, return the first word from the string with that length. Ignore punctuation and assume sen will not be empty. Words may also contain numbers, for example "Hello world123 567"
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.