How can I print a string instead of char?

0

In the following code I have a char with 2 characters, H ( Heads ) and T ( Tails ), at the time of printing it will clearly print one of these 2 letters but I wanted to make a change and print "Face" or "Crown". How would I have to do this? Through a string vector? If so, 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 declaration and impressions:

 char fc[2][10] = {"Cara", "Coroa"};
 printf("%s" , comp);
    
asked by anonymous 07.11.2017 / 09:24

2 answers

1

First you declare an array from pointers to strings :

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

Next, one of the pointers contained in array is deleted:

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
    
07.11.2017 / 10:33
0
char fc[2][10] = {"Cara", "Coroa"};

Notice that your array has:

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

Position 1: ['C', 'o', 'r', 'o', 'a', '', ...]

while comp is a char,

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

This function will not execute. comp = ['C','a','r','a','',...]; fails since comp is a single char .

In addition, rand() must be initialized. The result of rand() named so will always be 0. Source

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

Here's the solution:

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