"how we write a program for" time swap" in c plus plus only with string"
Program Description
The program you are asking about is a time swap program written in C++ using strings. The purpose of the program is to swap the hours and minutes of a given time.
Program Steps:
- Include the necessary header files:
#include <iostream>
#include <string>
- Define the main function:
int main() {
// code goes here
return 0;
}
- Declare the variables to store the time:
std::string time;
std::string hours;
std::string minutes;
- Get the input for the time from the user:
std::cout << "Enter the time in HH:MM format: ";
std::cin >> time;
- Extract the hours and minutes from the input time:
hours = time.substr(0, 2);
minutes = time.substr(3, 2);
- Swap the hours and minutes:
std::string swappedTime = minutes + ':' + hours;
- Print the swapped time:
std::cout << "Swapped Time: " << swappedTime << std::endl;
Complete Program Example:
#include <iostream>
#include <string>
int main() {
std::string time;
std::string hours;
std::string minutes;
std::cout << "Enter the time in HH:MM format: ";
std::cin >> time;
hours = time.substr(0, 2);
minutes = time.substr(3, 2);
std::string swappedTime = minutes + ':' + hours;
std::cout << "Swapped Time: " << swappedTime << std::endl;
return 0;
}
This program prompts the user to enter a time in the format HH:MM, extracts the hours and minutes, swaps them, and then prints the swapped time.