
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Print array of strings in sorted order without copying one string into another in C++
In the problem to print an array of strings in sorted order without copying one string into another, we need to sort the array of string. Here the programmer cannot copy a string into another string while sorting.
Let’s take an example to understand the concept better :
Example −
Input : {“Delhi”, “Hyderabad”, “Indore”, “Mumbai”, “Banglore”} Output : Banglore, Delhi, Hyderabad, Indore, Mumbai
Explanation − Lexicographically the ordering is done. So, Bangalore starting with B comes first and Mumbai starting with M comes last.
Now, let's try to derive a solution to our problem.
To solve the problem, we can create an array that stores the correct index of these string because actually changing positions of the string needs coping to be done. So this is one possible way to solve the problem.
We will use an index array and use the sorting technique to sort it and then print it. Here, we will use a selection sort technique that uses direct comparisons.
Example
Now let's create a program to illustrate the working −
#include <iostream> using namespace std; void sortedStringArray(string arr[], int n){ int stringIndex[n]; int i, j, min; for (i=0; i<n; i++) stringIndex[i] = i; for (i=0; i<n-1; i++){ min = i; for (j=i+1; j<n; j++){ if (arr[stringIndex[min]].compare(arr[stringIndex[j]]) > 0) min = j; } if (min != i){ int temp = stringIndex[min]; stringIndex[min] = stringIndex[i]; stringIndex[i] = temp; } } for (i=0; i<n; i++) cout << arr[stringIndex[i]] << ", "; } int main(){ string arr[] = {"Delhi", "Hyderabad", "Indore", "Mumbai", "Banglore"}; int n = 5; sortedStringArray(arr, n); return 0; }
Output
Banglore, Delhi, Hyderabad, Indore, Mumbai,
- Related Questions & Answers
- C Program for copying the contents of one file into another file
- Print all possible ways to convert one string into another string in C++
- What are the different ways of copying an array into another array in Java?
- Sort the array of strings according to alphabetical order defined by another string in C++
- Merging two sorted arrays into one sorted array using JavaScript
- Print Binary Tree levels in sorted order in C++
- Print all permutations in sorted (lexicographic) order in C++
- Copying the Queue elements to one-dimensional array in C#
- How to print one dimensional array in reverse order?
- Print sorted distinct elements of array in C language
- Print common characters of two Strings in alphabetical order in C++
- Python program to copy all elements of one array into another array
- How to add properties from one object into another without overwriting in JavaScript?
- Minimum number of deletions and insertions to transform one string into another using C++.
- Python Program to Print All Permutations of a String in Lexicographic Order without Recursion