Methodological Instructions
Theme: String manipulation 1 lesson
Objective: 10.4.1.2 use procedures and functions for string manipulation
Assessment criteria
· can define string data; String and char;
· uses string functions to solve problems
· applies string procedures to solve problems
Basic Level:
From a grade 7–9 course, students have knowledge and programming skills.
Key words and phrases:
Use of appropriate subject specific terminology on the topic in the study of new content
String, processing strings, insert substring, delete symbols, string length determination, copying symbols, determining the position.
The function differs from the procedure in that the procedure..., while the function....
In order to determine the length of a string...
To insert a substring in a string, we apply…
I. Theory
The term string generally means an ordered sequence of characters, with a first character, a second character, and so on, and in most programming languages such strings are enclosed in either single or double quotes. In C++ the enclosing delimiters are double quotes. In this form the string is referred to as a string literal and we often use such string literals in output statements when we wish to display text on the screen for the benefit of our users. For example, the usual first C++ program displays the string literal "Hello, world!" on the screen with the following output statement:
cout << "Hello, world!" << endl;
However, without string variables about all we can do with strings is output string literals to the screen, so we need to expand our ability to handle string data. When we talk about strings in C++, we must be careful because the C language, with which C++ is meant to be backward compatible, had one way of dealing with strings, while C++ has another, and to further complicate matters there are many non-standard implementations of C++ strings. These should gradually disappear as compiler vendors update their products to implement the string component of the C++ Standard Library.
As a programmer, then, you must distinguish between the following three things:
Both the C-string library functions and the C++ string library functions are available to C++ programs. But, don't forget that these are two *different* function libraries, and the functions of the first library have a different notion of what a string is from the corresponding notion held by the functions of the second library. There are two further complicating aspects to this situation: first, though a function from one of the libraries may have a counterpart in the other library (i.e., a function in the other library designed to perform the same operation), the functions may not be used in the same way, and may not even have the same name; second, because of backward compatibility many functions from the C++ string library can be expected to work fine and do the expected thing with C-style strings, but not the other way around.
The last statement above might seem to suggest we should use C++ strings and forget about C-strings altogether, and it is certainly true that there is a wider variety of more intuitive operations available for C++ strings. However, C-strings are more primitive, you may therefore find them simpler to deal with (provided you remember a few simple rules, such as the fact that the null character must always terminate such strings), and certainly if you read other, older programs you will see lots of C-strings. So, use whichever you find more convenient, but if you choose C++ strings and occasionally need to mix the two for some reason, be extra careful. Finally, there are certain situations in which C-strings must be used.
To understand strings, you will have to spend some time studying sample programs. This study must include the usual prediction of how you expect a program to behave for given input, followed by a compile, link and run to test your prediction, as well as subsequent modification and testing to investigate questions that will arise along the way. In addition to experimenting with any supplied sample programs, you should be prepared to make up your own.
In the following examples we attempt to draw the distinction between the two string representations and their associated operations. The list is not complete, but we do indicate how to perform many of the more useful kinds of tasks with each kind of string. The left-hand column contains examples relevant to C-strings and the right-hand column shows analogous examples in the context of C++ strings.
C-strings (#include <cstring>) C++ strings (#include <string>)
=============================== ================================
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Declaring a C-string variable Declaring a C++ string object
----------------------------- -----------------------------
char str[10]; string str;
Initializing a C-string variable Initializing a C++ string object
-------------------------------- --------------------------------
char str1[11] = "Call home!"; string str1("Call home!");
char str2[] = "Send money!"; string str2 = "Send money!";
char str3[] = {'O', 'K', '\0'}; string str3("OK");
Last line above has same effect as:
char str3[] = "OK";
string str4(10, 'x');
Assigning to a C-string variable Assigning to a C++ string object
-------------------------------- --------------------------------
Can't do it, i.e., can't do this: string str;
char str[10]; str = "Hello";
str = "Hello!"; str = otherString;
Concatenating two C-strings Concatenating two C++ string objects
--------------------------- ------------------------------------
strcat(str1, str2); str1 += str2;
strcpy(str, strcat(str1, str2)); str = str1 + str2;
Copying a C-string variable Copying a C++ string object
--------------------------- ---------------------------
char str[20]; string str;
strcpy(str, "Hello!"); str = "Hello";
strcpy(str, otherString); str = otherString;
Accessing a single character Accessing a single character
---------------------------- ----------------------------
str[index] str[index]
str.at(index)
str(index, count)
Comparing two C-strings Comparing two C++ string objects
----------------------- --------------------------------
if (strcmp(str1, str2) < 0) if (str1 < str2)
cout << "str1 comes 1st."; cout << "str1 comes 1st.";
if (strcmp(str1, str2) == 0) if (str1 == str2)
cout << "Equal strings."; cout << "Equal strings.";
if (strcmp(str1, str2) > 0) if (str1 > str2)
cout << "str2 comes 1st."; cout << "str2 comes 1st.";
Finding the length of a C-string Finding the length of a C++ string object
-------------------------------- -----------------------------------------
strlen(str) str.length()
Output of a C-string variable Output of a C++ string object
----------------------------- -----------------------------
cout << str; cout << str;
cout << setw(width) << str; cout << setw(width) << str;
In what follows, keep in mind that cin ignores white space when reading a string, while cin.get(), cin.getline() and getline() do not. Remember too that cin.getline() and getline() consume the delimiter while cin.get() does not. Finally, cin can be replaced with any open input stream, since file input with inFile, say, behaves in a manner completely analogous to the corresponding behavior of cin. Analogously, in the output examples given immediately above, cout could be replaced with any text output stream variable, say outFile. In all cases, numCh is the maximum number of characters that will be read.
Input of a C-style string variable Input of a C++ string object
---------------------------------- ----------------------------
cin >> s; cin >> s;
cin.get(s, numCh+1);
cin.get(s, numCh+1,'\n');
cin.get(s, numCh+1,'x');
cin.getline(s, numCh+1); getline(cin, s);
cin.getline(s, numCh+1, '\n');
cin.getline(s, numCh+1, 'x'); getline(cin, s, 'x');
A useful naming convention for C-strings is illustrated by examples like
typedef char String80[81];
typedef char String20[21];
in which the two numbers in each definition differ by 1 to allow for the null character '\0' to be stored in the array of characters, but to *not* be considered as part of the string stored there. No analog to this naming convention is necessary for C++ strings, since for all practical purposes, each C++ string variable may contain a string value of virtually unlimited length.
П. Practical tasks
1. Write a program in C++ to display various
type or arithmetic operation using mixed data type.
Sample output:
Display arithmetic operations with mixed data
type :
5 + 7 = 12
3.7 + 8.0 = 11.7
5 + 8.0 = 13.0
5 - 7 = -2
3.7 - 8.0 = -4.3
5 - 8.0 = -3.0
5 * 7 = 35
3.7 * 8.0 = 29.6
5 * 8.0 = 40.0
5 / 7 = 0
3.7 / 8.0 = 0.5
5 / 8.0 = 0.6
2. Write a program in C++ to check
overflow/underflow during various arithmetical operation.
Sample Output:
Check overflow/underflow during various arithmetical
operation :
Range of int is [-2147483648, 2147483647]
Overflow the integer range and set in minimum
range : -2147483648
Increasing from its minimum range :
-2147483647
Product is :1
Underflow the range and set in maximum range :
2147483647
Decreasing from its maximum range : 2147483646
Product is : 0
3. Write a program in C++ to display the
operation of pre and post increment and decrement.
Sample Output:
Display the operation of pre and post increment
and decrement :
The number is : 57
After post increment by 1 the number is : 58
After pre increment by 1 the number is : 59
After increasing by 1 the number is : 60
After post decrement by 1 the number is : 59
After pre decrement by 1 the number is :
58
After decreasing by 1 the number is : 57
4. Write a program in C++ to formatting the output.
Sample Output:
Formatting the output :
The value of pi : 3.1416
The value of pi 4 decimal place of total width 8
: | 3.1416|
The value of pi 4 decimal place of total width
10 : | 3.1416|
The value of pi 4 decimal place of total width 8
: |--3.1416|
The value of pi 4 decimal place of total width
10 : |----3.1416|
The value of pi in scientific format is :
3.1416e+00
Status in number : 0
Status in alphabet : false
5. Write a program in C++ to print the result
of the specified operations.
Sample Output:
Print the result of some specific operation :
Result of 1st expression is : 23
Result of 2nd expression is : 5
Result of 3rd expression is : 12
Result of 4th expression is : 3
Visual Aids and Materials.
1. Slides
2. Procedures and functions of working with strings: http://pas1.ru/stringfunction
3. http://purecodecpp.com/archives/920
4. http://cs.stmarys.ca/~porter/csc/ref/c_cpp_strings.html
5. https://www.w3resource.com/cpp-exercises/basic/index.php
6. https://web.stanford.edu/class/archive/cs/cs106b/cs106b.1132/handouts/08-C++-Strings.pdf
7. https://cal-linux.com/tutorials/strings.html
8. Скачано с www.znanio.ru
© ООО «Знанио»
С вами с 2009 года.