This problem we are going to solve in c++ programming language , and we use here some basic string functions and Arrays of string .
About This Article
1-) Question
2-) source code
3-) Explanation
4-) Video Explanation
4-) Feedback
Question
Write down the names of 10 of your friends in a list and then sort those in alphabetically ascending order.
source code
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
int main()
{
string str[10],temp; //take temp variable for swapping to greater alphabet
cout<<"Enter names :"<<endl;
for(int i=0;i<10;i++) //run the loop till 10 and take into inputs
{
getline(cin,str[i]);
}
for(int i=0;i<10;i++)
//here we make j=i+1 because we want to check from the next cstring of i
for(int j=i+1;j<10;++j) //this loop is for compare with other characters
//if found any of the character smaller than the 1st char then swaped it if no then print immidiate
{
if(str[i]>str[j])
{
temp=str[i];
str[i]=str[j];
str[j]=temp;
}
}
cout<<"The names are :"<<endl;
for(int i=0;i<10;i++)
{
cout<<str[i]<<endl;
}
return 0;
}
Explanation
- First of all we add some required header files for performing our operations.
- Then we take a array of string and ask the user to input the names up to 10. And the we run a for loop for taking the string inputs .
- Then again we run the loop for checking which name is greater or no if found any of the character smaller than the 1st char then swapped it if no then print immediate.
- Then we again run a for loop and to print the names alphabetically in order.
video explanation
if you still have any we recommend you to watch out the video for better understanding about the logic building
Post a Comment
Post a Comment
Please do not spam in comment