Pseudocode CamelCase适用于AS Level Homework

时间:2018-01-17 19:54:14

标签: pseudocode

我得到了这个任务来完成作业...我得到的关键任务是数组的声明,但我不知道如何将每个单词分隔为伪代码中的字符串并用&#34填充空格;(空)"这是我的代码到目前为止,但我很难过!这是dor AS Level BTW ...

DECLARE CamelCaseArray : ARRAY[1:10] OF STRING
REPEAT
    PRINT “Enter a word starting with a capital letter: (eg. Word)”
UNTIL ??? /*I am stumped on what to do here, I do not have to use a REPEAT UNTIL BTW.*\

enter image description here

2 个答案:

答案 0 :(得分:0)

伪代码是单独变化的,所以我不知道你会发现下面的伪代码有多少与你想象的相似。

首先,我假设您已经硬编码(即它是一个固定值)10 字符串数组中的元素。

我还假设您无法使用任何预编码功能(如Split('')),但您可以像在数组中一样访问字符串中的字符。 (示例STRING STR = "HELLO", STR[0] = 'H')并且您可以将字符文字(' C')与ASCII值(67)进行比较。

你的伪代码让我想起了COBOL(Link提供的)所以也许你可以对它做一些相似之处。

https://www.tutorialspoint.com/cobol/cobol_loop_statements.htm (寻找PERFORM UNTIL)

    DECLARE CamelCaseArray : ARRAY[1:10] OF STRING
    DECLARE Input : STRING
    DECLARE CharCounter : INT >* This counter will be used to move thru the string as an array of characters
    DECLARE ArrayCounter : INT >* This will indicate an element of our array.
    CharCounter = 0 *> Set both to zero.
    ArrayCounter = 0 
    PRINT “Enter a string in CamelCase format>”
    READ Input FROM CONSOLE

    REPEAT 
    IF Input[CharCounter] > 64 AND Input[CharCounter] < 91 AND CharCounter IS NOT 0 >* Check whether our current character's ASCII values are between 64 and 91, which are the values for capital letters in the ASCII table AND if our character is not the first one in the string (since then we don't advance our array counter, we just add it instead.")
    CamelCaseArray[ArrayCounter] = CamelCaseArray[ArrayCounter] + Input[CharCounter] >* If it is, add it to the STRING element of the array
    ArrayCounter++ >* Increment our array counter.
    ELSE
    CamelCaseArray[ArrayCounter] = CamelCaseArray[ArrayCounter] + Input[CharCounter] >* Otherwise, just add the current character to the current array element
    END IF
    CharCounter++ >* Increment the character counter anyways, since we advance in the string.
    UNTIL CharCounter = LENGTH OF Input >* Do all this UNTIL our character counter is at the end of the string (I.E. It's length).
>* Now , filling the rest of the array with the (EMPTY) string:
IF ArrayCounter<LENGTH OF CamelCaseArray >* If our array counter is less than the array length after we went through our 10 words, we can fill the rest with (EMPTY)
REPEAT
CamelCaseArray[ArrayCounter] = "(EMPTY)" >* Fill current element with empty
ArrayCounter ++ >* Increment ArrayCounter
UNTIL ArrayCounter = LENGTH OF CamelCaseArray >* Do all this until we reach the end of our array.
END IF

希望我帮忙!

答案 1 :(得分:0)

这是你要求的python代码

CamelCaseArray = [""]*10
CharCounter = 0
ArrayCounter = 0
Input = input("Enter a string in CamelCase format>")
Input= list(Input)
for i in range (0,len(Input)):
 if ord(Input[i]) > 64 and ord(Input[i]) < 91 and i != 0:
  ArrayCounter+=1
 CamelCaseArray[ArrayCounter] = CamelCaseArray[ArrayCounter] + Input[i]

if ArrayCounter < 10:
 ArrayCounter+=1
 for i in range (ArrayCounter,10):
  CamelCaseArray[i] = "(EMPTY)"

for i in range (0,10):
 print(CamelCaseArray[i])