#include <iostream>//for cin, cout
#include <string>//For string
using namespace std;
//prototype of functions
string swapLetters(string name);
void displayNames(string studentNames[25]);
//The start point of the program
int main() {
//a string array to store the name of 25 students
string studentNames[25];
//get 25 names from the user
for(int i=0;i<25;i++){
cout << "Enter name "<<(i+1)<<": ";
getline(cin,studentNames[i]);
}
//display the names before swapping
cout<<endl << "The names before swapping: "<<endl;
//display names
displayNames(studentNames);
//swap letters
for(int i=0;i<25;i++){
studentNames[i]=swapLetters(studentNames[i]);
}
//display the names after swapping
cout<<endl << "The names after swapping: "<<endl;
//display names
displayNames(studentNames);
//Delay
system("pause");
return 0;
}
//This function allows to swap the first letter of the name to last letter
string swapLetters(string name){
char tmp=name[name.length()-1];//get the last letter
name[name.length()-1]=name[0];//swap the letters
name[0]=tmp;//set the last letter as the first letter
return name;//return new name
}
//This function allows to display names
void displayNames(string studentNames[25]){
for(int i=0;i<25;i++){
cout <<(i+1)<<". "<< studentNames[i]<<endl;
}
}
Comments
Leave a comment