Search code examples
pythonarrayspseudocode

Writing pseudo code for camel cap into array


a friend asked me the other day to help him write pseudo code that would separate each word of the following string "HelloWorld" and store it in an array [x] with unused array elements with an empty string.

I actually didn't know where to start, I programmed it instead in Python however used some functions like in [A-Z] [a-z] to detect upper and lower cases so it knows when to split the strings but how do you even go about writing that in pseudo code since it doesn't have any official documentation?


Solution

  • The rationale behind pseudo code is to describe an algorithm. Normally you leave the gory details for a language implementation. But the fact is that you have to decide what level you write and what level you leave.

    Here I would write:

    declare an array of 10 strings arr initialized to empty strings
    set arr_index to 0
    initialize index to first position in string
    loop
        find first uppercase letter after index
        if none find: exit loop
        if found at new_index
            copy characters from index (inclusive) to new_index (exclusive) to a new string
            store that string in arr[arr_index]
            increment arr_index
            set index = new_index
    end loop
    arr_index is the number of words found
    

    Here I have left the research of an uppercase letter and the copy of a substring to a new string for the implementer, but IMHO the description is enough to write this immediately in C, C++ Python or Java (the languages I know)