Q:

Write a Ruby program to truncate a given string to the first n words

0

Write a Ruby program to truncate a given string to the first n words.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

str = "Python is a  widely used   high-level, general-purpose,
interpreted, dynamic programming language.
Its design philosophy emphasizes code readability, 
and its syntax allows programmers to express concepts in
fewer lines of code than possible in languages such 
as C++ or Java."
n = 3
print str.split[0...n].join(' ')
n = 8
print "\n",str.split[0...n].join(' ')
n = 11
print "\n",str.split[0...n].join(' ')

Output:

Python is a
Python is a widely used high-level, general-purpose, interpreted,
Python is a widely used high-level, general-purpose, interpreted, dynamic programming language.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now