# Ch10 - CSEB114: Chapter 9 Character and String prepared by...

This preview shows pages 1–11. Sign up to view the full content.

Click to edit Master subtitle style 9/25/2008 prepared by NI, edited by MAF CSEB114: Principle of Programming Chapter 9: Character and String

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
9/25/2008 prepared by NI, edited by MAF Objectives In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference between an integer digit and a character digit Character handling library String conversion functions Standard input/output library functions String manipulation functions
9/25/2008 prepared by NI, edited by MAF Fundamentals of Characters and Strings Characters in C consist of any printable or nonprintable character in the computer’s character set including lowercase letters, uppercase letters, decimal digits, special characters and escape sequences. A character is usually stored in the computer as an 8-bits (1 byte) integer. The integer value stored for a character depends on the character set used by the computer on which the program is running.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
9/25/2008 prepared by NI, edited by MAF Fundamentals of Characters and Strings There are two commonly used character sets: ASCII (American Standard Code for Information Interchange) EBCDIC (Extended Binary Coded Decimal Interchange Code)
9/25/2008 prepared by NI, edited by MAF Difference Between an Integer Digit and a  Character Digit char num = 1 and char num = ‘1’ are not the same. char num = 1 is represented in the computer as 00000001. char num = ‘1’ on the other hand is number 49 according to the ASCII character set. Therefore, it is represented in the computer as 00110001.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
9/25/2008 prepared by NI, edited by MAF Example: ASCII character #include <stdio.h> void main(void) { char my_A = 'A'; char my_Z = 'Z'; char my_a = 'a'; char my_z = 'z'; printf("\nASCII value for A is %d", my_A); printf("\nASCII value for Z is %d",my_Z); printf("\nASCII value for a is %d", my_a); printf("\nASCII value for z is %d",my_z); printf("\n"); printf("\n65 in ASCII represents %c",65); printf("\n90 in ASCII represents %c",90); printf("\n97 in ASCII represents %c",97);
9/25/2008 prepared by NI, edited by MAF Sample output ASCII value for A is 65 ASCII value for Z is 90 ASCII value for a is 97 ASCII value for z is 122 65 in ASCII represents A 90 in ASCII represents Z 97 in ASCII represents a 122 in ASCII represents z

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
9/25/2008 prepared by NI, edited by MAF Example cont… #include <stdio.h> void main(void) { char ch; printf("enter a character: "); scanf("%c", &ch); if (ch >= 'A' && ch <= 'Z') { printf("\ncapital letter\n"); } } #include <stdio.h> void main(void) { char ch; printf("enter a character: "); scanf("%c", &ch); if (ch >= 65 && ch <= (65+26)) { equivalen t to
9/25/2008 prepared by NI, edited by MAF Fundamentals of Characters and Strings A string in C is an array of characters ending with the null character (‘\0’). It is written inside a double quotation mark (“ ”) A string may be assigned (in a declaration) to either a char array or to a char pointer: char color[] = “green”; OR char *color = “green”;

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
9/25/2008 prepared by NI, edited by MAF Fundamentals of Characters and Strings A string can also be defined by specifying the individual characters:
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern