Thread: Basic beginner loop (with arrays) does NOT work but why?

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    23

    Basic beginner loop (with arrays) does NOT work but why?

    Hi. I am frustrated with a piece of code I wrote because it malfunctions when the user inputs (command argument style) any string (keyword) with the letter a or A, and I have no idea why and I hate not knowing what's going on. Can you please tell me what I am doing wrong and why the code doesn't work when the user inputs a string with the letter a or A?

    Code:
    #include <cs50.h>
    #include <stdio.h>
    #include <string.h>
    
    int main(int argc, string argv[])
    {
         string keyword = argv[1];
         for (int j = 0; j < strlen(keyword); j++)
        {
            if (keyword[j] >= 'a' && keyword[j] <= 'z')
            {
                keyword[j] = keyword[j] - 'a';
            }
            else if (keyword[j] >= 'A' && keyword[j] <= 'Z')
            {
                keyword[j] = keyword[j] - 'A';
            }
            printf("%i\n", keyword[j]);    
        } 
    }
    P.S. The code works fine with every other letter. Just not A or a.

    Thank you!
    Last edited by Rubik; 03-07-2013 at 04:50 PM.

  2. #2
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    It's a subtle little bug. If the string contains an 'A' or an 'a', then keyword[j] gets set to zero. Once that happens, keyword[j] = 0 = '\0' = end of string. So strlen(keyword) thinks the string ends there, and halts the loop. You want this:

    Code:
    length_of_word = strlen(keyword);
    for (int j = 0; j < length_of_word; j++) {
    /*and so on */
    Code:
    while(!asleep) {
       sheep++;
    }

  3. #3
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    How does strlen() work?
    What is the integer value of the character '\0'?
    What is the integer value of 'a' - 'a' ?

  4. #4
    Registered User
    Join Date
    Feb 2013
    Posts
    23
    Hi TheBigH! Thank you for your answer!

    I tried putting a while loop at the bottom inside a for-loop, but it doesn't work; here's what I did:
    Code:
    printf("%i", keyword[j]);
    while (keyword[j] == 0)
        j++;
    Then, I tried writing an if-condition within the if statement:
    Code:
    for (int j = 0; j < strlen(keyword); j++)
        if (keyword[j] >= 'a' && keyword[j] <= 'z')
        {
           if (keyword[j] == 'a')
           {     
                keyword[j] = keyword[j] - 'a';
                j++; 
            }
            else 
                 keyword[j] = keyword[j] - 'a';
         }
    but that gives me completely different values when I enter a (it messes up the j count).

    What am I doing wrong?

  5. #5
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    Quote Originally Posted by TheBigH View Post
    Code:
    length_of_word = strlen(keyword);
    for (int j = 0; j < length_of_word; j++) {
    /*and so on */
    Ummm.

  6. #6
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    294
    Quote Originally Posted by Tclausex View Post
    Ummm.
    It's not exactly the answer to the question posted, but a good idea nonetheless.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Tclausex View Post
    How does strlen() work?
    It counts characters until it finds one with a value zero.

    Quote Originally Posted by Tclausex View Post
    What is the integer value of the character '\0'?
    Zero.
    Quote Originally Posted by Tclausex View Post
    What is the integer value of 'a' - 'a' ?
    Seriously? You're asking this?
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    @Grumpy: I'm pretty sure Tclausex was asking those questions of the OP to get them thinking about the problems they were having with the code.

  9. #9
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    I'm a fan of the Socratic method /shrug.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Explain This Basic Program To Beginner
    By ASHWHIT in forum C Programming
    Replies: 8
    Last Post: 01-03-2012, 10:51 PM
  2. Basic compiling problem for a beginner
    By crazychile in forum C Programming
    Replies: 4
    Last Post: 09-21-2008, 02:27 AM
  3. Beginner: Why doesn't this work?
    By UncouthYouth in forum C Programming
    Replies: 5
    Last Post: 01-11-2008, 01:09 PM
  4. Basic stuff but dont work.
    By peckitt99 in forum C++ Programming
    Replies: 5
    Last Post: 09-05-2006, 03:20 AM
  5. very basic beginner problem
    By sandingman1 in forum C++ Programming
    Replies: 7
    Last Post: 11-26-2005, 05:48 PM