0

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;

  while (1) 
  { 
    s = inputString();
    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.

John Doe
  • 205
  • 6
  • 17

3 Answers3

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

  }

First of all, in your example you are looking for word "reses". And you just don't change a string inside the loop. So you will get out of the loop only if random string is reset after the initial generation. I don't know why do you need this, but code above will get out of the loop. Maybe not so soon, but it will.

Also, what is this one:

string[i] = digits[ rand() % 4 + 0 ];

Why + 0? And why % 4, not % 5?

Better go with this:

char *inputString()
{
    static char string[6];
    const char *digits = "reset";
    for (int i = 0; i < 5; i++) 
    {
        string[i] = digits[ rand() % 5];
    }
    return string;
}

And try using debugger, it really helps.

Pavel Oganesyan
  • 6,774
  • 4
  • 46
  • 84
  • sorry the function was supposed to be inside the loop, posted it wrong the first time – John Doe Jul 18 '15 at 00:30
  • Isn't string [5] already '\0' anyways? when I declared it: string[6] I thought that meant null byte was at string[5] with indexes 0 -4 for characters – John Doe Jul 18 '15 at 00:34
  • 1
    Correct me if I am wrong, but aren't `static` variables always initialised to zero due to their being in the `data` section? If so, `string[5] = '\0';` is redundant as it is already 0 – Levi Jul 18 '15 at 00:37
  • @Levi http://stackoverflow.com/questions/11229477/are-all-char-arrays-automatically-null-terminated – Pavel Oganesyan Jul 18 '15 at 00:43
  • 1
    @Levi and also this one - http://stackoverflow.com/questions/1414215/initial-value-of-int-array-in-c And to make it clear - yes, you are right, static makes it initialized. – Pavel Oganesyan Jul 18 '15 at 00:45
1

You are only running the inputString function once, so you will only ever receive one string. This can be fixed by moving the function call to the while (1) loop:

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

Also, to make your code slightly cleaner, you can use C's strcmp function to compare the string, rather than checking each individual character:

#include <string.h>

while (1) 
{ 
  s = inputString();
  if (strcmp(s, "reset") == 0) 
    {
      printf('worked!\n');
      break;
    }
}

Adding on to what @Pavel said about your random character selection, to make it a bit more reusable, you can use sizeof instead of a hard-coded 5:

string[i] = digits[ rand() % (sizeof(string) - 1)]; // Also removed extraneous 0
Levi
  • 1,921
  • 1
  • 14
  • 18
-1

I have two changes for you: You can make sure string[5] is 0. (It should already be because it's static)

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

    for (i = 0; i < 5; i++) 
    {
        string[i] = digits[ rand() % 4 + 0 ];
    }
    string[5] = '\0'; // null terminate your string.

    return string;
}

you can avoid doing while(1) by just putting the condition inside the while conditional check. and then perform the print after the while loop.

other_fxn() 
{
   char *s = inputString();

   while (s[0] != 'r' || s[1] != 'e' || s[2] != 's' || s[3] != 'e' || s[4] != 's' || s[5] != '\0') 
   { 
    s = inputString();
   }
   printf('worked!\n');
}
Timothy Murphy
  • 1,322
  • 8
  • 16