DOC PREVIEW
IUPUI CSCI 23000 - Characters and Strings

This preview shows page 1-2-3-4 out of 12 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 12 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 12 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 12 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 12 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 12 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Slide 1Character Handling LibrarySlide 3String Conversion FunctionsSlide 5Standard Input/Output Library FunctionsSlide 7String Manipulation FunctionsSlide 9Comparison FunctionsSearch Functions of the String Handling LibraryOther Functions of the String Handling LibraryDale RobertsDepartment of Computer and Information Science,Department of Computer and Information Science,School of Science, IUPUISchool of Science, IUPUICSCI 230 Characters and StringsFunctionsDale Roberts, LecturerComputer Science, IUPUIE-mail: [email protected] RobertsCharacter Handling LibraryCharacter Handling LibraryCharacter handling library Character handling library Includes functions to perform useful tests and Includes functions to perform useful tests and manipulations of character datamanipulations of character dataEach function receives a character (an Each function receives a character (an intint) or ) or EOFEOF as an as an argumentargumentThe following slide contains a table of all the The following slide contains a table of all the functions in functions in <ctype.h><ctype.h>Dale RobertsCharacter Handling LibraryCharacter Handling Library Prototype 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 ) If c is a lowercase letter, toupper returns c as an uppercase letter. Otherwise, toupper 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.Dale RobertsString Conversion FunctionsString Conversion FunctionsConversion functionsConversion functionsIn In <stdlib.h><stdlib.h> (general utilities library) (general utilities library)Convert strings of digits to integer and floating-Convert strings of digits to integer and floating-point valuespoint values Prototype Description double atof( const char *nPtr ) Converts the string nPtr to double. int atoi( const char *nPtr ) Converts the string nPtr to int. long atol( const char *nPtr ) Converts the string nPtr to long int. double strtod( const char *nPtr, char **endPtr ) Converts the string nPtr to double. long strtol( const char *nPtr, char **endPtr, int base ) Converts the string nPtr to long. unsigned long strtoul( const char *nPtr, char **endPtr, int base ) Converts the string nPtr to unsigned long.Dale Roberts1 /* Fig. 8.6: fig08_06.c2 Using atof */3 #include <stdio.h>4 #include <stdlib.h>56 int main()7 { 8 double d;910 d = atof( "99.0" );11 printf( "%s%.3f\n%s%.3f\n",12 "The string \"99.0\" converted to double is ", d,13 "The converted value divided by 2 is ", 14 d / 2.0 );15 return 0;16 }The string "99.0" converted to double is 99.000The converted value divided by 2 is 49.5001. Initialize variable1. Initialize variable2. Convert string and2. Convert string and assign to variableassign to variable3. Print3. PrintProgram OutputProgram OutputDale RobertsStandard Input/Output Library FunctionsStandard Input/Output Library FunctionsFunctions in Functions in <stdio.h><stdio.h>Used to manipulate character and string dataUsed to manipulate character and string dataFunction prototype Function description int getchar( void ); Inputs the next character from the standard input and returns it as an integer. char *gets( char *s ); Inputs characters from the standard input into the array s until a newline or end-of-file character is encountered. A terminating null character is appended to the array. int putchar( int c ); Prints the character stored in c. int puts( const char *s ); Prints the string s followed by a newline character. int sprintf( char *s, const char *format, ... ); Equivalent to printf, except the output is stored in the array s instead of printing it on the screen. int sscanf( char *s, const char *format, ... ); Equivalent to scanf, except the input is read from the array s instead of reading it from the keyboard.Dale Roberts1. Initialize variables1. Initialize variables2. Input2. Input3. Print3. Print3.1 Function definition (note 3.1 Function definition (note recursion)recursion)Program OutputProgram Output1 /* Fig. 8.13: fig08_13.c2 Using gets and putchar */3 #include <stdio.h>45 int main()6 { 7 char sentence[ 80 ];8 void reverse( const char * const );9 10 printf( "Enter a line of text:\n" );11 gets( sentence );1213 printf( "\nThe line printed backwards is:\n" );14 reverse( sentence );1516 return 0;17 }1819 void reverse( const char * const sPtr )20 { 21 if ( sPtr[ 0 ] == '\0' )22 return;23 else { 24 reverse( &sPtr[ 1 ] );25 putchar( sPtr[ 0 ] );26 }27 }Enter a line of text:Characters and Strings The line printed backwards is:sgnirtS dna sretcarahC reverse calls itself using substrings of the original string. When it reaches the '\0' character it prints using putcharDale RobertsString Manipulation FunctionsString Manipulation FunctionsString handling library has functions toString handling library has functions toManipulate string dataManipulate string dataSearch stringsSearch stringsTokenize stringsTokenize stringsDetermine string lengthDetermine string length Function prototype Function description char *strcpy( char *s1, const char *s2 ) Copies string s2 into array s1. The value of s1 is returned. char *strncpy( char *s1, const char *s2, size_t n ) Copies at most n characters of string s2 into array s1. The value of s1 is returned. char


View Full Document

IUPUI CSCI 23000 - Characters and Strings

Download Characters and Strings
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Characters and Strings and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Characters and Strings 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?