codescracker
c++

C++ Program to Sort String



« Previous Program Next Program »

In this article, you will learn and get code to sort string(s) in C++. Here are the list of programs on sorting of string(s) entered by user at run-time:

Sort String in Ascending Order

This program sorts the string entered by user in ascending order. For example, if given string is bcabcz. Then the same string in ascending order is abbccz. Let's have a look at the program:

The question is, write a program in C++ to sort a string in ascending order. Here is its answer:

// C++ Program Sort String in Ascending Order
// ----codescracker.com----

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
int main()
{
    char str[200], ch;
    int i, j, len;
    cout<<"Enter the String: ";
    gets(str);
    len = strlen(str);
    for(i=0; i<len; i++)
    {
        for(j=0; j<(len-1); j++)
        {
            if(str[j]>str[j+1])
            {
                ch = str[j];
                str[j] = str[j+1];
                str[j+1] = ch;
            }
        }
    }
    cout<<"\nString in Ascending Order: "<<str;
    cout<<endl;
    return 0;
}

This program was build and run under Code::Blocks IDE. Here is its sample run:

C++ program sort string

Now supply the input say codescracker as string, then press ENTER key to sort it in ascending order and print the sorted string on output as shown in the snapshot given below:

sort string in ascending order c++

The dry run of above program with user input, codescracker goes like:

Note - To find the length of string manually, without using strlen() function, refer to find length of string article to get every required code to implement it.

Sort String in Descending Order

To sort string in descending order, just change the following condition of if (from previous) program:

if(str[j]>str[j+1])

With the code given below:

if(str[j]<str[j+1])

Note - Just a matter of greater than (>) and less than (<) sign, the whole program gets reversed.

Here is its sample run with same user input as of previous program's sample run, that is codescracker:

sort string in descending order c++

Sort String (Names) in Alphabetical Order

To sort strings (names) in alphabetical order in C++ programming, you have to ask from user to enter few names (strings). Then sort these entered strings or names in alphabetical order as shown in the program given below:

This program only receives 5 names from user at run-time. The question is write a program in C++ to sort 5 names entered by user in alphabetical order. Here is its answer:

// C++ Sort Strings (Names) in Alphabetical Order
// ----codescracker.com----

#include<iostream>
#include<string.h>
using namespace std;
int main()
{
    char nameAll[5][20], nameOne[20];
    int i, j;
    cout<<"Enter 5 Strings (Names): ";
    for(i=0; i<5; i++)
        cin>>nameAll[i];
    for(i=1; i<5; i++)
    {
        for(j=1; j<5; j++)
        {
            if(strcmp(nameAll[j-1], nameAll[j])>0)
            {
                strcpy(nameOne, nameAll[j-1]);
                strcpy(nameAll[j-1], nameAll[j]);
                strcpy(nameAll[j], nameOne);
            }
        }
    }
    cout<<"\nStrings (Names) in Alphabetical order:\n";
    for(i=0; i<5; i++)
        cout<<nameAll[i]<<endl;
    cout<<endl;
    return 0;
}

Here is the initial output produced by this program:

sort string in alphabetical order c++

Now supply any 5 strings (names). That is, enter first string or name say sumit and press ENTER key, then enter second string say gaurav and again press ENTER, and so on upto to enter fifth string (name) say anjani and press ENTER key this time to sort all the 5 names in alphabetical order. Now print strings (arranged in alphabetical order) as shown in the output given below:

c++ sort names in alphabetical order

Note - To compare two strings manually (without using strcmp() function), refer to compare two strings article to get every required things about the topic.

Note - To copy string manually (without using strcpy() function), refer to copy string to implement user-based code to do the same task.

Allow User to Define Size of Names

Now this program allows user to enter the size. That is how many names, user wants to enter. And then sort all those entered names in alphabetical order as shown here in the followig program:

// C++ Modified Version of Previous Program
// ----codescracker.com----

#include<iostream>
#include<string.h>
using namespace std;
int main()
{
    int i, j, tot;
    char nameAll[50][20], nameOne[20];
    cout<<"How many String to Enter (Max.50) ? ";
    cin>>tot;
    cout<<"Enter "<<tot<<" Strings (Names): ";
    for(i=0; i<tot; i++)
        cin>>nameAll[i];
    for(i=1; i<tot; i++)
    {
        for(j=1; j<tot; j++)
        {
            if(strcmp(nameAll[j-1], nameAll[j])>0)
            {
                strcpy(nameOne, nameAll[j-1]);
                strcpy(nameAll[j-1], nameAll[j]);
                strcpy(nameAll[j], nameOne);
            }
        }
    }
    cout<<"\nStrings (Names) in Alphabetical order:\n";
    for(i=0; i<tot; i++)
        cout<<nameAll[i]<<endl;
    cout<<endl;
    return 0;
}

Here is its sample run with user input 7 as size of names, and Maeran, Maeko, Magan, Madora, Maeves, Maeva and Mae as 7 names:

sort string name c++

Same Program in Other Languages

C++ Online Test


« Previous Program Next Program »



© Copyright 2021. All Rights Reserved.

CodesCracker