Question

In: Computer Science

C++ Sort Vector of Strings (case-sensitive) Without using libraries, how would I arrange a vector like...

C++ Sort Vector of Strings (case-sensitive)

Without using libraries, how would I arrange a vector like this alphabetically?

vector words {"September", "test" "seven", "apple"}

The output should be: "apple, seven, September, test"

Solutions

Expert Solution

#include <iostream>
#include<string.h>
#include<bits/stdc++.h>
using namespace std;

int main()
{
    vector<string> array;
    string temp;
    int i,j;
    array.push_back("september");
    array.push_back("test");
    array.push_back("seven");
    array.push_back("apple");
  
  
    for(i=0;i<array.size();i++)
    {
        //temp=array[i];
        for(j=i+1;j<array.size();j++)
        {
            if(array[i]>array[j])
            {
                temp=array[i];
                array[i]=array[j];
                array[j]=temp;
            }
        }
    }
    cout<<array[0]<<endl;
    cout<<array[1]<<endl;
    cout<<array[2]<<endl;
    cout<<array[3]<<endl;
    return 0;
}

code:

output:

If you have any queries...please comment...Thank you...


Related Solutions

i have an array of strings, how do i sort them into a binary tree? C...
i have an array of strings, how do i sort them into a binary tree? C Program
I would like to integrate a bubble sort into this binary search in c ++ Thank...
I would like to integrate a bubble sort into this binary search in c ++ Thank you! #include <iostream> using namespace std; // Binary search algorith // f is the first , l is the last , t is the target int binarySearch(int stgrade[], int f, int l, int t) {         while (f <= l)         {             int m = f + (l - l) / 2;                 // Check if...
Change the code to sort list of strings without using any pre-defined functions Here is my...
Change the code to sort list of strings without using any pre-defined functions Here is my code: int n; String temp; Scanner in = new Scanner(System.in); System.out.print("Enter number of names you want to enter:"); n = in.nextInt(); String names[] = new String[n]; Scanner s1 = new Scanner(System.in); System.out.println("Enter all the names:"); for(int i = 0; i < n; i++){ names[i] = s1.nextLine(); } ***CHANGE THIS PART*** for (int i = 0; i < n; i++){ for (int j = i...
Using only core C++ (no special libraries, except STL vector or string if you want), write...
Using only core C++ (no special libraries, except STL vector or string if you want), write a C++ program that allows a user to input a string and (a) Checks if the expression is a valid polynomial. Parentheses or negation are not allowed. Spaces should be ignored. E.g., the following are valid i. n^2+2*n+5 ii. 2*n + 4.54* n^5 +4 +5*n and the following are invalid iii. n^3n iv. n^4.2 v. 5n vi. n^3 -3*n if an input is given...
need to write program on python, without using any other libraries like panda, numpy, etc. here...
need to write program on python, without using any other libraries like panda, numpy, etc. here is google link on csv file https://drive.google.com/file/d/1O3cC9JAPVkXSrddTR6RocpSV8NMHrmRx/view?usp=sharing There is a csv file, which is exel file, very big and what program should do: - Read a CSV file 'annual.csv' enterprise into a data structure - Count the number of rows and columns - Determine if the data contains empty values (should search in all rows) - Replace the empty values by 'NA' for strings,...
C++ How would I sort my output to evaluate it by sorting the column by it's...
C++ How would I sort my output to evaluate it by sorting the column by it's size? I didn't include my full program but here is the main.cpp where i'm supposed to sort the output by column size. //User Libraries #include <cstdlib> #include <ctime> #include <iostream> using namespace std; //User Libraries #include "Table.h" #include "Triangle.h" //Global Constants //Function Prototype void prntRow(RowAray *,int); void prntTab(Table *); void prntTri(Triangle *); //Execution Begins Here! int main(int argc, char** argv) { //Initialize the random...
How would I add a quickSort function to the below C++ code to sort the randomly...
How would I add a quickSort function to the below C++ code to sort the randomly generated numbers? #include <iostream> #include <cstdlib> #include <ctime> using namespace std; int i; int array[10]; int odd; int Max; int counter = 0; int main() { cout << "The 10 random elements are: "; cout << endl; srand ( time(0) ); for (int j = 0; j < 99; j++) { i = rand() % 100; if (i != i - 1) array[j] =...
Write a program that performs a merge-sort algorithm without using a recursion. Only using pointers. C++...
Write a program that performs a merge-sort algorithm without using a recursion. Only using pointers. C++ programming language; #include<iostream>
How would I make a bubble sort and an optimized bubble sort with the code given?...
How would I make a bubble sort and an optimized bubble sort with the code given? I also need to implement a timer into each sort and display runtime with the sorts. NODE.H _______________________________________________________________________________________________________ /* node.h */ /* two classes 1: node.h 2. singlylinkedlist.h nod1 (value + pointer) ---> node2 ---> node3 ---> |||| <--- node.h ^ | singlylinkedlist ----------------*node head; */ #ifndef NODE_H #define NODE_H #include <iostream> using namespace std; class Node {    friend class singlyLinkedList; public:   ...
I would like the answers for these case study questions showing clearly how they are calculated.  ...
I would like the answers for these case study questions showing clearly how they are calculated.   Issuer Face Value Coupon Rate     Rating Quoted Price $ Years Until Maturity Sinking Fund Call Period ABC Energy 1000 6% AAA 809.10 20 Yes 3 Yrs ABC Energy 1000 0% AAA 211.64 20 Yes N/A Trans Power 1000 10% AA 1025.00 20 Yes 5 Yrs Telco Utilities 1000 12% AA 1300.00 30 No 5 Yrs 1. One of Jill's best clients poses the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT