vendredi 17 juillet 2015

Trying to randomly generate a string in order to get a conditional to execute

Seems that strings in C are my weak point, need your guys' help again. I should have included this in my first one, so I apologize about that.

So I have this function that I got working correctly, it fills a string with random chars from the set {r, e, s, e, t} and returns it:

char *inputString()
{
    static char string[6];
    const char *digits = "reset";
    int i;

    for (i = 0; i < 5; i++) 
    {
        string[i] = digits[ rand() % 4 + 0 ];
    }

    return string;
}

So the char at string[5] is '\0', correct? And string[1] through string[4] are random combinations of the other characters (r, e, s, e, t).

Now, I have another function that calls this one. These purpose of it is trying to get the printf statement to execute:

other_fxn() 
{
  char *s;

  s = inputString();
  while (1) 
  { 
    if (s[0] == 'r'&& s[1] == 'e' && s[2] == 's' && s[3] == 'e' && s[4] == 's'
        && s[5] == '\0') 
    {
      printf('worked!\n');
      break;
    }
  }
}

main:

{
   srand(time(NULL));
   other_fxn();
}

It compiles fine but the loop runs forever, with the if statement never executing. Can somebody help me out why it won't execute. Thanks once again.




Aucun commentaire:

Enregistrer un commentaire