ASCII is a 7-bit character set containing 128 characters.
It contains the numbers from 0-9, the upper and lower case English letters from A to Z, and some special characters.
isaplha(c)
isdigit(c)
islower(c)
isupper(c)
isspace(c)
. . .
Testing Functions
toupper(c)
tolower(c)
Conversion Functions
Suppose you want to go to a location, located at (X,Y) starting from origin (0,0). Your friend tells you a long way by telling the steps you should walk in each direction (N,W,E or S). Find the shortest path. Input & Output is a string. Refer Example:
Input: NNNEEWS
Output: NNE
Print a pattern of the following form.
AA
ABBA
ABCCBA
ABCDDCDA
Understanding the term "null-terminated"
char name[ ] {"Prateek"}
char name[10] {"Hello World"}
char name[10];
name = "Prateek"; //Error array name and string literal are location in memory
strcpy(name, "Prateek") // Allowed
char name[10]; //no null initialisation by default
char name[10];
char name[10]; //no null initialisation by default
Printing name can possibly print garbage letters until a null is found in the memory.
//Include the Header file
#include<cstring>
char str[100];
strcpy(str, "Hello"); //Copy
strcat(str," world"); // Concatenatio
cout << strlen(str) <<endl; // length
strcmp(str, "anothe string"); //comparison
#include<iostream>
using namespace std;
int main(){
cout<<"Hello World!";
return 0;
}
cstring proivides functions to operate on character arrays.
cctype provides us functions to operate on characters.
string class available in C++ Standard Template Library.
#include<string>
using namespace std;
string s1;
string s2{"Hello"};
string s3{s2};
string s4{"Hello",3}; //Hel
string s6{s2, 0,2} // He
string s5(3,'X'); //XXX
#include<string>
using namespace std;
string s1;
cin>>s1;
getline(cin,s1); //Reads and stores in s1
getline(cin,s1,'$'); //Reads and stores in s1
#include<string>
using namespace std;
string s1;
s1 = "Hello World";
string s2 = "Hi";
s2 = s1; //Overwrite
#include<string>
using namespace std;
string firstname = "Prateek"
string lastname = "Narang"
string fullname = firstname + " " + lastname;
#include<string>
using namespace std;
string firstname = "Prateek"
string lastname = "Narang"
firstname.at(0)
firstname[0]
#include<string>
using namespace std;
string firstname = "Prateek"
string lastname = "Narang"
for(char c:firstname){
cout << c <<endl;
}
for(int c:firstname){
cout << c <<endl;
}
==
!=
>
>=
<
<=
+
+=
string_object.substr( index, length);
string_object.find(other_string);
+=
Given marks of a student in 3 subjects - Physics, Chemistry and Maths - print their average.
Sample Input
Physics = 90
Maths = 75
Chemistry = 68
Sample Output
77.66
#include
directive tells the compiler to include the header file in the source code.
#include<iostream>
#incldue<algorithm>
#include<stack>
int main(){
....
}
#define
directive tells the compiler to create symbolic constants. The symbolic constant is called a macro.
All subsequent occurrences of macro in that source code will be replaced by its replacement text before the program is compiled.
#define PI 3.14
int main(){
//Area of Circle
int r = 5;
float area = PI*r*r;
return 0;
}
#define
directive tells the compiler to create symbolic constants. The symbolic constant is called a macro.
All subsequent occurrences of macro in that source code will be replaced by its replacement text before the program is compiled.
#define PI 3.14
#define AREA(l,b) (l*b)
int main(){
//Area of Rectangle
int area = AREA(10,5);
return 0;
}
The identifier is only used to identify an entity uniquely in a program at the time of execution whereas, a variable is a name given to a memory location, that is used to hold a value.
float calculateMarks(int p,int c, int m){
float average = (p+c+m)/3;
return average;
}
auto double int struct
break else long switch
case enum register typedef
char extern return union
const float short unsigned
continue for signed void
default goto sizeof volatile
do if static while
32 Keywords common in C++ and C
asm dynamic_cast namespace reinterpret_cast
bool explicit new static_cast
catch false operator template
class friend private this
const_cast inline public throw
delete mutable protected true
try typeid typename using
using virtual wchar_t
Some new Keywords in C++
int factorial(int n){
if(n<0){
cout << "Invalid Input";
return -1;
}
int ans = 1;
for(int i=1;i<=n;i++){
ans = ans*i;
}
return ans;
}
int main(){
//Execution stars from here
// Your Work
return 0;
}
int main(){
//logic
return 0;
}
int main(int argc,char *argv[]){
//logic
return 0;
}
using namespace cv;
To avoid data structure and function name conflicts with other libraries, OpenCV has its own namespace: cv.
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
cout<<"Welcome to Image Editing App";
Mat image = imread("Bob.jpg");
return 0;
}
Variable name: A label for a memory location
Value: The something that would be
stored in a variable
Storage: A place where data can be stored
Declaration: Announcing a variable (usually) at the beginning of a program
Naming convention: A set of rules about the names of variables
Assignment: Giving (setting) a variable a value
//Valid Names
double simple_interest;
int student_age;
float Student_percentile;
int prateek123;
//Invalid Names
int 123_age;
//Initialisation of Variable
float a = 10;
//Declaration
int b;
//Assignment
b = 20;
Boolean - boolean
Character - char
Integer – int
Floating Point – float
Double Floating Point – double
Several of the basic types can be modified using one or more of these type modifiers
signed
unsigned
short
long
int marks;
unsigned int roll_number;
unsigned long long int large_factorial;
short int age;
Constants are variables or values in programming language which cannot be modified once they are defined.
const float pi = 3.14
const int loan_period = 10
//Don't do this, Initialisation is Must for constants
const int x;
x = 5;
// Assignment is not allowed here
Using a const keyword
Using preprocessor directive
#define PI 3.14
#define LOAN_PERIOD 10
// Single If
int marks = 90;
if (marks > 80) {
cout << “Let's Party!”;
}
If-else block
// If-Else Block
int marks = 70;
if (marks > 80) {
cout << “Let's Party”;
}
else{
cout<< “Work hard next time“;
}
If-else-if-else block
// If-Else Block
int marks = 70;
if (marks > 80) {
cout << “Let's Party”;
}
else if(marks>60){
cout<<"Good Job";
}
else{
cout<<"Work hard next time";
}
Challenge 🔥
Electricity Bill Calculator : Given total consumption of a
household in units, write a program to estimate the total bill amount as per the table.
Units | Charges |
---|---|
1 to 100 units | Free |
100 to 200 units | Rs. 5/unit |
200 to 300 units | Rs.10/unit |
300+ units | Rs.12/unit |
switch(expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}
//Init
while(..condition is true ..){
//execute some stuff
//update
}
for(init;stopping_condition;update_statement){
//execute some stuff
}
for(int calories=0;calories<100;calories = calories+1){
//execute some stuff
cout<<"Run 1 step";
}
Given N, Print following pattern (For Example N = 5)
*
***
*****
*******
Given N, Print following pattern (For Example N = 5)
ABCDEEDCBA
ABCDDCBA
ABCCBA
ABBA
AA
Given N, Print all Prime Numbers upto N.
For Example - N = 10
Output - [2,3,5,7]
//init
do(){
//execute some stuff
}
while(condition);
//init
do(){
//execute some stuff
}
while(condition);
//Exit Controlled Loop
//init
//Entry Controlled Loop
while(condition){
//execute some stuff
}
int calories = 0;
while(calories<20){
if(calories==15){
cout<<"Stop the Workout";
break;
}
cout<<calories<<" ";
calories = calories + 1;
}
cout<<"Complete";
int calories = 0;
while(calories<20){
if(calories==15){
cout<<"Stop the Workout";
calories = calories + 1;
continue;
}
cout<<calories<<" ";
calories = calories + 1;
}
cout<<"Complete";
int calories = 0;
while(calories<20){
if(calories==15){
cout<<"Stop the Workout";
break;
}
cout<<calories<<" ";
calories = calories + 1;
}
cout<<"Complete";
Stop the loop when executed.
int calories = 0;
while(calories<20){
if(calories==15){
cout<<"Go to the next Step";
calories = calories + 1;
continue;
}
cout<<calories<<" ";
calories = calories + 1;
}
cout<<"Complete";
Control jumps to the beginning of the loop for next iteration
1
232
34543
4567654
567898765