Question

write a C++ program that accepts N strings and sorts the strings in alphabetical order

write a C++ program that accepts N strings and sorts the strings in alphabetical order

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>
#include <vector>
#include <string>

using namespace std;

void sort(string arr[], int size) {
    string temp;
    for(int i = 0; i < size; ++i) {
        for(int j = 0; j < size-1; ++j) {
            if(arr[j] > arr[j+1]) {
                temp = arr[j];
                arr[j] = arr[j+1];
                arr[j+1] = temp;
            }
        }
    }
}

void print(string arr[], int size) {
    for(int i = 0; i < size; ++i) {
        cout << arr[i] << " ";
    }
    cout << endl;
}

int main() {
    int n;
    cout << "How many strings do you want to enter? ";
    cin >> n;
    string *arr = new string[n];
    cout << "Enter " << n << " strings: ";
    for (int i = 0; i < n; ++i) {
        cin >> arr[i];
    }
    sort(arr, n);
    cout << "Sorted strings are: ";
    print(arr, n);
    delete[] arr;
    return 0;
}
Add a comment
Know the answer?
Add Answer to:
write a C++ program that accepts N strings and sorts the strings in alphabetical order
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT