Jump to content

Código C


Localhost
 Share

Recommended Posts

Olá pessoal, eu estou aqui com muitas dificuldades neste programa, o problema é que ele me está a imprimir duas vezes a mesma string e está a pôr mais letras do que aquilo que devia, eu só queria que ele pusesse 4.

Está aqui o programa:

#include <stdio.h>

int main()
{
  char abc[24] = {'a','b','c','d','e','f','g','h','i','j','l','m','n','o','p','q','r','s','t','u','v','x','z'};
  int i=0,y,z,j,x=0;
  char str[3];
  srand(time(NULL));
  while(i<=3)
  {
    str[i] = abc[x];
    x++;
    i++;
    y = rand() % 24 + 1;
    str[i] = abc[y];
    z = rand() % 24 + 1;
    i++;
    str[i] = abc[z];
    j = rand() % 24 + 1;
    i++;
    str[i] = abc[j];
    printf("%s\n", str);
  }
  return 0;
}

here since 2009

Link to comment
Share on other sites

Pessoal, já consegui pôr direito com três caracteres mas quando meto 4 já com os ajustes em todas as strings (numero de caracteres) ele falha.

Está aí o código neste momento:

#include <stdio.h>

int main()
{
  char abc[24] = {'a','b','c','d','e','f','g','h','i','j','l','m','n','o','p','q','r','s','t','u','v','x','z','\0'};
  int i=0,y,z,j,x=0,a=0;
  char str[3];
  srand(time(NULL));
  /*while(a<100)
  {*/
    for(i=0;i<=3;i++)
    {
      str[i] = abc[x];
      x++;
      i++;
      y = rand() % 24 + 1;
      str[i] = abc[y];
      z = rand() % 24 + 1;
      i++;
      str[i] = abc[z];
      j = rand() % 24 + 1;
      i++;
      str[i] = abc[j];
      if(i==3) str[i] = '\0';
      printf("%s\n", str);
      a++;
    }
  //}
  return 0;
}

here since 2009

Link to comment
Share on other sites

E utilizares o ciclo para o que ele serve?

#include <stdio.h>

int main()
{
  char abc[23] = {'a','b','c','d','e','f','g','h','i','j','l','m','n','o','p','q','r','s','t','u','v','x','z'};
  int i=0,x=0;
  char str[3];
  srand(time(NULL));
  for(i=0;i<=3;i++) {
      x = rand() % 23 + 1;
      str[i] = abc[x];
  }
  str[i] = '\0';
  printf("%s\n", str);

  return 0;
}

PS: Não compilei, mas acho que deve estar a funcionar.

"Nós somos o que fazemos repetidamente, a excelência não é um feito, e sim, um hábito."
Não respondo a questões por PM que possam ser colocadas no fórum!

Link to comment
Share on other sites

Pessoal, já consegui pôr o programa a funcionar direitinho, agora tem um problema...

É que eu pu-lo a começar sempre a string com a letra "a", só que agora o problema é que eu ao metê-lo a iniciar com a letra "a" e ele não mete mais "a's" em mais nenhum lado da string. Será que é algum problema com a função rand()?

here since 2009

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.