0% found this document useful (0 votes)
2 views

C++ assignment

C++

Uploaded by

luel2905
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

C++ assignment

C++

Uploaded by

luel2905
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

1. Write a C++ program to remove all characters in string except alphabets?

#include <iostream>

#include <string>

#include <cctype>

using namespace std;

int main() {

string input;

cout << "Enter a string: ";

getline(cin, input);

string result;

for (char c : input) {

if (isalpha(c)) {

result += c;

cout << "Result: " << result << endl;

return 0;

2. Write a C++ program to find the length of a string?


#include <iostream>

#include <string>

using namespace std;

int main() {

string input;

cout << "Enter a string: ";

1
getline(cin, input);

int length = input.length();

cout << "Length of the string: " << length << endl;

return 0;

3.Write a C++ program to concatenate two strings?

#include <iostream>

#include <string>

using namespace std;

int main() {

string str1 = "Hello, ";

string str2 = "World!";

string result = str1 + str2;

cout << "Concatenated string: " << result << endl;

return 0;

4. Write a C++ program to copy strings?


#include <iostream>

#include <string>

using namespace std;

int main() {

string str1, str2;

cout << "Enter the first string: ";

getline(cin, str1);

str2 = str1;

2
cout << "The copied string is: " << str2 << endl;

return 0;

5.Write a C++ program to store information of a student in a structure?

#include <iostream>

#include <string>

using namespace std;

struct Student {

string name;

int age;

float gpa;

};

int main() {

Student student;

cout << "Enter the student's name: ";

getline(cin, student.name);

cout << "Enter the student's age: ";

cin >> student.age;

cout << "Enter the student's GPA: ";

cin >> student.gpa;

cout << "Student Information:" << endl;

cout << "Name: " << student.name << endl;

cout << "Age: " << student.age << endl;

cout << "GPA: " << student.gpa << endl;

3
return 0;

6. Write a C++ program to find the size of int, float, char and double in your system?

#include <iostream>

using namespace std;

int main() {

cout << "Size of int: " << sizeof(int) << " bytes" << endl;

cout << "Size of float: " << sizeof(float) << " bytes" << endl;

cout << "Size of char: " << sizeof(char) << " bytes" << endl;

cout << "Size of double: " << sizeof(double) << " bytes" << endl;

return 0;

7. Write a C++ program to sort and print elements in Lexicographical order(Dictionary order)?

#include <iostream>

#include <string>

#include <algorithm>

#include <vector>

using namespace std;

int main() {

vector<string> words = {"apple", "zebra", "banana", "orange", "grape"};

sort(words.begin(), words.end());

cout << "Words in lexicographical order:" << endl;

for (const auto& word : words) {

cout << word << endl;

4
}

return 0;

8. Write a C++ program that converts Fahrenheit to Celsius and vice versa?

#include <iostream>

using namespace std;

double fahrenheitToCelsius(double fahrenheit);

double celsiusToFahrenheit(double celsius);

int main ()

double fahrenheit ;

cout<<"Enter the value of Fahrenheit:";

cin>>fahrenheit;

double celsius = fahrenheitToCelsius(fahrenheit);

cout << fahrenheit << " Fahrenheit is equal to " << celsius << " Celsius" << endl;

celsius;

cout<<"Enter the value of celsius:";

cin>>celsius;

fahrenheit = celsiusToFahrenheit(celsius);

cout << celsius << " Celsius is equal to " << fahrenheit << " Fahrenheit" << endl;

return 0;

double fahrenheitToCelsius(double fahrenheit)

5
return (fahrenheit - 32) * 5 / 9;

double celsiusToFahrenheit(double celsius)

return (celsius * 9 / 5) + 32;

9. Write a C++ program that converts Decimal number to Binary and vice versa?

#include <iostream>

#include <bitset>

using namespace std;

int main() {

int decimal = 10;

bitset<8> binary(decimal);

cout << decimal << " in binary is " << binary.to_string() << endl;

string binaryStr = "1010";

int decimalFromBinary = std::stoi(binaryStr, 0, 2);

cout << binaryStr << " in decimal is " << decimalFromBinary << endl;

return 0;

10.Write a C++ program that converts Uppercase to Lowercase and Lowercase to uppercase
letters?

#include <iostream>

#include <cctype>

using namespace std;

6
int main() {

char uppercaseLetter = 'A';

char lowercaseLetter = tolower(uppercaseLetter);

cout << uppercaseLetter << " in lowercase is " << lowercaseLetter << endl;

char lowercaseLetter2 = 'z';

char uppercaseLetter2 = toupper(lowercaseLetter2);

cout << lowercaseLetter2 << " in uppercase is " << uppercaseLetter2 << endl;

return 0;

You might also like