Answer to Question #149485 in C++ for Likitha

Question #149485
Create a string array to store the name of 25 students .write the program 3access the name and swap the first letter of the name to last letter and display the names before swapping and after swapping
1
Expert's answer
2020-12-07T13:48:23-0500
#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;
	}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment