Mike Fredrik
Mike Fredrik

Reputation: 73

Use string functions on a Const char string[]

I'm attempting to make a method that will take the argument const char string[] from the user. this parameter. will then be outputted via a for loop to the screen character by character until the string has reached it's length.

I need to know the length of the string in order for my loop to work. for instance, if the length is 6, the loop should run 6 times, and display 6 characters onto the display.

I would have used a foreach loop for this, but I'm actually in the GBA environment and for some reason for each loops don't compile correctly. So I'm trying to use .strlen() instead to get the length of the string.

However, I cannot use the .strlen() method because my const char string[] isn't a string.

With all the above in mind, How can I use the .strlen() method in the following code?:

void DrawText(int x, int y, const char string[])
{


    for(int i = 0; i < string.strlen(); i++)
    {
        SetTile(0, x + i, y, 70);
    }
}

Upvotes: 1

Views: 60

Answers (1)

Emil Laine
Emil Laine

Reputation: 42838

You can use std::strlen(string) from the cstring header, but only if string points to a null-terminated char array.

Upvotes: 1

Related Questions