0% found this document useful (0 votes)
2 views

Lecture - 7

The document provides an overview of strings in C++, detailing the two types: null-terminated strings (C-strings) and the string class. It explains how to declare character arrays for strings, input/output operations using cin and cout, and highlights the importance of handling string lengths and null characters. Additionally, it lists common string manipulation functions and includes exercises for practicing string handling in C++.

Uploaded by

umarhadi10109
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lecture - 7

The document provides an overview of strings in C++, detailing the two types: null-terminated strings (C-strings) and the string class. It explains how to declare character arrays for strings, input/output operations using cin and cout, and highlights the importance of handling string lengths and null characters. Additionally, it lists common string manipulation functions and includes exercises for practicing string handling in C++.

Uploaded by

umarhadi10109
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Programming Fundamentals Strings

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.

Lecturer : Mohsin Hasan Huseein 1


Programming Fundamentals Strings

Input / output strings:


The cout function can be used for printing out a string. And cin can be used to
receive a string from the keyboard, as shown in the following example :

#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 .

Lecturer : Mohsin Hasan Huseein 2


Programming Fundamentals Strings

#include <iostream>
main()
{
char name[20];
gets(name);
puts(name);
}

More about Strings:


In what way are character arrays different than numeric arrays? Can elements in a
character array be accessed in the same way as the elements of a numeric array?
Do I need to take any special care of ‘\0’? , let us settle some of these issues right
away with the help of sample programs.

// print array elements char by char


main( )
{
char name[6]= "Ahmed";
int i;
for(i=0;i<=4;i++)
cout<<name[i];
}

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];
}

Lecturer : Mohsin Hasan Huseein 3


Programming Fundamentals Strings

Standard Library String Functions:


C/C++ supports a wide range of functions that manipulate null-terminated strings.
The most common are:

Function Name Description

strcpy(s1, s2) Copies s2 into s1.

strcat(s1, s2) Concatenates s2 onto the end of s1.

strlen(s1) Returns the length of s1.

Returns 0 if s1 and s2 are the same; less than 0 if


strcmp(s1, s2)
s1<s2; greater than 0 if s1>s2.
Compares two strings without regard to case ("i"
strcmpi(s1, s2)
denotes that this function ignores case)

strlwr(st) Converts a string st to lowercase

Strupr(st) Converts a string st to uppercase

Work Sheet (1 )

Write a user defined functions for each of the slandered functions listed in
the above table.

Lecturer : Mohsin Hasan Huseein 4


Programming Fundamentals Strings

Work Sheet (2)

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.

Lecturer : Mohsin Hasan Huseein 5

You might also like