0% found this document useful (0 votes)
27 views13 pages

Lab 11 Task 1

The document contains code for 4 tasks that involve string and number manipulation functions in C++. Task 1 defines a function to validate if a string is a valid number. Task 2 defines a function that performs arithmetic operations by reference on two integers. Task 3 defines a function to count the most frequent character in a string. Task 4 defines a recursive function to reverse an integer.

Uploaded by

hazyhazy9977
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views13 pages

Lab 11 Task 1

The document contains code for 4 tasks that involve string and number manipulation functions in C++. Task 1 defines a function to validate if a string is a valid number. Task 2 defines a function that performs arithmetic operations by reference on two integers. Task 3 defines a function to count the most frequent character in a string. Task 4 defines a recursive function to reverse an integer.

Uploaded by

hazyhazy9977
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Lab 11

Task 1
#include <iostream>

using namespace std;

bool isValid(string input)

bool dot = false;

bool digit = false;

for(int i=0; i<input.length(); i++)

bool flag = false;

string sub = input.substr(i,1);

for(int j=0; j<=9; j++)

string t = to_string(j);

if(sub==t)

flag = true;

digit = true;

if(!flag)

if(sub=="." && !dot)

dot = true;

else if(sub=="-" && i!=0)

return false;

}
}

return digit;

int main()

string input;

cout << "Enter a number: ";

cin>>input;

while(input!="quit")

if(isValid(input))

cout << "Input is valid :)"<<endl;

else

cout << "Input is not valid :("<<endl;

cout << "\nEnter a number: ";

cin>>input;

return 0;

Task 2:
#include <iostream>

using namespace std;

double alu(int &a, int &b)

double ratio = a;

ratio /= b;
int sum = a + b;

int product = a * b;

a = sum;

b = product;

return ratio;

int main()

int a,b,c,d;

cout << "Enter 2 integers: ";

cin >> a >> b;

c = a;

d = b;

double e = alu(c,d);

cout << "Sum of " << a << " and " << b << " is " << c << endl;

cout << "Product of " << a << " and " << b << " is " << d << endl;

cout << "Quotient of " << a << " and " << b << " is " << e << endl;

return 0;

Task 3:
#include <iostream>

using namespace std;

string counting(string input, int &frequency)

string out = "";

int max = 0;
for(int i='A'; i<='Z'; i++)

string c = "";

c += static_cast<char>(i);

int count = 0;

for(int j=0; j<input.length(); j++)

string sub = input.substr(j,1);

if(sub==c)

count++;

if(count>max)

out = c;

max = count;

else if(count==max)

out += c;

frequency = max;

return out;

int main()

string input,output;

int count;
cout << "Enter a string: ";

getline(cin,input);

output = counting(input,count);

cout << output.substr(0,1);

for(int i=1; i<output.length()-1; i++)

cout << ", " << output.substr(i,1);

if(output.length()>1)

cout << " and " << output.substr(output.length()-1,1);

cout << " occurred "<< count << " times in the input string";

return 0;

Task 4:
#include <iostream>

using namespace std;

int stringToInt(string str)

string first = str.substr(0,1);

for(int i=0; i<=9; i++)

string sdigit = to_string(i);

if(first==sdigit)

return i;

return -1;

}
bool isStrong(string pin)

string first = pin.substr(0,1);

bool weak = true;

for(int i=1; i<pin.length(); i++)

if(pin.substr(i,1)!=first)

weak = false;

break;

if(weak)

return false;

weak = true;

for(int i=1; i<pin.length(); i++)

int prev = stringToInt(pin.substr(i-1,1))+1;

if(pin.substr(i,1)!=to_string(prev))

weak = false;

break;

if(weak)

return false;
weak = true;

for(int i=1; i<pin.length(); i++)

int prev = stringToInt(pin.substr(i-1,1))-1;

if(pin.substr(i,1)!=to_string(prev))

weak = false;

break;

if(weak)

return false;

return true;

int main()

string pin;

cout << "Enter your PIN: ";

cin >> pin;

bool strong = isStrong(pin);

while(!strong)

cout << "Your PIN is not strong!" << endl;

cout << "Re-enter: ";

cin>> pin;

strong = isStrong(pin);

return 0;
}

Lab 12

#include <iostream>

#include <cmath>

using namespace std;

int main ()

int myarray[10];

for(int i=0; i<10; i++)

cin>>myarray[i];

int largest;

int secondlargest;

largest = myarray[0];

secondlargest = myarray[0];

int temp;

for(int i=0; i<10; i++)

if (myarray[i]>largest)

largest = myarray[i];
}

for(int i = 0; i<10 ; i++)

if (myarray[i]>secondlargest && myarray[i] != largest)

secondlargest = myarray[i];

cout << "largest is : "<<largest<<"\nsecond largest is : "<<secondlargest<<"\naverage is :


"<<(largest+secondlargest)/2<<endl;

Task 2

#include <iostream>

#include <cmath>

using namespace std;

int main ()

int myarray[10];

int myarray2[5];

for ( int i=0; i<10; i++)

cin>>myarray[i];

int sum=0;
for ( int i=0; i<10; i++)

sum = sum + myarray[i];

cout<<"mean is : "<<sum/10<<endl;

for ( int i=0; i<5; i++)

cin>>myarray2[i];

cout<<"medain is : "<<myarray[2]<<endl;

Task 3

#include <iostream>

#include <cmath>

using namespace std;

int main ()

string string1;

string string2;

string output="";

cout<<"Please input string1: "; cin>>string1;

cout<<"\n Please input string2: "; cin>>string2;


for (int i = 0; i<string2.length(); i++)

for(int j=0; j<string1.length(); j++ )

char a = string1[j];

if ( a == string2[i])

string2[i] = ' ';

for(int i =0; i<string2.length(); i++)

if(string2[i]!=' ')

output = output + string2[i];

cout<<"String 2 is : "<<output<<endl;

Task 4

#include <iostream>

#include <cmath>

using namespace std;


int powerof10(int x)

int r=1;

for( int i =0 ; i< x; i++)

r =r*10;

return r;

int reverse_Number( int number, int iteration , int mysize)

if (number == 0) {return 0;}

else

cout<<"extracted: "<<number%(10)<<endl;

cout<<"multiplied by: "<<powerof10(mysize-iteration)<<endl;

return ((number%(10)) * powerof10(mysize-iteration) + reverse_Number((number/10),


iteration+1,mysize));

int main ()

int num;

cout << "Enter number:\t";

cin >> num;


cout<<"\n The Reverse Number is: "<<reverse_Number(num,1,4)<<endl;

// cout<<powerof10(5)<<endl;}

You might also like