CLEO Help split string

CLEO related
Status
Not open for further replies.

Cape

Member
Joined
Aug 23, 2018
Messages
7
Reaction score
0
How do I split a string into characters

e.g I have a string "Hello"

and I want it to be spit into variables like "H" "E" "L" "L" "O"

thanks
 

monday

Expert
Joined
Jun 23, 2014
Messages
1,127
Solutions
1
Reaction score
158
Hi, it would be good if you specified what you'd like to do with these letters afterwards. That's because there are many ways you could approach it. I bet you don't need to split anything, in the memory the string is stored as numbers sitting next to each other (1 number per 1 character), you can just navigate to where each letter is stored and read 1 byte from that location to get the value of the character.

e.g I have a string "Hello"
In your code you probably reference that string by using its' pointer. In C it would look like:
C:
char str[] = "HELLO";  // where "str" becomes pointer
or
C:
char * str = malloc(6);
strcpy(str, "HELLO");

In both situations "str" becomes a pointer to the "HELLO", the 2nd example is just more similar to the cleo example below. In cleo it would look something like:
Code:
alloc 0@ 6    // number of allocated bytes should be no less than length of string + 1
format 0@ "HELLO"

And then you can simply add the index of desired character to the pointer, and read its' value. That's how it looks in C:
C:
// use this to test yourself: http://www.onlinegdb.com/online_c_compiler
#include <stdio.h>

int main()
{
    char str[] = "HELLO";
    printf("2nd character = %c", *(str + 1));
    return 0;
}

// in this example the memory for "HELLO" is allocated on the "stack" area and the size of it is determined at the time of compilation
or
C:
#include <stdio.h>

int main()
{
    char * str = malloc(6);
    strcpy(str, "HELLO");
    printf("2nd character = %c", *(str + 1));
   
    free(str);
    return 0;
}

// in this example the memory is allocated on the "heap", the compiler won't handle the allocation, the user is responsible for making sure that he passes the right number to malloc(), the user is also responsible for calling free() to deallocate memory when it's not needed anymore
The output of both codes is:
2nd character = E

In cleo it would look something like:
Code:
alloc 0@ 6
format 0@ "HELLO"
0@ += 1
0A8D: read_memory 0@ size 1 vp 0 store_to 1@ 
0AD1: show_formatted_text_highpriority "2nd character = %c" time 2000 1@

0@ -= 1
free 0@

But it all comes down to what is your goal with this.

You could check this out anyway:
http://ugbase.eu/index.php?threads/tutorial-text-handling.19741/
 
Status
Not open for further replies.
Top