For Loop Question

This is a discussion on For Loop Question within the C Programming forums, part of the General Programming Boards category; I encountered this problem when going over some walkthrough's. Code: for (i = 1; i + k < len; i++) ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    3

    For Loop Question

    I encountered this problem when going over some walkthrough's.

    Code:
    for (i = 1; i + k < len; i++)
    {
               for (j = i; s[j + k]; j++)
                    s[j] = s[j + k];
    
               s[j] = '\0';
               len -= k;
               k++;
    }
    In the nested for loop,

    Code:
     for (j = i; s[j + k]; j++)
    what does s[j + k] do? how does the for loop terminate? assume s is an array of characters.

    Any help would be great. Thanks

    Here's the whole program:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    void fold(char s[], int k)
    {
            int i, j, len;
    
            len = strlen(s);
            for (i = 1; i + k < len; i++)
            {
                    for (j = i; s[j + k]; j++) 
                    {
                            s[j] = s[j + k];
                    }
                  
                    s[j] = '\0';
                    len -= k;
                    k++;
            }
    }
    
    main()
    {
            char s1[31], s2[31];
    
            strcpy(s1, "Show me less!? We always");
            strcpy(s2, "worry:kiss and neck, or split");
            fold(s1, 1);
            fold(s2, 0);
            printf("%s %s all, however\n", s1, s2);
    }
    Output:

    Some ways work not all, however
    Last edited by Cloud@CNET; 03-23-2003 at 07:15 PM.

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,671
    Here's how I "read" for loops:
    Code:
    int n;
    for (n = 0; n < 100; n++)
       ...
    "For n equals 0, while n is less than 100, n plus plus"
    The first expression is only executed once, and first: "n = 0".
    The middle expression is the loop conditional - while expression is true, keep truck'n
    The last expression is executed after every loop and before the middle expression is executed.

    So we can rewrite the loop as while loop like so:
    Code:
    int n = 0;
    while (n < 100)
    {
       ...
       n++;
    }
    A conditional expression is C will evaluate to true (non-zero) or false (zero). So if s[j + k] contains a value of 0, then "if (s[j + k])" will evaluate to false.
    0 == '\0'

    gg

  3. #3
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    >>main()
    That's wrong, main() only returns integer, so you should change this to int main() and at the end of the program return a value;

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >That's wrong
    Only half wrong. With C89 if the return type is omitted then int is assumed, C99 makes this illegal though. Another interesting property of the implicit int is that by using it if you don't actually return a value, the compiler tends not to complain. I believe this is why such declarations of main were popular. But to be 100% correct and portable, main should be declared as so:
    Code:
    int main ( void )
    {
      return 0; /* May be omitted in C99, 0 returned by default */
    }
    or
    Code:
    int main ( int argc, char *argv[] )
    {
      return 0; /* May be omitted in C99, 0 returned by default */
    }
    Other portable return values are defined as EXIT_SUCCESS and EXIT_FAILURE in stdlib.h.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 12:47 AM

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