Problem Statement:- Program to Sort names in alphabetical order.
Sample Input/Output:-
Data requirement:-
Input Data:- str, size
Output Data:- str
Additional Data:-i, j, temp or ele in Python
Program in C
Here is the source code of the C Program to Sort names in alphabetical order.
Output Data:- str
Additional Data:-i, j, temp or ele in Python
Here is the source code of the C Program to Sort names in alphabetical order.
Code:
#include<stdio.h>
#include<string.h>
int main()
{
char str[50][50],temp[50];
int i,size,j;
printf("Enter number of names:");
scanf("%d",&size);
printf("Enter %d names:",size);
for(i=0;i<size;i++)
scanf("%s",str[i]);
char ch;
for(i=0;i<size;i++)
{
for(j=i+1;j<size;j++)
{
if(strcmp(str[i],str[j])>0)
{
strcpy(temp,str[i]);
strcpy(str[i],str[j]);
strcpy(str[j],temp);
}}}
printf("After sorting names are:\n");
for(i=0;i<size;i++)
{
printf("%s\n",str[i]);
}
}
Input/Output:
Enter number of names:5
Enter 5 names:Daisy
Ram
Aadi
wan
Naava
After sorting names are:
Aadi
Daisy
Naava
Ram
wan
Enter 5 names:Daisy
Ram
Aadi
wan
Naava
After sorting names are:
Aadi
Daisy
Naava
Ram
wan
Program in C++
Here is the source code of the C++ Program to Sort names in alphabetical order.
Here is the source code of the C++ Program to Sort names in alphabetical order.
Code:
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
char str[50][50],temp[50];
int i,size,j;
cout<<"Enter number of names:";
cin>>size;
cout<<"Enter "<<size<<" names:";
for(i=0;i<size;i++)
cin>>str[i];
char ch;
for(i=0;i<size;i++)
{
for(j=i+1;j<size;j++)
{
if(strcmp(str[i],str[j])>0)
{
strcpy(temp,str[i]);
strcpy(str[i],str[j]);
strcpy(str[j],temp);
}}}
cout<<"After sorting names are:\n";
for(i=0;i<size;i++)
{
cout<<str[i]<<"\n";
}
}
#include<cstring>
using namespace std;
int main()
{
char str[50][50],temp[50];
int i,size,j;
cout<<"Enter number of names:";
cin>>size;
cout<<"Enter "<<size<<" names:";
for(i=0;i<size;i++)
cin>>str[i];
char ch;
for(i=0;i<size;i++)
{
for(j=i+1;j<size;j++)
{
if(strcmp(str[i],str[j])>0)
{
strcpy(temp,str[i]);
strcpy(str[i],str[j]);
strcpy(str[j],temp);
}}}
cout<<"After sorting names are:\n";
for(i=0;i<size;i++)
{
cout<<str[i]<<"\n";
}
}
Input/Output:
Enter number of names:4
Enter 5 names:tat
ali
som
gobi
After sorting names are:
Program in Java
Here is the source code of the Java Program to Sort names in alphabetical order.
Enter 5 names:tat
ali
som
gobi
After sorting names are:
ali
gobi
som
tat
Program in Java
Here is the source code of the Java Program to Sort names in alphabetical order.
Code:
import java.util.Scanner;
public class p27 {
public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
int i,size,j;
System.out.println("Enter number of names:");
size=cs.nextInt();
System.out.println("Enter "+size+" names:");
String str[]=new String[size+1];
for(i=0;i<size+1;i++)
str[i]=cs.nextLine();
String temp;
for(i=0;i<size+1;i++)
{
for(j=i+1;j<size+1;j++)
{
if(str[i].compareTo(str[j])>0)
{
temp=str[i];
str[i]=str[j];
str[j]=temp;
}}}
System.out.println("After sorting names are:");
for(i=0;i<size+1;i++)
{
System.out.println(str[i]);
}
cs.close();
}
}
public class p27 {
public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
int i,size,j;
System.out.println("Enter number of names:");
size=cs.nextInt();
System.out.println("Enter "+size+" names:");
String str[]=new String[size+1];
for(i=0;i<size+1;i++)
str[i]=cs.nextLine();
String temp;
for(i=0;i<size+1;i++)
{
for(j=i+1;j<size+1;j++)
{
if(str[i].compareTo(str[j])>0)
{
temp=str[i];
str[i]=str[j];
str[j]=temp;
}}}
System.out.println("After sorting names are:");
for(i=0;i<size+1;i++)
{
System.out.println(str[i]);
}
cs.close();
}
}
Input/Output:
Enter number of names:5
Enter 5 names:Daisy
Ram
Aadi
wan
Naava
After sorting names are:
Aadi
Daisy
Naava
Ram
wan
Program in Python
Here is the source code of the Python Program to Sort names in alphabetical order.
Code:
Enter 5 names:Daisy
Ram
Aadi
wan
Naava
After sorting names are:
Aadi
Daisy
Naava
Ram
wan
Program in Python
Here is the source code of the Python Program to Sort names in alphabetical order.
Code:
size=int(input("Enter number of names:"))
print("Enter ",size," names:")
str=[]
for i in range(size):
ele=input()
str.append(ele)
for i in range(size):
for j in range(i+1,size):
if (str[i]>str[j])>0:
temp=str[i]
str[i]=str[j]
str[j]=temp
print("After sorting names are:")
for i in range(size):
print(str[i])
print("Enter ",size," names:")
str=[]
for i in range(size):
ele=input()
str.append(ele)
for i in range(size):
for j in range(i+1,size):
if (str[i]>str[j])>0:
temp=str[i]
str[i]=str[j]
str[j]=temp
print("After sorting names are:")
for i in range(size):
print(str[i])
Input/Output:
Enter number of names:5
Enter 5 names:Daisy
Ram
Aadi
wan
Naava
After sorting names are:
Aadi
Daisy
Naava
Ram
wan
Enter 5 names:Daisy
Ram
Aadi
wan
Naava
After sorting names are:
Aadi
Daisy
Naava
Ram
wan
0 Comments
Please do not Enter any spam link in the comment box