C++ Programming
- Write a C++ Program to print a welcome text in a separate line.
- Write a Program in C++ to add two numbers.
- C++ program for addition of two numbers using functions.
- Add two numbers in C++ using class.
- Addition of two numbers using call by reference in C++
- C++ program to write a program to add two numbers using call by value.
- Swapping of two numbers in C++ without using third variable.
- Swap two numbers in C++ using function.
- C++ program to swap two numbers using class.
- C++ program to swap two numbers using call by value.
- C++ program to swap two numbers using pointers.
- Write a program to swap private data members of two classes using friend function.
- write a program to add data objects of two different classes using friend function.
- Threading in C++.
- C++ thread lambda
- C++ thread lambda with parameter
- C++ thread lambda pass by reference
- Singleton C++ example
Write a C++ program to change every letter in a given string with the letter following it in the alphabet (i.e. a becomes b, p becomes q, z becomes a).
Method 1
#include <iostream>
#include <string>
using namespace std;
int main() {
string str;
//Enter String
cout << "Enter any string: ";
getline(cin, str);
for (int i = 0; i < str.length(); i++) {
//Check if string is alphabet or not
if (isalpha(str[i])) {
if (str[i] == 'z' || str[i] == 'Z') {
str[i] -= 25;
}
else {
str[i]++;
}
}
}
cout << "New modified string will be: " << str << endl;
return 0;
}
Output
Enter any string: CodDesire
New modified string will be: DpeEftjsf
Method 2
#include <iostream>
#include <string>
using namespace std;
string changedLetters(const std::string& input) {
string result = input;
for (size_t i = 0; i < result.size(); ++i) {
// Check if a string is a alphabet or not
if (std::isalpha(result[i])) {
// For 'z' make it 'a'
if (result[i] == 'z') {
result[i] = 'a';
// For 'Z' make it 'A'
} else if (result[i] == 'Z') {
result[i] = 'A';
} else {
result[i] = result[i] + 1;
}
}
}
return result;
}
int main() {
string input;
cout << "Enter any string: ";
getline(std::cin, input);
string newString = changedLetters(input);
cout << "New string: " << newString << std::endl;
return 0;
}
Output
Enter any string: CodDesire
New string: DpeEftjsf