C++ Programming: String Manipulation and Function Definitions, Slides of C programming

Two c++ programs. The first program demonstrates string manipulation by modifying a string using substr, erase, and insert functions. The second program defines and calls two functions: function1 and function2, which perform subtraction and addition operations respectively. The main function initializes variables, calls the functions, and prints the results.

Typology: Slides

2021/2022

Uploaded on 09/27/2022

norris
norris 🇬🇧

4

(5)

212 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
#include <iostream>
using namespace std;
#include <iostream>
using namespace std;
int main (void)
{
string str1 = "What is the camel doing?";
string str2 = "fly";
string str3 = str1.substr(12, 5);
str1.erase(12, 5);
str1.insert(12, str2);
cout << str1 << endl;
// Remember strings are arrays as well.
for (int i = 0; i < str3.length(); i++)
cout << str3[i];
cout << endl;
}
pf3
pf4

Partial preview of the text

Download C++ Programming: String Manipulation and Function Definitions and more Slides C programming in PDF only on Docsity!

using namespace std; #include using namespace std; int main (void) { string str1 = "What is the camel doing?"; string str2 = "fly"; string str3 = str1.substr(12, 5); str1.erase(12, 5); str1.insert(12, str2); cout << str1 << endl; // Remember strings are arrays as well. for (int i = 0; i < str3.length(); i++) cout << str3[i]; cout << endl;

using namespace std; int a = 18; int b = 6; int function1(int a, int b) { return a - b; } int function2() { int c; c = a + b; return c; } int main (void) { int b = 12; int c = 0; a = function1(b, a); c = function2(); cout << "a: " << a << " b: " << b << " c: " << c << endl;

using namespace std; void F1(int males, int females); void F2(int &m, int &f); int main() { int Boys = 3, Girls = 5; F1(Boys, Girls); cout << "\nAfter calling F1, within main()"; cout << "\n\tBoys = " << Boys; cout << "\n\tGirls = " << Girls; F2(Boys, Girls); cout << "\nAfter calling F2, within main()"; cout << "\n\tBoys = " << Boys; cout << "\n\tGirls = " << Girls; } void F1(int b, int g) { b += 3, g += 4; cout << "\nF1"; cout << "\n\tBoys = " << b; cout << "\n\tGirls = " << g; } void F2(int &b, int &g) { b = b + 8, g = g + 5; cout << "\nF2"; cout << "\n\tBoys = " << b; cout << "\n\tGirls = " << g;