How to Assign a Phrase to a line in a chars matrix?

Asked

Viewed 68 times

-1

I want to modify standard phrases from a theoretical game. For example, the game has the phrase "Choose a command" and, over a given state, I would like to exchange all letters "o" and "a" for a @.

For this, I thought of implementing a character array,

lines[100][500], which can store 100 500-character phrases.

I want to assign to the lines[1] "Choose an action", lines[2] "You cannot go there", etc.

And, to do what I want to do, given that the player is in such a state, I would use sscanf in sentence[x][] and go through the whole sentence[x][i], with i going from 0 to strlen phrase[x].

However, I’m not able to assign and I don’t know how to print these sentences.

The first problem is not knowing how to include ' 0' at the end of a string typed within the program. At the same time %s should work until you find the first 0 in the memory bin, and it is printing only a random character in the program below:

#include<stdio.h>

int main (){

   char falas [100][500];

   falas[1][0] =  "Seja bem vindo ao meu jogo";

   printf("%s", falas[1]);

}
  • 1

    Well, then it gets a little complicated because there’s a lot of things you’re not able to do, so we’ll have to do all the code for you, it would be better to face one problem at a time.

  • Just assigning strings to lines of a character array would help a lot.

  • How many sentences will there be?

1 answer

-1


You can assign using strcpy that is in the include cstring. So the code you set as an example should look like this:

 #include <stdio.h>
 #include <cstring>

int main() {

    char falas[100][500];

    strcpy(falas[0], "Seja bem vindo ao meu jogo");

    printf("%s", falas);
}
  • It worked, thank you very much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.