Lecture - 7
Lecture - 7
Introduction:
By far the most common use of the one-dimensional array is as a character string.
C++ supports two types of strings. The first is the null-terminated string, which is
a null-terminated character array. (A null character is zero and written as ‘\0’)
Thus a null-terminated string contains the characters that comprise the string
followed by a null. This is the only type of string defined by C, and it is still the
most widely used. Sometimes null-terminated strings are called C-strings. C++
also defines a string class, called string, which provides an object-oriented
approach to string handling. Here, null-terminated strings are examined.
The way a group of integers can be stored in an integer array, similarly a group of
characters can be stored in a character array. Character arrays are many a time also
called strings. Many languages internally treat strings as character arrays, but
somehow conceal this fact from the programmer. Character arrays or strings are
used by programming languages to manipulate text such as words and sentences.
When declaring a character array that will hold a null-terminated string, you need
to declare it to be one character longer than the largest string that it is to hold. For
example, to declare an array str that can hold a 10-character string, you would
write :
char str[11] ;
When you use a quoted string constant in your program, you are also creating a
null-terminated string. A string constant is a list of characters enclosed in double
quotes. For example, char st[12] = "hello there" ;
You do not need to add the null to the end of string constants manually the
compiler does this for you automatically.
#include <iostream>
main()
{
char name[20];
cin>>name;
cout<<name;
}
Without using for loop statement, the cin function fills in the characters typed at
keyboard into name array until the enter key is hit. Once enter is hit, cin places a
‘\0’ in the array.
While entering the string using cin we must be cautious about two things:
1- The length of the string should not exceed the dimension of the character
array. This is because the C++ compiler doesn’t perform bounds checking
on character arrays. Hence, if you carelessly exceed the bounds there is
always a danger of overwriting something important, and in that event,
you would have nobody to blame but yourselves.
2- cin is not capable of receiving multi-word strings. Therefore names such as
“ali ahmed” would be unacceptable. The way to get around this limitation
is by using the function gets( ). The usage of functions gets( ) and its
counterpart puts( ) is shown below.
The following example shows the input ouput strings using gets and puts .
#include <iostream>
main()
{
char name[20];
gets(name);
puts(name);
}
Can we write the while loop without using the final value 7? We can; because we
know that each character array always ends with a ‘\0’. Following program
illustrates this.
// print array elements char by char
main( )
{
char name[6]= "Ahmed";
int i;
for(i=0;name[i]!=’\0’;i++)
cout<<name[i];
}
Work Sheet (1 )
Write a user defined functions for each of the slandered functions listed in
the above table.
Q1: Write a C++ program to input the string S1 and print the index of certain
character in string.
Q2: Write a C++ program to input two strings S1 and S2 then insert the S2 into
certain position of S1.
Q3: Write a C++ program to input string S and find the number of special
characters in the string.
Q4: Write a C++ program to input the information of 100 books ( Title and
published year ) then print the information of most modern book .
Q5: Write a C++ program to input the information of 50 students (name and 7
marks for each student) then store the average of pass students in double array
average, and print the information of top student.
Q6: Write a C++ program to input the information of 100 books (Title and
published year ) then search about certain book by title in the list of books .
Q6: Write a C++ program to input the information of 100 books (Title and
published year ) then search about certain book by title in the list of books .
Q7: Write a C++ program to input the information of 100 person (name and age)
then sort them ascending by name.