The ASCII Character Set Strings and Characters

The ASCII Character Set Strings and Characters

Characters and Strings 57:017, Computers in Introduction Engineering—Quick Review Fundamentals of Strings and Characters of C Character Strings Character Handling Library String Conversion Functions Standard Input/Output Library Functions String Manipulation Functions of the String Handling Library Comparison Functions of the String Handling Library Search Functions of the String Handling Library Other Functions of the String Handling Library Fundamentals of Strings Introduction and Characters z Introduce some standard library functions z Characters z Easy string and character processing z Character constant z Programs can process characters, strings, lines of z represented as a character in single quotes text, and blocks of memory z 'z' represents the integer value of z z stored in ASCII format ((yone byte/character) z These techniques used to make: z Strings z Word processors z Series of characters treated as a single unit z Can include letters, digits and special characters (*, /, $) z Page layout software z String literal (string constant) - written in double quotes z Typesetting programs z "Hello" z etc. z Strings are arrays of characters z The type of a string is char * i.e. “pointer to char” z Value of string is the address of first character The ASCII Character Set Strings and Characters z String declarations z Declare as a character array or a variable of type char * char color[] = "blue"; char *colorPtr = "blue"; z Remember that strings represented as character arrays end with '\0' z color has 5 eltlements z Inputting strings z Use scanf char word[10]; scanf("%s", word); z Copies input into word[] z Do not need & (because word is a pointer) z Remember to leave room in the array for '\0' 1 Chars as Ints Inputting Strings using scanf char word[10]; z Since characters are stored in one-byte ASCII representation, char *wptr; they can be considered as one-byte integers. … z C allows chars to be treated as ints and vice versa—e.g.: // This is OK as long as input string is less int main() { // than 10 characters in length int ivar; scanf((,“%s”, word); char cvar; ivar = ‘b’; // But this is not, since wptr does not have associated cvar = 97; // storage locations to hold the string printf (“ivar=%d cvar=‘%c’\n”, ivar, cvar); scanf(“%s”, wptr); } //This would be OK: wptr = word; ivar=98 cvar=‘a’ scanf(“%s”, wptr); Character Handling Library Character Handling Library Prototype Description z Character handling library int isdigit( int c ) Returns true if c is a digit and false otherwise. z Includes functions to perform useful tests and int isalpha( int c ) Returns true if c is a letter and false otherwise. manipulations of character data 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. z Each function receives a character (an int) or EOF as an int islower( int c ) Returns true if c is a lowercase letter and false otherwise. argument 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 z The following slide contains a table of all the returns the argument unchanged. int toupper( int c ) If c is a lowercase letter, toupper returns c as an uppercase letter. Otherwise, toupper functions in <ctype.h> returns the argument unchanged. int isspace( int c ) Returns true if c is a white-space character—newline ('\n'), space (' '), form feed ('\f'), carriage return ('\r'), horizontal tab ('\t'), or vertical tab ('\v')—and false otherwise int iscntrl( int c ) Returns true if c is a control character and false otherwise. int ispunct( int c ) Returns true if c is a printing character other than a space, a digit, or a letter and false otherwise. int isprint( int c ) Returns true value if c is a printing character including space (' ') and false otherwise. int isgraph( int c ) Returns true if c is a printing character other than space (' ') and false otherwise. 1 /* Fig. 8.2: fig08_02.c 2 Using functions isdigit, isalpha, isalnum, and isxdigit */ 33 isxdigit( '7' ) ? "7 is a " : "7 is not a ", 3 #include <stdio.h> 34 "hexadecimal digit", 4 #include <ctype.h> 5 35 isxdigit( '$' ) ? "$ is a " : "$ is not a ", 6 int main() 36 "hexadecimal digit", 7 { 37 isxdigit( 'f' ) ? "f is a " : "f is not a ", 8 printf( "%s\n%s%s\n%s%s\n\n", "According to isdigit: ", 38 "hexadecimal digit" ); 9 isdigit( '8' ) ? "8 is a " : "8 is not a ", "digit", 39 return 0; 10 isdigit( '#' ) ? "# is a " : 40 } 11 "# is not a ", "digit" ); 12 printf( "%s\n%s%s\n%s%s\n%s%s\n%s%s\n\n", According to isdigit: 8 is a digit 13 "According to isalpha:", # is not a digit 14 isalpha( 'A' ) ? "A is a " : "A is not a ", "letter", 15 isalpha( 'b' ) ? "b is a " : "b is not a ", "letter", According to isalpha: 16 isalpp(ha( '&' ) ? "& is a " : "& is not a ", "letter", A is a letter 17 isalpha( '4' ) ? "4 is a " : b is a letter 18 "4 is not a ", "letter" ); & is not a letter 19 printf( "%s\n%s%s\n%s%s\n%s%s\n\n", 4 is not a letter 20 "According to isalnum:", 21 isalnum( 'A' ) ? "A is a " : "A is not a ", According to isalnum: 22 "digit or a letter", A is a digit or a letter 23 isalnum( '8' ) ? "8 is a " : "8 is not a ", 8 is a digit or a letter # is not a digit or a letter 24 "digit or a letter", 25 isalnum( '#' ) ? "# is a " : "# is not a ", According to isxdigit: 26 "digit or a letter" ); F is a hexadecimal digit 27 printf( "%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n", J is not a hexadecimal digit 28 "According to isxdigit:", 7 is a hexadecimal digit 29 isxdigit( 'F' ) ? "F is a " : "F is not a ", $ is not a hexadecimal digit 30 "hexadecimal digit", f is a hexadecimal digit 31 isxdigit( 'J' ) ? "J is a " : "J is not a ", 32 "hexadecimal digit", 2 1 /* Fig. 8.6: fig08_06.c String Conversion Functions 2 Using atof */ 3 #include <stdio.h> 4 #include <stdlib.h> z Conversion functions 5 z In <stdlib.h> (general utilities library) 6 int main() 7 { z Convert strings of digits to integer and floating-point values 8 double d; 9 10 d = atof( "99.0" ); Prototype Description 11 printf( "%s%.3f\n%s%.3f\n", double atof( const char *nPtr ) Converts the string nPtr to double. 12 "The string \"99.0\" converted to double is ", d, 13 "The converted value divided by 2 is ", int atoi( const char *nPtr ) Converts the string to . nPtr int 14 d / 2.0 ); long atol( const char *nPtr ) Converts the string nPtr to long int. 15 return 0; double strtod( const char *nPtr, Converts the string nPtr to double. 16 } char **endPtr ) long strtol( const char *nPtr, Converts the string nPtr to long. The string "99.0" converted to double is 99.000 char **endPtr, int base ) The converted value divided by 2 is 49.500 unsigned long strtoul( const char Converts the string nPtr to unsigned *nPtr, char **endPtr, int base ) long. 1 /* Fig. 8.13: fig08_13.c 2 Using gets and putchar */ Standard Input/Output 3 #include <stdio.h> 4 void reverse( const char * const ); 5 int main() Library Functions 6 { 7 char sentence[ 80 ]; 8 z Functions in <stdio.h> 10 printf( "Enter a line of text:\n" ); z Used to manipulate character and string data 11 gets( sentence ); 12 13 printf( "\nThe line printed backwards is:\n" ); Function prototype Function description 14 reverse( sentence ); 15 int getchar( void ); Inputs the next character from the standard input and 16 return 0;; returns it as an integer. 17 } 18 char *gets( char *s ); Inputs characters from the standard input into the array 19 void reverse( const char * const sPtr ) s until a newline or end-of-file character is 20 { encountered. A terminating null character is appended 21 if ( sPtr[ 0 ] == '\0' ) reverse calls itself using substrings of to the array. 22 return; the original string. When it reaches the 23 else { character it prints using '\0' putchar int putchar( int c ); Prints the character stored in c. 24 reverse( &sPtr[ 1 ] ); int puts( const char *s ); Prints the string s followed by a newline character. 25 putchar( sPtr[ 0 ] ); 26 } int sprintf( char *s, Equivalent to printf, except the output is stored in 27 } const char *format, ... ); the array s instead of printing it on the screen. Enter a line of text: Characters and Strings int sscanf( char *s, const Equivalent to scanf, except the input is read from the char *format, ... ); array s instead of reading it from the keyboard. The line printed backwards is: sgnirtS dna sretcarahC A Non-recursive Version of reverse() String Manipulation Functions of the String Handling Library In the previous example, the function reverse() was written z String handling library has functions to recursively. Here is a non-recursive version of the function: z Manipulate string data z Search strings void reverse( const char * const sPtr ) { z Tokenize strings int i=0; z Determine string length /* fin d en d o f the s tr ing. Cou ld a lso use s tr len () */ while (sPtr[i] != '\0') i++; Function prototype Function description char *strcpy( char *s1, Copies string s2 into array s1.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    8 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us