What the string?!

This is a discussion on What the string?! within the C Programming forums, part of the General Programming Boards category; why does lower[] print lower and upper case letters? probably right in front of my face! Code: #include <stdio.h> void ...

  1. #1
    Registered User PunchOut's Avatar
    Join Date
    Jun 2008
    Location
    norfolk, va
    Posts
    16

    What the string?!

    why does lower[] print lower and upper case letters?

    probably right in front of my face!

    Code:
    #include <stdio.h>
    
    void alpha(char upper[26], char lower[26]);
    
    main()  
    {
    	char upper[26], lower[26];
    	alpha(upper, lower);
    	printf("%s\n%s\n", upper, lower);
    
    }
    
    void alpha(char u[], char l[])
    {
    	int i;
            char X = 65;
            char x = 97;
    
            for(i = 0; i < 26; i++) 
    	{
                    u[i] = X++;
                    l[i] = x++;
            }
    }

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,655
    You need to have room for 27 chars in your arrays...you need room for the null terminator, and you need to add it.

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    BTW, I hope you don't use this technique in production code when in a company, since it isn't portable (i.e. it only works on ASCII platforms).
    But if you're doing this just for school then it would be a lot more readable if you replace 65 with 'A' and 97 with 'a' so people don't have to look up an ASCII chart to see if it's right or not.
    Last edited by cpjust; 11-23-2008 at 01:00 AM.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. compare structures
    By lazyme in forum C++ Programming
    Replies: 15
    Last Post: 05-28-2009, 03:40 AM
  2. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 05:30 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 07:49 PM

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