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

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

View Full Document Right Arrow Icon
Click to edit Master subtitle style 9/25/2008 prepared by NI, edited by MAF CSEB114: Principle of Programming Chapter 9: Character and String
Image of page 1

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

View Full Document Right Arrow Icon
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
Image of page 2
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.
Image of page 3

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

View Full Document Right Arrow Icon
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)
Image of page 4
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.
Image of page 5

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

View Full Document Right Arrow Icon
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);
Image of page 6
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
Image of page 7

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

View Full Document Right Arrow Icon
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
Image of page 8
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”;
Image of page 9

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

View Full Document Right Arrow Icon
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:
Image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    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.

    Student Picture

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

  • Left Quote Icon

    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.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern