Flowgorithm - Change a character in a string

110 Views Asked by At

Is there a way to change a single character in a String? In C programming you could do:

char s[4] = "abc";
s[0]='c';

Is there a way to manipulate a string in flowgorithm? I get an error if I try it:

enter image description here

There is always the error that the string is not an array. Maybe there is someone who got a hint to solve it.

1

There are 1 best solutions below

0
Costantino Grana On

Flowgorithm is often lacking in many aspects. Strings are not Arrays and cannot be indexed for writing. No idea why! But you can use Char() to get the i-th character in a string (whose type is a String with length 1). You can convert a 1 character long String into its ASCII code with ToCode() and you can move back from ASCII code to 1 character long String with ToChar(). Additionally Arrays (also parameters) are mutable, while Strings are immutable. We can then make a StringToArray() function:

StringToArray

then a ArrayToString() function:

ArrayToString

Then put them together in a SetChar() function:

SetChar

You can use it like this:

Main

The XML version:

    <function name="Main" type="None" variable="">
        <parameters/>
        <body>
            <declare name="x" type="String" array="False" size=""/>
            <assign variable="x" expression="&quot;Test 123&quot;"/>
            <assign variable="x" expression="SetChar(x, 4, &quot;0&quot;)"/>
            <output expression="x" newline="True"/>
        </body>
    </function>
    <function name="ArrayToString" type="String" variable="s">
        <parameters>
            <parameter name="a" type="Integer" array="True"/>
        </parameters>
        <body>
            <declare name="s" type="String" array="False" size=""/>
            <declare name="j" type="Integer" array="False" size=""/>
            <assign variable="s" expression="&quot;&quot;"/>
            <for variable="j" start="0" end="Size(a)-1" direction="inc" step="1">
                <assign variable="s" expression="s &amp; ToChar(a[j])"/>
            </for>
        </body>
    </function>
    <function name="SetChar" type="String" variable="s">
        <parameters>
            <parameter name="s" type="String" array="False"/>
            <parameter name="i" type="Integer" array="False"/>
            <parameter name="c" type="String" array="False"/>
        </parameters>
        <body>
            <declare name="a" type="Integer" array="True" size="Len(s)"/>
            <call expression="StringToArray(s,a)"/>
            <assign variable="a[i]" expression="ToCode(c)"/>
            <assign variable="s" expression="ArrayToString(a)"/>
        </body>
    </function>
    <function name="StringToArray" type="None" variable="">
        <parameters>
            <parameter name="s" type="String" array="False"/>
            <parameter name="a" type="Integer" array="True"/>
        </parameters>
        <body>
            <declare name="j" type="Integer" array="False" size=""/>
            <for variable="j" start="0" end="Len(s)-1" direction="inc" step="1">
                <assign variable="a[j]" expression="ToCode(Char(s,j))"/>
            </for>
        </body>
    </function>