From a223e3bf7aa0b345bb941551e85c3a93ff228eb0 Mon Sep 17 00:00:00 2001 From: ashu9335 <115342974+ashu9335@users.noreply.github.com> Date: Sun, 29 Oct 2023 21:37:28 +0545 Subject: [PATCH] Create CREATE LONGEST WORD IN A GIVEN SENTENCE --- CREATE LONGEST WORD IN A GIVEN SENTENCE | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 CREATE LONGEST WORD IN A GIVEN SENTENCE diff --git a/CREATE LONGEST WORD IN A GIVEN SENTENCE b/CREATE LONGEST WORD IN A GIVEN SENTENCE new file mode 100644 index 0000000..ab592f0 --- /dev/null +++ b/CREATE LONGEST WORD IN A GIVEN SENTENCE @@ -0,0 +1,23 @@ +def find_longest_word(sentence): + # Split the sentence into words using spaces as the separator + words = sentence.split() + + # Initialize variables to store the longest word and its length + longest_word = "" + max_length = 0 + + # Iterate through the words to find the longest one + for word in words: + # Remove punctuation from the word, if necessary + cleaned_word = ''.join(c for c in word if c.isalnum()) + + if len(cleaned_word) > max_length: + max_length = len(cleaned_word) + longest_word = cleaned_word + + return longest_word + +# Example usage: +sentence = "This is an example sentence with some long words like 'antiestablishmentarianism'." +result = find_longest_word(sentence) +print("The longest word in the sentence is:", result)