Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Table of Contents
1. Introduction 124
In the Lab-7, you have already learned how to deal with data consisting of sequence of characters (text).
Sequence of characters can be manipulated using character arrays (char [ ]). Actually, character array is
the way C language deals with the text data; therefore it is also called “C-String”.
The section 2 presents a table that outlines some major activities and tasks you will do as the part of this
lab. Table 1 also provides the estimated-time for each activity, which will help you to organize your tasks
well. Section 3 presents some of the learning objectives for this lab. Section 4 (“Concept Map”) discusses
and provides a comprehensive introduction of the topic. Section 5 lists the set of home-tasks you are
required to complete before this lab. Section 6 presents a “walkthrough task” that you will do as the first
practical activity during your lab. The walkthrough task has many small steps which you should follow as
directed in-order to complete the task and to get the desired output. After that, you will be ready to
work on some tasks on your own. The section 7 lists practice tasks for this purpose. As the part of
section 8, your lab instructor will give you some tasks at runtime and will evaluate those according to
the criteria mentioned in section 9. Section 10 lists some further reading links.
Note: Before coming to the lab, you are required to read Lab contents until section 5. You will
start your practical work from section 6 onward in the lab.
a) Lecture No. 21
b) Text Book: Computer Programming by D.S. Malik, second edition, pages: 446—472
2. Activity Time-boxing
4. Concept Map
To deal with sequence of characters, C++ provides its own specialized data-type based on string class. In
C++, std::string is the standard way to deal with the sequence of characters. A string variable can store
sequence of characters, letters, symbols (*, $, %), and non-keyboard characters such as escape
sequences [Lab-1]. Some of the examples of text that a string variable can store are: “Hello”,
“BC050102”, “I Love C++ Programming!”, “Grade:\n\tA”.
The string data-type provides many built-in functions to ease the tasks related to text manipulation.
Some of the example tasks which can be performed using built-in string functions are: text comparison,
combining two text values (concatenation), finding occurrence of some character or sequence of
characters in a text, and replacing/erasing some parts of the text. The ‘Appendix A’ shows some of the
most used string functions along their parameter specification/meaning. String is not the built-in data
type rather it is part of the C++ standard library. Therefore, to use string types you have to include the
corresponding library in the program using include statement:
#include <string>
C++ provides a specialized data-type (called string) to handle or manipulate text. A string type variable
can store same information but provides many useful methods/functions to process the text. C++’s
string type has several other advantages too over C-String (character array) as discussed below:
5.1.1 Problem description: Write pseudo-code of program that finds number of occurrences of a string
Department of Computer Science, P a g e | 125
CUST, 2018
Lab 12: Manipulating Sequence of Characters using C++ “string” data-type.
(based on at-least two characters) in another string (for example a sentence). The program should get
input in a string “str1” representing a sentence (terminated by”.”). Then, it should ask the user to enter
another string “str2”. In the end program should display the count or number representing total
occurrences of “str2” in “str1”.
Sample Output:
Total Vowels: 6
A: 1
E: 1
I: 2
O: 2
U: 0
5.3.2 Task-2
Write a C++ program that asks the user to enter a string. After that, the program should ask for another
string in a variable named “str_new”. Take another string input in the variable named “str_old”. In the
end, the program should replace the “str_new” with “str_old”. In the end the program prints the update
string.
Sample Input:
Please enter a string: Java programming is a great fun!
Enter word to replace: Java
Enter word to replace with: C++
Sample Output:
C++ programming is a great fun!
6.1 Tools
Visual Studio 2008.
paragraph (terminated by enter key). Your program should count total number of words in the
paragraph (using find function of string) and print that value on screen as follows:
You entered 20 words for the paragraph.
6.3.2 Compilation
After writing the code, compile your code according to the guidelines mentioned in Lab-1 (section 6.3.2)
7. Practice Tasks
This section will provide more practice exercises which you need to finish during the lab. You need to
finish the tasks in the required time. When you finish them, put these tasks in the folder specified by the
lab instructor.
Sample Inputs:
My name is Ali Raza
Sample Outputs:
1) String length: 19
2) azaR ilA si eman yM
3) My name is Ali Raza Hello World
4) 7
5) my name is ali raza hello world
Sample Inputs:
Enter a number: +92214352682
Sample Outputs:
This number belongs to Karachi
Do you want to enter another number (y/n): n
End of program. Bye!
7.4 Testing
Test Cases for Practice Task-1
Sample Input-1 Y<X is the same as X>= Y
Output-A Length: 26
Output-B Y=>X sa emas eht si X<Y
Output-C Y < X is the same as X>= Y Hello World
Output-D 6
Output-E Y<x is the same as x>=y
9. Evaluation Criteria
The evaluation criteria for this lab will be based on the completion of the following tasks. Each task is
assigned the marks percentage which will be evaluated by the instructor in the lab whether the student
has finished the complete/partial task(s).
10.2 Slides
The slides and reading material can be accessed from the folder of the class instructor available at
\\dataserver\jinnah$\
str.insert(X, CH) Inserts the character CH into string str starting at position X (integer value).
replace(X, N, str) Replace N number of characters in the calling string, with value of “str” string variable
starting at position X (integer value).