Friday, July 31, 2009

C programming help?

Hey guys, just trying to learn c programming here. I'm wondering, how would you be able to write a function that could determine between a capitalized and lowercase letter? I've been hearing something about a malloc or pointers and I'm not too sure if I understand.





Thanks

C programming help?
I'll try to explain pointers first, since they are very important in C programming.





In C, if you have a chunk of text in a variable, what you really have is a memory address that tells your code where in RAM (which I'll refer to as just memory) your text is being stored. This memory address is called a pointer in C.





When dealing with data in memory, everything (and I mean EVERYTHING) in C is handled as a number. So if you have code like this:





char letter = 'A';





what you really have is a 1 byte number with the value of 65. That's right, a capital "a" is actually stored in memory as the number 65, whereas a lowercase "a" is 97. These numbers are referred to as ASCII codes.





Here's an excellent ASCII reference: http://www.asciitable.com/


You'll be most interested in the "Dec" column for each letter for the correct number. "Dec" is short for decimal, which is the base 10 number system on computers (numbers 0 - 9), while "hex" (or "Hx" on that website) is short for hexadecimal, the base 16 number system (numbers 0 - F, see http://en.wikipedia.org/wiki/Hexadecimal...





Anyway, back to your question...





Now, armed with this information, if you look at the ASCII codes for each letter, you'll notice something: all uppercase letters have an ASCII code between 65 and 90, while lowercase letters are between 97 and 122.





The simplest way to test if a letter is uppercase or lowercase is to do something like this:





bool isupper( char letter )


{


if ( letter %26gt;= 65 %26amp;%26amp; letter %26lt;= 90 ) return true;


return false;


}





bool islower( char letter )


{


if ( letter %26gt;= 97 %26amp;%26amp; letter %26lt;= 122 ) return true;


return false;


}





What these two functions do is simply test to see if the letter you send them is within a certain range. If the letter is in that range, then the letter is either uppercase (if you tested with isupper) or lowercase (if you tested with islower).





Here are a few examples:





isupper( 'C' ) // returns true


isupper( 'j' ) // returns false





islower( 'C' ) // returns false


islower( 'j' ) // returns true





isupper( '2' ) // returns false


islower( '?' ) // returns false


isupper( '_' ) // returns false


islower( '@' ) // returns false





Something to note is if you give either function something that isn't a letter, you will always get false back.





Now, going back to pointers...





When you want to work with more than one letter, say the text "I’m a text string", each character (letters, numbers, spaces, punctuation, etc.) is a separate 1 byte number, which means you need 17 bytes of memory to store that text. So, you’ll need more than just a single 1 byte variable. This is where pointers come in.





When you do this:





char *text = "I’m a text string";





you are creating what’s referred to as a string literal in your code (which is a practice known as hard-coding) which is handled as a pointer to a block of memory that stores the text.





There are two ways to interact with this text now. One way is through pointer arithmetic, which is beyond the scope of this answer, and the other way is to treat your pointer as an array. Any pointer can be handled as an array in C, it’s up to you to know when and where this is appropriate. Since we know that the pointer “text” in the above code points to a block of text in memory, we know it’s safe to use it as a character array.





Put simply, if you want to access the first character in your text string, you would use text[0]. The second character is accessed with text[1], the third with text[2], fourth with text[3], and so on.





So in the end, by calling isupper( text[0] ), you will get true back.





I really hope you were able to follow all of that, and that it helps out. If you have any questions regarding my answer, just let me know.
Reply:Actually, you shouldn't need any header files or libraries at all, unless your compiler doesn't support bool natively (though it should). Report It

Reply:http://c2.com/cgi/wiki?CapitalizationRul...


hope that will help ,I dont really know alot about c programming or about programming and design but i have my reasons to be hear.
Reply:Use the isupper() function.





char x = 'a';


bool testing = isupper(x); // false





x = 'A';


testing = isupper(x); // true





Remember to add #include %26lt;cctype%26gt; at the top.


No comments:

Post a Comment