C++ example: part2
String Length
To get the length of a string, use the use length() or size() functions
49 #include <iostream>
#include <string>
using namespace std;
int main() {
string txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
cout << "The length of the txt string is: " << [Link]();
return 0;
} // The length of the txt string is: 26
50 int main() {
string txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int size = [Link]();
cout << "The length of the txt string is: " <<size ;
return 0;
}
51 #include <iostream>
#include <string>
using namespace std;
int main() {
string txt = "";
cout << "The length of the txt string is: " << [Link]();
return 0;
}
52 #include <iostream>
#include <string>
using namespace std;
int main() {
string txt = "Ajloun ";
cout << "The length of the txt string is: " << [Link]();
return 0;
} // The length of the txt string is: 7
Access Strings
You can access the characters in a string by referring to its index number inside square
brackets [].
53 // This example prints the first character in myString:
int main() {
string myString = "Hello";
cout << myString[0];
return 0;
} // H
String indexes start with 0: [0] is the first character. [1] is the second character, etc.
This example prints the second character in myString:
54 int main() {
string myString = "Hello";
cout << myString[1];
return 0;
}// e
Change String Characters
To change the value of a specific character in a string, refer to the index number, and use
single quotes:
55 int main() {
string myString = "Hello";
myString[0] = 'J';
cout << myString;
return 0;
} // Jello
User Input Strings
use the getline() function to read a line of text. It takes cin as the first parameter, and the
string variable as second:
56 int main() {
string fullName;
cout << "Type your full name: ";
getline (cin, fullName); // input: Mohammad Ali
cout << "Your name is: " << fullName;
return 0;
} // Mohammad Ali
Omitting Namespace
57 int main() {
std::string greeting = "Hello";
std::cout << greeting;
return 0;
} // Hello
Math <cmath>
Math functions (from the <cmath> library)
C++ has many functions that allows you to perform mathematical tasks on numbers.
The max(x,y) function can be used to find the highest value of x and y.
And the min(x,y) function can be used to find the lowest value of x and y:
58 int main() {
cout << min(5, 10)<<"\n";
cout << max(5, 10);
return 0;
} // 5
10
59 #include <iostream>
#include <cmath>
using namespace std;
int main() {
cout << sqrt(64) << "\n";
cout << round(2.6) << "\n";
cout << floor(5.9) << "\n";
cout << ceil(5.9) << "\n";
cout << pow(5, 2) << "\n";
cout << fmod(5, 2) << "\n";
return 0;
}
8
3
5
6
25
1
Ex. int x=5, y=7;cout<<abs(x - y)
// 2
Programs below illustrate the swap() function.
60 int main() {
int x=9;
int y = 10;
swap(x, y);
cout <<x<<" "<<y;;
return 0;
}
Programs below illustrate the swap() function: using string variable
61 int main(){
string a = "school";
string b = "univerity";
cout << "Value of a before: " << a << endl;
cout << "Value of b before: " << b << endl;
swap(a, b);
cout << "Value of a now: " << a << endl;
cout << "Value of b now: " << b << endl;
return 0;
}
62 int main() {
int x = 10;
int y = 9;
cout << (x > y);
return 0;
} //1
63 int main() {
cout << (10 > 9);
return 0;
}// 1
64 int main() {
int x = 10;
cout << (x == 10);
return 0;
}// 1
65 int main() {
cout << (10 == 15);
return 0;
}// 1
66 int main() {
int myAge = 25;
int votingAge = 18;
cout << (myAge >= votingAge); // returns 1 (true), meaning 25 year olds are allowed
to vote!
return 0;
} // 1
Ex. Output "Old enough to vote!" if myAge is greater than or equal to 18. Otherwise
output "Not old enough to vote.":
67 int main() {
int myAge = 25;
int votingAge = 18;
if (myAge >= votingAge) {
cout << "Old enough to vote!";
} else {
cout << "Not old enough to vote.";
}
return 0;
}
Ex. Example: Program to Find Size of int, float, double and char? Using sizeof () function
68 int main()
{
cout << "Size of char: " << sizeof(char) << " byte" << endl;
cout << "Size of int: " << sizeof(int) << " bytes" << endl;
cout << "Size of float: " << sizeof(float) << " bytes" << endl;
cout << "Size of double: " << sizeof(double) << " bytes" << endl;
return 0;
}
Size of char: 1 byte
Size of int: 4 bytes
Size of float: 4 bytes
Size of double: 8 bytes
Program to Find ASCII Value of a Character?
69 int main()
{
char c;
cout << "Enter a character: ";
cin >> c;
cout << "ASCII Value of " << c << " is " << int(c);
return 0;
}
Type Conversion
Implicit Casting & explicit casting
70 int main (){
double x = 10.3;
int y;
y= int(x);
cout<<y;
return 0;
} // 10
71 int main() {
double x = 7.8;
int y= static_cast<int>(x);
cout <<y;
return 0;
} // 7
int main() {
double x = (7.8 + 7.5);
int y= int(x); // int y= static_cast<int>(x);
cout <<y;
return 0;
} // 15
72 int main (){
short a = 200;
int b;
b=a;
cout<<a<<" "<<b;
return 0;
} // 200 200
73 int main()
{
float f = 3.5;
// Implicit type case
// float to int
int a = f;
cout << "The Value of a: " << a;
// using static_cast for float to int
int b = static_cast<int>(f);
cout << "\nThe Value of b: " << b;
}
The Value of a: 3
The Value of b: 3
Operators Precedence اسبقية العوامل
+ addition
- subtraction
* multiplication
/ division
% modulus operator
74 int main() {
int num1 = 5 - 4 * 10;
int num2 = 5 - (3 * 5);
int num3 = (5 - 8) * 6;
cout << "num1 = " << num1 << endl;
cout << "num2 = " << num2 << endl;
cout << "num3 = " << num3 << endl;
return 0;
}
num1 = -35
num2 = -10
num3 = -18
75 cout << 3 * 7 - 6 + 2 * 5 / 4 + 6 ; // 23
76 cout << 2.5 * 3.6 - 4.50 ; // 4.5
77 cout << 5.4 * 2 - 7.6 + 18 / 2; // 12.2