Having a problem with an infinite loop

This is a discussion on Having a problem with an infinite loop within the C Programming forums, part of the General Programming Boards category; Hi everyone, I'm working on a programming assignment for a cs class and I am having trouble figuring out why ...

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    4

    Having a problem with an infinite loop

    Hi everyone,

    I'm working on a programming assignment for a cs class and I am having trouble figuring out why I am getting an infinite loop in my function flipstring, a function that reverses the order of words in a string while keeping the characters the same. Flipstring calls the function nextword so I will copy both functions. Here is my code,
    Code:
    int nextword(char *str);
    void flipstring(char *str);
    
    int nextword(char *str)
    {
            static char *s;
            static int nextindex;
            int thisindex;
            // reset the static variables
            if (str != NULL)  //string is called once.  NULL is used to find the nextword in str.
            {
                    s = str;
                    thisindex = 0;
    
    
            }
            else
            {
                    // set the return value to be the nextindex
                    thisindex = nextindex;
    
    
            }
    
    
            if (nextindex != -1)
              {
              while((s[nextindex] != ' ') && (s[nextindex] != '\n') && (s[nextindex] != '\t')){
                if(s[nextindex]=='\0'){
                  thisindex=-1;
                  return thisindex; //returns final index
                }
                else
                  nextindex++;  //if s is not white space, nextindex is incremented
              }
              s[nextindex]='\0';
              while ((s[nextindex] == ' ') || (s[nextindex] == '\n') || (s[nextindex] == '\t') || (s[nextindex] == '\0'))//nextindex is incremented after \0 is placed
                nextindex++;
            }
    
    
    
    
            return thisindex;
    }
    
    void flipstring(char *str){
      int i = nextword(str);
      char *c[256];
      int j=0,w=0,x=0;
      while(i != -1){
    
    
        c[j]=&(str[i]);
        printf("%d: %s\n",j,c[j]);  //Here for testing reasons
        i = nextword(NULL);
        j++;
      }
      j--; //j was incremented too far in the last while loop
      for(j;j>=0;j--){
        w=0;  //reset w for every string in c
        while(c[j][w] != '\0'){
          str[x]=c[j][w];
          printf("str[x] x=%d c[j][w] j=%d w=%d str=%s",x,j,w,str); //Here for testing reasons
          w++;
          x++;
        }
        str[x]=' '; //sets space
        x++;
      }
      str[x]='\n';
      str[x+1]='\0';
    }
    Here is the code I am using to test flipstring,

    Code:
    void main(int argc, char **argv) 
    {
    char str[] = "Hello Everyone.  I'm doing great!";
    flipstring(str);
    printf("%s",str);
    }
    I use nextword to save my strings in an array of strings, c. I then try to copy of the characters of the strings in c back to the string str in the reverse order. For some reason my while loop,
    Code:
    while(c[j][w] != '\0')
    , runs infinitely. The print statement tells me that j is decremented down to 1 and stays at 1, while x and w increment infinitely. My only theory as to why the while statement runs forever is that there are no \0 characters in some/all of my strings, but this theory doesn't explain why j is decremented at all in the first place. Any help with this problem would be much appreciated, I have been trying to figure this out for a while now.

  2. #2
    Registered User
    Join Date
    Mar 2012
    Posts
    35
    Looks like in flipstring
    Code:
    while(i != -1)
    is always true....

    N this declaration is i = nextword(NULL); is keeping it true.....

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Code:
    int nextword(char *str)
    {
    ...
            if (nextindex != -1)
              {
              while((s[nextindex] != ' ') && (s[nextindex] != '\n') && (s[nextindex] != '\t')){
                if(s[nextindex]=='\0'){
                  thisindex=-1;
                  return thisindex; //returns final index
                }
    If you reach the end of the string ('\0') and the word doesn't end with whitespace you throw the starting index for the last word away.

    Bye, Andreas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. infinite loop problem
    By langamer101 in forum C Programming
    Replies: 7
    Last Post: 12-12-2011, 01:55 AM
  2. Replies: 3
    Last Post: 10-14-2011, 11:33 PM
  3. Infinite Loop, problem with while()
    By JonathanS in forum C Programming
    Replies: 2
    Last Post: 09-08-2011, 03:47 AM
  4. Infinite loop problem
    By Xanth in forum C++ Programming
    Replies: 2
    Last Post: 02-19-2005, 11:08 PM
  5. infinite loop problem
    By Gil22 in forum C++ Programming
    Replies: 3
    Last Post: 03-07-2003, 02:24 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21