In: Computer Science
Module Main
declare String str
declare String spaceSeparatedSentence
declare String pigLatinSentence
// input the run together string
Input str
// convert the input string to string with
spaces
stringWithSpace(str, spaceSeparatedSentence)
// display the formatted string
Display spaceSeparatedSentence
// convert the space separated string to
pigLatin
toPigLatin(spaceSeparatedSentence,
pigLatinSentence)
// display the pigLatin string
Display pigLatinSentence
End Module
// Module that takes as input a string in which all of the words
are run together and returns a string where words are separated by
spaces
Module stringWithSpace(String runTogetherSentence, String ref
spaceSeparatedSentence)
// declare integers for startIndex and endIndex
declare integer startIndex
declare integer endIndex
// set output string to first character of input
string
Set spaceSeparatedSentence to
runTogetherSentence[0]
// set startIndex to 1
Set startIndex to 1
Set endIndex to startIndex
// loop over the input string
while endIndex < length of
runTogetherSentence
do
// if character at endIndex is
uppercase
if runTogetherSentence[endIndex] is
uppercase then
// append the
substring from startIndex to endIndex(exclusive) to
spaceSeparatedSentence and then add a space and then append
lowercase version of character at endIndex to
spaceSeparatedSentence
spaceSeparatedSentence = spaceSeparatedSentence +
runTogetherSentence[startIndex:endIndex] + " " +
runTogetherSentence[endIndex].lower()
// update
startIndex to endIndex + 1
startIndex =
endIndex + 1
end if
// increment endIndex by 1
Set endIndex = endIndex + 1;
end while
// append the last word to spaceSeparatedSentence
string
spaceSeparatedSentence = spaceSeparatedSentence +
runTogetherSentence[startIndex:endIndex]
End Module
// Module to convert the input string separated by space to
pigLatin
Module toPigLatin(String spaceSeparatedSentence, String ref
pigLatinSentence)
// declare an empty list of words
declare List words
declare String word
declare integer i // loop variable
// split the input string where words are separated by
spaces to list of strings using space as the delimiter
words = spaceSeparatedSentence.split(" ")
// set output string to empty string
pigLatinSentence = ""
// loop over the list of words from index 0 to
end
for i = 0 to length of words-1
do
// get the ith word from list
word = words[i]
// append the word from second
character to end to pigLatinSentence and then append the first
character followed by ay and a space
pigLatinSentence = pigLatinSentence
+ word[1:] + word[0] + "ay "
end for
// remove the trailing space from
pigLatinSentence
pigLatinSentence = pigLatinSentence.strip()
End Module