33
Introduction to Introduction to Programming Programming Lecture 17 Lecture 17

CS201- Introduction to Programming- Lecture 17

Embed Size (px)

DESCRIPTION

Virtual University Course CS201- Introduction to Programming Lecture No 17 Instructor's Name: Dr. Naveed A. Malik Course Email: [email protected]

Citation preview

Page 1: CS201- Introduction to Programming- Lecture 17

Introduction to Introduction to ProgrammingProgramming

Lecture 17Lecture 17

Page 2: CS201- Introduction to Programming- Lecture 17

String HandlingString Handling

Page 3: CS201- Introduction to Programming- Lecture 17

String Manipulation FunctionsString Manipulation Functions

Page 4: CS201- Introduction to Programming- Lecture 17

CharacterCharacter

Page 5: CS201- Introduction to Programming- Lecture 17

ASCIIASCII

Page 6: CS201- Introduction to Programming- Lecture 17

1 byte = 8 bits1 byte = 8 bits

Page 7: CS201- Introduction to Programming- Lecture 17

Example 1Example 1#include<iostream.h>#include<iostream.h>main ( )main ( ){{

int i ;int i ;char c ;char c ;for( i = 0; i < 256 ; i ++ )for( i = 0; i < 256 ; i ++ ){{

c = i ;c = i ;cout << i << “\t” << c <<endl ;cout << i << “\t” << c <<endl ;

}}

}}

Page 8: CS201- Introduction to Programming- Lecture 17

Header File Header File

ctype.hctype.h#include<ctype.h>#include<ctype.h>

Page 9: CS201- Introduction to Programming- Lecture 17

ctype Functionsctype Functions

int isdigit ( int c )int isdigit ( int c )

int isalpha ( int c )int isalpha ( int c )

int isalnum ( int c )int isalnum ( int c )

int isxdigit ( int cint isxdigit ( int c ))

int islower ( int c )int islower ( int c )

int isupper ( int c )int isupper ( int c )

int tolower ( int c )int tolower ( int c )

int toupper ( int c )int toupper ( int c )

int isspace ( int c )int isspace ( int c )

int iscntrl ( int c )int iscntrl ( int c )

int ispunct ( int c )int ispunct ( int c )

int isprint ( int c )int isprint ( int c )

int isgraph ( int c )int isgraph ( int c )

Page 10: CS201- Introduction to Programming- Lecture 17

isdigit ( ) Functionisdigit ( ) Function

int isdigit ( int c ) ;int isdigit ( int c ) ;

Page 11: CS201- Introduction to Programming- Lecture 17

isalpha ( ) isalpha ( ) FunctionFunction

int isalpha ( int c ) ;int isalpha ( int c ) ;

Page 12: CS201- Introduction to Programming- Lecture 17

isalnum ( ) isalnum ( ) FunctionFunction

int isalnum ( int c ) ;int isalnum ( int c ) ;

Page 13: CS201- Introduction to Programming- Lecture 17

islower ( ) islower ( ) FunctionFunction

int islower ( int c ) ;int islower ( int c ) ;

Page 14: CS201- Introduction to Programming- Lecture 17

isupper ( ) isupper ( ) FunctionFunction

int isupper ( int c ) ;int isupper ( int c ) ;

Page 15: CS201- Introduction to Programming- Lecture 17

tolower ( ) tolower ( ) FunctionFunction

int tolower ( int c ) ;int tolower ( int c ) ;

Page 16: CS201- Introduction to Programming- Lecture 17

toupper ( ) toupper ( ) FunctionFunction

int toupper ( int c ) ;int toupper ( int c ) ;

Page 17: CS201- Introduction to Programming- Lecture 17

getchar ( ) ;getchar ( ) ;

Page 18: CS201- Introduction to Programming- Lecture 17

cout << “Please enter a character string then press enter”;cout << “Please enter a character string then press enter”;while ( ( c = getchar ( ) ) != ‘\n’ )while ( ( c = getchar ( ) ) != ‘\n’ ){{ if ( islower ( c ) )if ( islower ( c ) ) lc ++ ;lc ++ ; else if ( isupper ( c ) )else if ( isupper ( c ) ) uc ++ ;uc ++ ; else if (isdigit ( c ) )else if (isdigit ( c ) ) dig ++;dig ++; else if ( isspace ( c ) )else if ( isspace ( c ) ) ws ++ ;ws ++ ; else if ( ispunct ( c ) )else if ( ispunct ( c ) ) pun ++ ;pun ++ ; elseelse oth ++ ;oth ++ ;} }

Page 19: CS201- Introduction to Programming- Lecture 17

String Conversion String Conversion FunctionsFunctions

double atof ( char * str )double atof ( char * str )

int atoi (char * str )int atoi (char * str )

long atol (char * str )long atol (char * str )

Page 20: CS201- Introduction to Programming- Lecture 17

atoi ( ) atoi ( ) FunctionFunction

char str [ ] ;char str [ ] ;

int age ;int age ;

age = atoi ( str ) ;age = atoi ( str ) ;

Page 21: CS201- Introduction to Programming- Lecture 17

atof ( ) atof ( ) FunctionFunction

12.8912.89

Page 22: CS201- Introduction to Programming- Lecture 17

atof ( ) atof ( ) FunctionFunction

char str [ ] ;char str [ ] ;

double dVar ;double dVar ;

dVar = atof ( str ) dVar = atof ( str ) ;;

Page 23: CS201- Introduction to Programming- Lecture 17

int main (int agrc, char int main (int agrc, char **agrv )**agrv )

‘‘argcargc’ stands for a count of the number of ’ stands for a count of the number of argumentsarguments

‘‘argvargv’ stands for a vector of arguments’ stands for a vector of arguments

Page 24: CS201- Introduction to Programming- Lecture 17

String FunctionsString Functions

Page 25: CS201- Introduction to Programming- Lecture 17

String Manipulation Functions

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.

int strcmp( const char *s1, const char *s2 );

Compares string s1 to s2 Returns a negative number if s1 < s2, zero if s1 == s2 or a positive number if s1 > s2

int strncmp( const char *s1, const char *s2, size_t n );

Compares up to n characters of string s1 to s2 Returns values as above

char *strcat( char *s1, const char *s2 )

Appends string s2 to array s1. The first character of s2 overwrites the terminating null character of s1. The value of s1 is returned.

char *strncat( char *s1, const char *s2, size_t n )

Appends at most n characters of string s2 to array s1. The first character of s2 overwrites the terminating null character of s1. The value of s1 is returned.

Page 26: CS201- Introduction to Programming- Lecture 17

int sum ;int sum ;int sum_even ; int sum_even ; int sum_odd ;int sum_odd ;

myStrcpy ( ) ;myStrcpy ( ) ;

Page 27: CS201- Introduction to Programming- Lecture 17

String Manipulation String Manipulation FunctionsFunctions

char * strcpy (char *s1 , const char *s2 ) ;char * strcpy (char *s1 , const char *s2 ) ;

char * strncpy ( char *s1 , char *s2 , int n ) ;char * strncpy ( char *s1 , char *s2 , int n ) ;

char *char * strcat (char *s1 , char *s2 ) ;strcat (char *s1 , char *s2 ) ;

char *char * strncat ( char *s1 , char *s2 , int n ) ;strncat ( char *s1 , char *s2 , int n ) ;

Page 28: CS201- Introduction to Programming- Lecture 17

strcmp ( ) strcmp ( ) FunctionFunction

int strcmp (const char *s1 , const char *s2 ) int strcmp (const char *s1 , const char *s2 ) ;;

Page 29: CS201- Introduction to Programming- Lecture 17

strncmp ( ) strncmp ( ) FunctionFunction

int strncmp ( const char *s1 , const char *s2 , int n ) int strncmp ( const char *s1 , const char *s2 , int n ) ;;

Page 30: CS201- Introduction to Programming- Lecture 17

strlen ( ) strlen ( ) FunctionFunction

int strlen ( const char *s ) ;int strlen ( const char *s ) ;

Page 31: CS201- Introduction to Programming- Lecture 17

Search FunctionsSearch Functions

Page 32: CS201- Introduction to Programming- Lecture 17

Function prototype Function description

char *strchr( const char *s, int c );

Locates the first occurrence of character c in string s. If c is found, a pointer to c in s is returned. Otherwise, a NULL pointer is returned.

size_t strcspn( const char *s1, const char *s2 );

Determines and returns the length of the initial segment of string s1 consisting of characters not contained in string s2.

size_t strspn( const char *s1, const char *s2 );

Determines and returns the length of the initial segment of string s1 consisting only of characters contained in string s2.

char *strpbrk( const char *s1, const char *s2 );

Locates the first occurrence in string s1 of any character in string s2. If a character from string s2 is found, a pointer to the character in string s1 is returned. Other-wise, a NULL pointer is returned.

char *strrchr( const char *s, int c );

Locates the last occurrence of c in string s. If c is found, a pointer to c in string s is returned. Otherwise, a NULL pointer is returned.

char *strstr( const char *s1, const char *s2 );

Locates the first occurrence in string s1 of string s2. If the string is found, a pointer to the string in s1 is returned. Otherwise, a NULL pointer is returned.

char *strtok( char *s1, const char *s2 );

A sequence of calls to strtok breaks string s1 into “tokens”—logical pieces such as words in a line of text—separated by characters contained in string s2. The first call contains s1 as the first argument, and subsequent calls to continue tokenizing the same string contain NULL as the first argument. A pointer to the current token is returned by each call. If there are no more tokens when the function is called, NULL is returned.

Search FunctionsSearch Functions

Page 33: CS201- Introduction to Programming- Lecture 17

This is a test This is a test

“ “ “ “ wrongwrong

‘ ‘ ‘ ‘ rightright

NULLNULL