Introduction to Programming Lecture No. 17
Reading Material
Deitel & Deitel - C++ How to Program Chapter 5
5.29,5.30, 5.31, 5.32, 5.33, 5.34
Chapter 16
16.16 – 16.33 ( Pages 869 – 884)
Summary
· String Handling
· String Manipulation Functions
· Character Handling Functions
· Sample Program
· String Conversion Functions
· String Functions
· Search Functions
· Examples
· Exercises
String Handling
Deitel & Deitel - C++ How to Program Chapter 5
5.29,5.30, 5.31, 5.32, 5.33, 5.34
Chapter 16
16.16 – 16.33 ( Pages 869 – 884)
Summary
· String Handling
· String Manipulation Functions
· Character Handling Functions
· Sample Program
· String Conversion Functions
· String Functions
· Search Functions
· Examples
· Exercises
String Handling
We have briefly talked about 'Strings' in some of the previous lectures. In this lecture, you will see how a string may be handled. Before actually discussing the subject, it is pertinent to know how the things were going on before the evolution of the concept of 'strings'.
When C language and UNIX operating system were being developed in BELL Laboratories, the scientists wanted to publish the articles. They needed a text editor to publish the articles. What they needed was some easy mechanism by which the articles could be formatted and published. We are talking about the times when PCs and word processors did not exist. It may be very strange thing for you people who can perform the tasks like making the characters bold, large or format a paragraph with the help of word processors these days. Those scientists had not such a facility available with them. The task of writing article and turning into publishable material was mainly done with the help of typewriters. Then these computer experts decided to develop a program, which could help in the processing of text editing in an easy manner. The resultant efforts led to the development of a program for editing the text. The process to edit text was called text processing. The in- line commands were written as a part of the text and were processed on out put. Later, such programs were evolved in which a command was inserted for the functions like making the character bold. The effect of this command could be preview and then modified if needed.
Now coming to the topic of strings again, we will discuss in detail the in-built functions to handle the strings.
String Manipulation Functions
C language provides many functions to manipulate strings. To understand the functions, let’s consider building block (or unit) of a string i.e., a character. Characters are represented inside the computers in terms of numbers. There is a code number for each character, used by a computer. Mostly the computers use ASCII (American Standard Code for Information Interchange) code for a character to store it. This is used in the computer memory for manipulation. It is used as an output in the form of character. We can write a program to see the ASCII values.
We have a data type char to store a character. A character includes every thing, which we can type with a keyboard for example white space, comma, full stop and colon etc all are characters. 0, 1, 2 are also characters. Though, as numbers, they are treated differently, yet they are typed as characters. Another data type is called as int, which stores whole numbers. As we know that characters are stored in side computer as numbers so these can be manipulated in the same form. A character is stored in the memory in one byte i.e. 8 bits. It means that 28 (256) different combinations for different values can be stored. We want to ascertain what number it stores, when we press a key on the board. In other words, we will see what character will be displayed when we have a number in memory.
The code of the program, which displays the characters and their corresponding integer, values (ASCII codes) as under.
In the program the statement c = i ; has integer value on right hand side (as i is an int) while c has its character representation. We display the value of i and c. It shows us the characters and their integer values.
In the o
utput of this program, we will see integer numbers and their character representation. For example, there is a character, say white space (which we use between two words). It is a non-printable character and leaves a space. From the ASCII table, we can see that the values of a-z and A-Z are continuos. We can get the value of an alphabet letter by adding 1 to the value of its previous letter. So what we need to remember as a baseline is the value of ‘a’ and ‘A’.
Character Handling Functions
C language provides many functions to perform useful tests and manipulations of character data. These functions are found in the header file ctype.h. The programs that have character manipulation or tests on character data must have included this header file to avoid a compiler error. Each function in ctype.h receives a character (an int ) or EOF (end of file; it is a special character) as an argument. ctype.h has many functions, which have self-explanatory names.
Of these, int isdigit (int c) takes a simple character as its argument and returns true or false. This function is like a question being asked. The question can be described whether it is a character digit? The answer may be true or false. If the argument is a numeric character (digit), then this function will return true otherwise false. This is a useful function to test the input. To check for an alphabet (i.e. a-z), the function isalpha can be used. isalpha will return true for alphabet a-z for small and capital letters. Other than alphabets, it will return false. The function isalnum (is alphanumeric) returns true if its argument is a digit or letter. It will return false otherwise. All the functions included in ctype.h are shown in the following table with their description.
utput of this program, we will see integer numbers and their character representation. For example, there is a character, say white space (which we use between two words). It is a non-printable character and leaves a space. From the ASCII table, we can see that the values of a-z and A-Z are continuos. We can get the value of an alphabet letter by adding 1 to the value of its previous letter. So what we need to remember as a baseline is the value of ‘a’ and ‘A’.
Character Handling Functions
C language provides many functions to perform useful tests and manipulations of character data. These functions are found in the header file ctype.h. The programs that have character manipulation or tests on character data must have included this header file to avoid a compiler error. Each function in ctype.h receives a character (an int ) or EOF (end of file; it is a special character) as an argument. ctype.h has many functions, which have self-explanatory names.
Of these, int isdigit (int c) takes a simple character as its argument and returns true or false. This function is like a question being asked. The question can be described whether it is a character digit? The answer may be true or false. If the argument is a numeric character (digit), then this function will return true otherwise false. This is a useful function to test the input. To check for an alphabet (i.e. a-z), the function isalpha can be used. isalpha will return true for alphabet a-z for small and capital letters. Other than alphabets, it will return false. The function isalnum (is alphanumeric) returns true if its argument is a digit or letter. It will return false otherwise. All the functions included in ctype.h are shown in the following table with their description.
The functions tolower and toupper are conversion functions. The tolower function converts its uppercase letter argument into a lowercase letter. If its argument is other than uppercase letter, it returns the argument unchanged. Similarly the toupper function converts its lowercase letter argument into uppercase letter. If its argument is other than lowercase letter, it returns the argument without effecting any change.
Sample Program
Let’s consider the following example to further demonstrate the use of the functions of ctype.h. Suppose, we write a program which prompts the user to enter a string. Then the string entered is checked to count different types of characters (digit, upper and lowercase letters, white space etc). We keep a counter for each category of character entered. When the user ends the input, the number of characters entered in different types will be displayed. In this example we are using a function getchar(), instead of cin to get the input. This function is defined in header file as stdio.h. While carrying out character manipulation, we use the getchar() function. This function reads a single character from the input buffer or keyboard. This function can get the new line character ‘\n’ (the ENTER key) so we run the loop for input until user presses the ENTER key. As soon as the getchar() gets the ENTER key pressed (i.e. new line character ‘\n’), the loop is terminated. We know that, every C statement returns a value. When we use an assignment statement ( as used in our program c = getchar()), the value assigned to the left hand side variable is the value of the statement too. Thus, the statement (c = getchar()) returns the value that is assigned to char c. Afterwards, this value is compared with the new line character ‘\n’. If it is not equal inside the loop, we apply the tests on c to check whether it is uppercase letter, lowercase letter or a digit etc. In this program, the whole string entered by the user is manipulated character.
Following is the code of this program.
// Example: analysis of text using <ctype.h>
library
#include <iostream.h>
#include <stdio.h>
#include <ctype.h>
main()
{
char c;
int i = 0, lc = 0, uc = 0, dig = 0, ws
= 0, pun = 0, oth = 0;
cout << "Please enter a
character string and then press ENTER:
";
// Analyse text as it is input:
while ((c = getchar()) != '\n')
{
if (islower(c))
lc++;
else if (isupper(c))
uc++;
else if (isdigit(c))
dig++;
else if (isspace(c))
ws++;
else
if (ispunct(c))
pun++;
else
oth++;
}
// display the counts
of different types of characters
cout
<< "You typed:"<< endl;
cout<< "lower case letters
= "<< lc<< endl;
cout << "upper case letters
= " << uc <<endl;
cout<<
"digits = " << dig
<< endl;
cout<<
"white space = "<< ws
<< endl;
cout<<
"punctuation = "<<
pun<< endl;
cout<<
"others = "<< oth;
}
|
A sample output of the program is given below.
Please enter a character string and then press ENTER: Sixty Five = 65.00
You typed:
lower case letters =
7
upper case letters =
2
digits = 4
white space = 3
punctuation = 2
others = 0
|
String Conversion Functions
The header file stdlib.h includes functions, used for
different conversions. When we get input of a different type other than the
type of variable in which the value is being stored, it warrants the need to
convert that type into another type. These conversion functions take an
argument of a type and return it after converting into another type. These
functions and their description are given in the table below.
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.
|
Use of these functions:
While writing main () in a program, we can put them inside the parentheses of main. ‘int arg c, char ** arg v are written inside the parentheses. The arg c is the count of number of arguments passed to the program including the name of the program itself while arg v is a vector of strings or an array of strings. It is used while giving command line arguments to the program. The arguments in the command line will always be character strings. The number in the command line (for example 12.8 or 45) are stored as strings. While using the numbers in the program, we need these conversion functions.
Following is a simple program which demonstrate the use of atoi function. This program prompts the user to enter an integer between 10-100, and checks if a valid integer is entered.
//This program demonstrate the use of atoi function
# include <iostream.h>
# include <stdlib.h>
main( )
{
int
anInteger;
char myInt
[20]
cout
<< "Enter an integer between 10-100 : ";
cin
>> myInt;
if
(atoi(myInt) == 0)
cout
<< "\nError : Not a valid input"; // could be non
numeric
else
{
anInteger
= atoi(myInt);
if
(anInteger < 10 || anInteger > 100)
cout
<< "\nError : only integers between 10-100 are allowed!";
else
cout
<< "\n OK, you have entered " << anInteger;
}
}
|
The output of the program is as follows.
Enter an integer between 10-100 : 45.5
OK, you have entered 45
|
0 comments:
Post a Comment