-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathq1_string.R
32 lines (24 loc) · 971 Bytes
/
q1_string.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# Lab cycle question 1
#Question 1: Data Types and Manipulation
#Develop a program to read a paragraph of text and perform the following tasks:
#a. Count the total number of words.
#b. Calculate the average word length.
#c. Identify and print the longest word.
#d. Replace all occurrences of a specific word with another word of your choice.
# Reading a paragraph
para <- readline("Enter your paragraph : ")
sprintf("Read paragraph: %s", para)
# Counting the number of words
num_words <- lengths(strsplit(para, " "))
sprintf("Number of words %x", num_words)
words <- strsplit(para, "[[:space:]]")
# Calculate the total number of characters in all words
total_characters <- sum(nchar(words))
# Calculate the total number of words
total_words <- length(words)
# Calculate the average word length
average_word_length <- total_characters / total_words
# Print the result
cat("Average word length:", average_word_length, "\n")
# Identifying the longest word
word_array