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
String is a collection of characters. There are two types of strings commonly used in C++ programming language:
Strings that are objects of string class (The Standard C++ Library string class)
C-strings (C-style Strings)
C-strings
In C programming, the collection of characters is stored in the form of arrays, this is also supported in C++ programming. Hence it's called C-strings.
C-strings are arrays of type char terminated with null character, that is, \0 (ASCII value of null character is 0).
How to define a C-string?
char str[] = "C++";
In the above code, str is a string and it holds 4 characters.
Although, "C++" has 3 character, the null character \0 is added to the end of the string automatically.
Alternative ways of defining a string
char str[4] = "C++";
char str[] = {'C','+','+','\0'};
char str[4] = {'C','+','+','\0'};
Like arrays, it is not necessary to use all the space allocated for the string. For example:
char str[100] = "C++";
Example 1: C++ String to read a word
C++ program to display a string entered by user.
#include <iostream>
using namespace std;
int main()
{
char str[100];
cout << "Enter a string: ";
cin >> str;
cout << "You entered: " << str << endl;
cout << "\nEnter another string: ";
cin >> str;
cout << "You entered: "<<str<<endl;
return 0;
}
Output
Enter a string: C++
You entered: C++
Enter another string: Programming is fun.
You entered: Programming
Notice that, in the second example only "Programming" is displayed instead of "Programming is fun".
This is because the extraction operator >> works as scanf() in C and considers a space " " has a terminating character.
Example 2: C++ String to read a line of text
C++ program to read and display an entire line entered by user.
#include <iostream>
using namespace std;
int main()
{
char str[100];
cout << "Enter a string: ";
cin.get(str, 100);
cout << "You entered: " << str << endl;
return 0;
}
Output
Enter a string: Programming is fun.
You entered: Programming is fun.
To read the text containing blank space, cin.get function can be used. This function takes two arguments.
First argument is the name of the string (address of first element of string) and second argument is the maximum size of the array.
In the above program, str is the name of the string and 100 is the maximum size of the array.
П. Practical tasks
1. Write a C program to find length of a string using loop. How to find length of a string without using in-built library function strlen()in C programming. Effective way to find length of a string without using strlen() function. How to find length of a string using strlen() string function.
Example
Input
Input string: I love programming.
Output
Length of string: 38
2. Write a C program to copy one string to another string using loop. C program to copy one string to another without using inbuilt library function strcpy(). How to copy one string to another without using inbuilt string library function in C programming. Effective logic to copy strings in C programming. How to copy one string to another string using strcpy() function in C program.
Example
Input
Input string: I love programming.
Output
Original string: I love programming.
Copied string: I love programming.
3. Write a C program to convert string from lowercase to uppercase string using loop. How to convert string from lowercase to uppercase using for loop in C programming. C program to convert lowercase to uppercase string using strupr() string function.
Example
Input
Input string: I love programming.
Output
I LOVE PROGRAMMING.
4. Write a C program to compare two strings using loop character by character. How to compare two strings without using inbuilt library function strcmp() in C programming. Comparing two strings lexicographically without using string library functions. How to compare two strings using strcmp() library function.
Example
Input
Input string1: Learn programming
Input string2: Learn programming
Output
Both strings are lexographically equal.
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. https://www.cs.fsu.edu/~myers/cop3014/hw/hwpractice.html
9. Theory https://codeforwin.org/2015/11/string-programming-exercises-and-solutions-in-c.html
10. https://www.programiz.com/cpp-programming/strings
11. Скачано с www.znanio.ru
Материалы на данной страницы взяты из открытых источников либо размещены пользователем в соответствии с договором-офертой сайта. Вы можете сообщить о нарушении.