Write a menu driven program to perform following operations on strings (without using
inbuilt string functions):
a) Convert all lowercase characters to uppercase
b) Concatenate two strings.
c) Compare two strings
d) Reverse the string
Source code:
#include <iostream>
#include <string>
using namespace std;
void lower_upper(string &a)
{
// string temp;
for (unsigned int i = 0; i < a.length(); i++)
{
if(a[i] <123 && a[i] >96){
a[i] -= 32;
}
}
return;
}
string concatinate(string a, string b)
{
string temp;
temp = a + " " + b;
return temp;
}
void reverse(string &a)
{
string temp;
for (int i = a.length(); i >= 0; i--)
{
temp += a[i];
}
a = temp;
return;
}
void compare(string& a, string& b){
if (a != b)
{
cout << "strings are not same" << endl;
if (a > b)
{
cout << a << " is greater than " << b << endl;
}
else
{
cout << b << " is greater than " << a << endl;
}
}
else
{
cout << "both strings are same" << endl;
}
return;
}
int main()
{
string a;
string b;
int choice{0};
cout << "1. convert lowercase to uppercase\n2. concatinate 2 strings\n3. compare 2 strings\n4. reverse a string" << endl;
cout << "select choice(1-4): ";
cin >> choice;
switch (choice)
{
case 1:
cout << "enter the string to convert case of each letter: ";
cin >> a;
lower_upper(a);
cout << "Output with upper case: " << a << endl;
break;
case 2:
cout << "enter the first string to join to other string: ";
cin >> a;
cout << "second string: ";
cin >> b;
cout << "output: " << concatinate(a, b) << endl;
break;
case 3:
cout << "enter the first string to conpare your strings: ";
cin >> a;
cout << "second string: ";
cin >> b;
compare(a,b);
break;
case 4:
cout << "give string to reverse: ";
cin >> a;
reverse(a);
cout << "output: " << a << endl;
break;
default:
cout << "error! \nwrong input \ngive valid input " << endl;
break;
}
return 0;
}
0 Comments
Post a Comment