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

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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