Localhost Posted December 17, 2009 at 09:18 PM Report Share #301183 Posted December 17, 2009 at 09:18 PM 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 More sharing options...
Localhost Posted December 17, 2009 at 09:39 PM Author Report Share #301188 Posted December 17, 2009 at 09:39 PM 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 More sharing options...
anolsi Posted December 17, 2009 at 10:07 PM Report Share #301192 Posted December 17, 2009 at 10:07 PM 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 More sharing options...
Localhost Posted December 17, 2009 at 10:50 PM Author Report Share #301201 Posted December 17, 2009 at 10:50 PM Isso eu ja tinha feito em codes anteriores, o problema é que ele está a repetir as palavras duas a duas, tipo este output: ntjh ntjh Eu não queria que isto acontecesse... here since 2009 Link to comment Share on other sites More sharing options...
Baderous Posted December 17, 2009 at 11:07 PM Report Share #301203 Posted December 17, 2009 at 11:07 PM Estás a escrever 5 caracteres para uma string de dimensão 3. Link to comment Share on other sites More sharing options...
Localhost Posted December 18, 2009 at 11:45 AM Author Report Share #301229 Posted December 18, 2009 at 11:45 AM 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 More sharing options...
Localhost Posted December 18, 2009 at 11:52 AM Author Report Share #301235 Posted December 18, 2009 at 11:52 AM Pessoal, eu acho e não sei porque que o problema é da letra "a", eu estou aqui a criar as palavras e letra "a" não aparece :s here since 2009 Link to comment Share on other sites More sharing options...
Localhost Posted December 18, 2009 at 11:55 AM Author Report Share #301236 Posted December 18, 2009 at 11:55 AM Triple-post 😛 O problemas estava no rand que estava a ir entre 1 e 23 não passando pelo 0, ou seja nao ia buscar a psoição 0 da string abc. Obrigado a todos, podem fechar 😛 here since 2009 Link to comment Share on other sites More sharing options...
skin Posted December 18, 2009 at 03:20 PM Report Share #301285 Posted December 18, 2009 at 03:20 PM Duas coisas: 1ª - para a próxima coloca um título esclarecedor no tópico 2ª - em vez de fazer 3 posts seguidos, altera o anterior 😛 Our lives begin to end the day we become silent about things that matter - Martin Luther King Link to comment Share on other sites More sharing options...
Localhost Posted December 18, 2009 at 09:43 PM Author Report Share #301360 Posted December 18, 2009 at 09:43 PM Ok, desculpa. here since 2009 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now