Universities Ranking-Test,-Notes-Tips-blog

HELLO I AM MUHAMMAD ALI AND STARTED BLOGGING SINCE 4 YEARS AGO.

Monday, 11 June 2018

Sorting String in Alphabetic Order ( whole stiring sentence)

The following program sort the whole string without OOP. No object is used in this program.
Dynamic memory allocation is used.  This program can sort whole string sentence in alphabetic order.

#include<iostream>
#include<string>
using namespace std;
int main()
{
 string *p;
 int n,i;
 cout<<"Enter the size of dynamic memory allocation:"<<endl;
 cin>>n;
 int temp;
  p=new string[n];
     cout<<"Enter words:"<<endl;
     for(int i=0;i<n;i++)
    {
    cin>>p[i];
    }
   
    cout<<"You entered:"<<endl;

        for(int i=0;i<n;i++)
        {
            cout<<p[i]<<endl;   
        }

    int index[100];   

        for(int i=0;i<=n;i++)
          {
              index[i]=i;
          }

        for(int i=0;i<n;i++)
        {
            for(int j=i+1;j<n;j++)
            {
                if(p[index[i]]>p[index[j]])
                {
                temp = index[i];
                index[i] = index[j];
                index[j] = temp;
                }
            }
        }

    cout<<"The alphbetic order is:"<<endl;
    for(int i=0;i<n;i++)
    {
    cout<<p[index[i]]<<endl;
    }   
   
return 0;
}

No comments:

Post a Comment