How can I print a string instead of char?

Asked

Viewed 190 times

0

In the following code I have a char with 2 characters, H (Heads) and T (Tails), at the time of printing will clearly be printed one of these 2 letters, but I wanted to make a change and print "Heads" or "Tails". In what way would I have to do that? Through a string array? If yes how?

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(int argc, char *argv[]) {

    char fc[2] = {'H','T'};
    srand(time(NULL));

    int i =0;
    int j = 0;
    char temp ;
    int c;
    printf("Nesse programa voce pode ver quantas vezes seria preciso para uma moeda cair X vezes do mesmo lado.\n");
    printf("Digite quantas vezes em sequencia a moeda deve cair:\n");
    scanf("%d",&c);

    while(i < c){
        char comp = fc[rand()%2];
        if (j == 0){
            printf("inicio\n");
            temp = comp;            
        }
        else if(temp == comp){
            i++;
            temp = comp;
            printf("%c\n", comp);
        }
        else {
            i = 0;
            printf("%c\n", comp);
        }
        j++;
    }

    printf("Tentativas: %d\n", j-1);

    system("pause");
    return 0;
}

I have tried to make the following changes to the statement and to the impressions:

 char fc[2][10] = {"Cara", "Coroa"};
 printf("%s" , comp);

2 answers

1


First you declare a array pointer strings:

char * fc[2] = { "Cara", "Coroa" };

Then one of the pointers contained in array:

char * comp = fc[ rand() % 2 ];

To display the string from the drawn pointer:

printf( "%s" ,comp );

Putting it all together:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>


int main( void )
{
    char * fc[2] = { "Cara","Coroa" };
    int x = 0;
    int nmax = 0;
    int aux = -1;
    int ntentativas = 0;
    int nrepeticoes = 0;

    srand(time(NULL));

    printf("Nesse programa voce pode ver quantas vezes seria preciso para uma moeda cair X vezes do mesmo lado.\n");
    printf("Digite quantas vezes em sequencia a moeda deve cair:\n");
    scanf( "%d", &nmax );

    while( nrepeticoes < nmax )
    {
        ntentativas++;

        x = rand() % 2;

        printf( "Tentativa %d: %s\n", ntentativas, fc[x] );

        if( aux == x )
            nrepeticoes++;
        else
            nrepeticoes = 1;

        aux = x;
    }

    printf("Numero de Tentativas: %d\n", ntentativas );

    return 0;
}

Testing:

$ ./moeda
Nesse programa voce pode ver quantas vezes seria preciso para uma moeda cair X vezes do mesmo lado.
Digite quantas vezes em sequencia a moeda deve cair:
4
Tentativa 1: Cara
Tentativa 2: Cara
Tentativa 3: Coroa
Tentativa 4: Coroa
Tentativa 5: Cara
Tentativa 6: Coroa
Tentativa 7: Cara
Tentativa 8: Cara
Tentativa 9: Cara
Tentativa 10: Cara
Numero de Tentativas: 10

0

char fc[2][10] = {"Cara", "Coroa"};

Note that your array has:

heading 0: ['C','a','r','a','',...]

position 1: ['C','o','o','a','',...]

while comp for a char,

char comp = fc[rand()%2];

this function will not perform. comp = ['C','a','r','a','',...]; failure since comp is a single char.

Additionally, rand() has to be initialized. The result of rand() so called will always be 0. Source

srand((unsigned) time(NULL));
//em alternativa: 
// time_t t;
// srand((unsigned) time(&t));

Here is the solution:

int main() 
{
    srand((unsigned) time(NULL));
    char fc[2][10] = {"Cara", "Coroa"};
    char *comp = fc[rand()%2];
    printf("%s" , comp);
}

Browser other questions tagged

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