Ch. 11: Characters and Strings

Ch. 11: Characters and Strings - Characters and Strings...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
Characters and Strings Outline Characters Character Input and Output Character Handling Strings String Input and Output String/Numerics Convesion String Manipulation Memory Functions in the String-Handling Library The main() Function and Command-line Arguments Extensions in Ch Handling Command Line Arguments Using __argc and __argv. String Type string_t as a First-Class Object
Background image of page 1

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

View Full DocumentRight Arrow Icon
Characters – Characters such as letters and punctuation are stored as integers according to a certain numerical code, such as ASCII code that ranges from 0 to 127 and only requires 7 bits to represent it. – Character constant is an integral value represented as a character in single quotes. ‘a' represents the integer value of a . If ASCII code is used to represent characters, the ASCII value for ‘a’ is 97. – Using char to declare a character variable. char c = ‘a’; // the same as char c = 97;
Background image of page 2
Character Input and Output Functions in <stdio.h> 1) int getchar(void); Reads the next character from the standard input. 2) int putchar(int c); Print the character stored in c 3) int scanf(const char *, …); with conversion specifier c. char c; scanf(“%c”, &c); 4) int printf(char *, …); with conversion specifier c. char c=‘a’; printf(“%c”, c);
Background image of page 3

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

View Full DocumentRight Arrow Icon
Example: /* File: charget.c */ #include <stdio.h> int main() { char c; printf("Please input a character\n"); c = getchar(); printf("ASCII c = %d, c = %c\n", c, c); putchar(c); printf("\n"); printf("Note: ASCII '\\n' = %d, '\\n' = %c\n", '\n', '\n'); /* obtain the character '\n' */ c = getchar(); printf("ASCII c = %d, c = %c\n", c, c); putchar(c); return 0; }
Background image of page 4
Interactive Execution: Please input a character a ASCII c = 97, c = a a Note: ASCII ‘\n’ = 10, ‘\n’ = ASCII c = 10, c =
Background image of page 5

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

View Full DocumentRight Arrow Icon
Character Handling – The header file ctype.h declares several functions to perform testing and manipulations of characters. – Each function has one argument of character ( int type). The value of argument shall be representable as an unsigned char or shall equal to the value of the macro EOF , defined in header file stdio.h . Each function returns a nonzero value for true or zero for false.
Background image of page 6
• Functions in <ctype.h> Function Prototype Function Description int isdigit(int c) Returns true if c is a digit and false otherwise. int isalpha(int c) Returns true if c is a letter and false otherwise. int isalnum(int c) Returns true if c is a digit or a letter and false otherwise. int isxdigit(int c) Returns true if c is a hexadecimal digit character and false otherwise. int islower(int c) Returns true if c is a lowercase letter and false otherwise. int isupper(int c) Returns true if c is an uppercase letter; false otherwise. int tolower(int c) If c is an uppercase letter, tolower returns c as a lowercase letter. Otherwise, tolower returns the argument unchanged. int toupper(int c)
Background image of page 7

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

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 66

Ch. 11: Characters and Strings - Characters and Strings...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online