Loop forever

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Will2k
    New Member
    • Feb 2007
    • 10

    #1

    Loop forever

    I am trying to make this function keep going on until I call an interrupt or, delete function in my GCC. HEre is what I have. I tried putting a while(1) loop in my driver.c but it does not stop.

    Code:
    #include <stdio.h>
    
    int sreplace(char newc, char oldc, char *s)
    {
    
            int cnt;
            cnt = 0;
    
            while(*s !='\0') //loop to run till the end of the string
            {
                    if(*s == oldc) //if the char found which to be replaced
                    {
                            *s = newc; //replace
    
                            cnt++;
                    }
                    s++;
    
            }
            return cnt;
    }
    Code:
    #include <stdio.h>
    
    char *strmatch(char *str, char *s)
    {
    
    
            char *tmp;
    
            while(*str!='\0'){
                    tmp = s;
                    while(*tmp !='\0' && *str == *tmp)
                    {
                            str++;
                            tmp++;
                    }
                     if(*tmp =='\0')
                     {
                            return str;
                     }
    
            str++;
            }
            return(NULL);
    }
    Code:
    #include <stdio.h>
    int sreplace(char newc, char oldc, char *s);
    char *strmatch(char *str, char *s);
    
    int main()
    {
            char line1[1024];
            char line2[1024];
            char *ptr;
            char *s1, *s2;
    
    
            fgets((s1=line1),1024,stdin);
            fgets((s2=line2),1024,stdin);
    
             sreplace('\0','\n', line1);
             sreplace('\0','\n', line2);
    
            ptr = strmatch(s1,s2);
    
            printf("%s\n",ptr);
    
    
            return 0;
    
    }
  • mattmao
    New Member
    • Aug 2007
    • 121

    #2
    Hi.

    If you want to have a forever loop, try this:

    while(1)
    {

    code block;

    //loop control goes here:
    if(test method)
    break;
    }

    This break; statement would let you terminate the while loop.

    Comment

    • sicarie
      Recognized Expert Specialist
      • Nov 2006
      • 4677

      #3
      Originally posted by mattmao
      Hi.

      If you want to have a forever loop, try this:

      while(1)
      {

      code block;

      //loop control goes here:
      if(test method)
      break;
      }

      This break; statement would let you terminate the while loop.
      or you could put the test method in the while condition...

      Comment

      Working...