0

I normally watch the video and then switch to my editor program and rebuild it out of memory following my logic and the tools I just learned.

So I programmed it a little bit different and wondered if it was against logic or style definitions to leave out the variable n, directly jump to the condition part and compare i to strlen(s)

Original:

    #include <cs50.h>
    #include <stdio.h>
    #include <string.h>

    int main (void)
    {
        printf("Input\n");
        string s = GetString(); 
      for (int i = 0, n = strlen(s); i < n; ++i)
        {
            if (s[i] >= 'a' && s[i] <= 'z')
            {
                printf("%c", s[i] - ('a' - 'A'));
            }
        }
        printf("\n");
     }

My approach:

      for (int i = 0 ; i < strlen(s); ++i)
3

Both are valid, but they have a big difference. As is explained in the video, the main difference is that in your approach everytime the loop runs the condition it has to check the length of s, since in the other approach, that length is stored into a variable at the beginning of the loop so you don't need to call the length() function each time.

Your approach:

Store 0 in the variable i
Check if i is less than (take the length of s)
    if so printf("%c", s[i] - ('a' - 'A'));
Loop
Check if i is less than (take the length of s)
    if so printf("%c", s[i] - ('a' - 'A'));
Loop
Check if i is less than (take the length of s)
    if so printf("%c", s[i] - ('a' - 'A'));
Loop ...

While the other approach is:

Store 0 in the variable i
Store (take the length of s) in the variable n
Check if i is less than n
    if so printf("%c", s[i] - ('a' - 'A'));
Loop
Check if i is less than n
    if so printf("%c", s[i] - ('a' - 'A'));
Loop
Check if i is less than n
    if so printf("%c", s[i] - ('a' - 'A'));
Loop ...

So, doing it this way, you save time calling the lenght() function just once, while in your approach you are calling it in every iteration.

5
  • I see, so to keep my program efficient if its larger the "other approach" will make more sense. Thank you!
    – athordan
    Jul 14 '14 at 10:51
  • @athordan Yes. In a program of this size, it won't make much of a difference, but if it were a larger program or something that is running constantly, the first approach would be more efficient.
    – m_duran
    Jul 14 '14 at 20:23
  • @m_duran and by the first approach you are referring to the original one, aren't you? just to be sure, bc the order changed in the answer. thank you!
    – athordan
    Jul 15 '14 at 4:27
  • Another approach that would keep the efficiency, but may be more readable for beginners would be to set the value of n outside the loop, then just use n as the upper boundary of the for loop. However, since n is only used in the scope of the loop, it makes good sense to declare/initialize it in the loop as in the short. Jul 15 '14 at 19:59
  • @athordan to clarify, the one that initializes the variable n to the size of your string and uses that for your loop condition is the more efficient one, instead of running strlen() every iteration of the loop to find the exact same value.
    – m_duran
    Jul 18 '14 at 4:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .