Upload
bilal-ahmed
View
17
Download
1
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
Introduction to Introduction to ProgrammingProgramming
Lecture 17Lecture 17
String HandlingString Handling
String Manipulation FunctionsString Manipulation Functions
CharacterCharacter
ASCIIASCII
1 byte = 8 bits1 byte = 8 bits
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 ;
}}
}}
Header File Header File
ctype.hctype.h#include<ctype.h>#include<ctype.h>
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 )
isdigit ( ) Functionisdigit ( ) Function
int isdigit ( int c ) ;int isdigit ( int c ) ;
isalpha ( ) isalpha ( ) FunctionFunction
int isalpha ( int c ) ;int isalpha ( int c ) ;
isalnum ( ) isalnum ( ) FunctionFunction
int isalnum ( int c ) ;int isalnum ( int c ) ;
islower ( ) islower ( ) FunctionFunction
int islower ( int c ) ;int islower ( int c ) ;
isupper ( ) isupper ( ) FunctionFunction
int isupper ( int c ) ;int isupper ( int c ) ;
tolower ( ) tolower ( ) FunctionFunction
int tolower ( int c ) ;int tolower ( int c ) ;
toupper ( ) toupper ( ) FunctionFunction
int toupper ( int c ) ;int toupper ( int c ) ;
getchar ( ) ;getchar ( ) ;
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 ++ ;} }
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 )
atoi ( ) atoi ( ) FunctionFunction
char str [ ] ;char str [ ] ;
int age ;int age ;
age = atoi ( str ) ;age = atoi ( str ) ;
atof ( ) atof ( ) FunctionFunction
12.8912.89
atof ( ) atof ( ) FunctionFunction
char str [ ] ;char str [ ] ;
double dVar ;double dVar ;
dVar = atof ( str ) dVar = atof ( str ) ;;
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
String FunctionsString Functions
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.
int sum ;int sum ;int sum_even ; int sum_even ; int sum_odd ;int sum_odd ;
myStrcpy ( ) ;myStrcpy ( ) ;
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 ) ;
strcmp ( ) strcmp ( ) FunctionFunction
int strcmp (const char *s1 , const char *s2 ) int strcmp (const char *s1 , const char *s2 ) ;;
strncmp ( ) strncmp ( ) FunctionFunction
int strncmp ( const char *s1 , const char *s2 , int n ) int strncmp ( const char *s1 , const char *s2 , int n ) ;;
strlen ( ) strlen ( ) FunctionFunction
int strlen ( const char *s ) ;int strlen ( const char *s ) ;
Search FunctionsSearch Functions
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
This is a test This is a test
“ “ “ “ wrongwrong
‘ ‘ ‘ ‘ rightright
NULLNULL