You are responsible for the development of an alphabetical string sorting progra
ID: 3702606 • Letter: Y
Question
You are responsible for the development of an alphabetical string sorting program in C++. Your program should prompt the user to enter some words and then sort the words in alphabetical order and display them. Sort them alphabetically. You can also store the words entered by the user in an array of strings with predefined size or a dynamic array. Dynamically define the array also.
A sample run of your program should look like this:
Enter a word: laurie
Enter a word: dinesh
Enter a word: Erlich
Enter a word: Richard
Enter a word: Jian
Enter a word: 0
Your sorted list is: dinesh Erlich Jian laurie Richard
-Store the words entered by the user in an array of strings (each element of the array is of type string).
-The maximum number of words for which the program needs to work must be set using the following constant (a global constant at the top of the program):
const int NUM_WORDS = 10; Then, throughout the program wherever this size is needed, the constant must be used rather than a literal number.
-You can assume that each word entered by the user will not contain any spaces.
- The program must stop prompting for further input if the maximum number of words is reached, or if the user enters "0" as one of the words. If the user enters "0" to end the input sequence, then the "0" must not be counted as one of the words sorted and later displayed.
- Sorting of the words must be done using the bubble sorting method.
-The sorting method must be implemented in a function that takes as input an array of strings and the size (number of words) in the array. This function must sort the words from "smallest" to "largest" (you can use the compare function), which corresponds to alphabetical order if the strings are of letters.
-For true alphabetical sorting: you will have to account for upper- or lower-case letters. The upper-case and lower-case versions of a letter should be treated as if they are equals.
-Prompt the user for the number of words they would like to enter (no need to declare the constant NUM_WORDS).
-Dynamically allocate an array of strings (each element of the array is of type string) of that size.
-The program must stop prompting for further input once the maximum number of words is reached
-There is no need for the user to type "0" to indicate that they are finished. You have already asked them how many words they want to type, so that is how many words they will type.
-For true alphabetical sorting: you will have to account for upper- or lower-case letters. The upper-case and lower-case versions of a letter should be treated as if they are equals.
-Use actual variable whole variable names. For example, int something_1;
-Don't use #include < bits/stdc++.h > if you don't have too because I don't understand how that works.
-The Output of the alphabetically sorted names or words must be printed out on one line. For example: dinesh Erlich Jian laurie Richard
-This Output is unacceptable:
dinesh
Erlich
Jian
laurie
Richard
Explanation / Answer
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
using namespace std;
int main()
{
clrscr();
const int NUM_WORDS = 10; //initializing maximum number of words
char array[20][20], temp[20];
int i, j,num;
cout<<"Enter number of strings you need to sort: ";
cin>>num;
if(num<=NUM_WORDS) //check entered count is less than or equal to maximum number of words if yes proceed further
{
//loop for receiving words.words will be stored in array.
for(i=0; i<num; i++)
{
cout<<"Enter a word:";
cin>>array[i];
}
//checking zero is entered.if yes delete from array
for(i=0; i<num; i++)
{
if(array[i]!=0)
{
array[i] = array[i+1];
}
cout<<array[i];
}
//code for bubble sort
for(i=1; i<num-1; i++)
{
for(j=1; j<num-i-1; j++)
{
if(strcmp(array[j-1] , array[j]) >0)
{
strcpy(temp, array[j-1]);
strcpy(array[j-1], array[j]);
strcpy(array[j], temp);
}
}
}
//printing sorted array in alphabetical order
cout<<"sorted string in alphabetical order : ";
for(i=0; i<num; i++)
{
cout<<array[i]<<" ";
}
}
else
cout<<" Maximum number of words exceeds.Please enter maimum of 10 words";
return 0;
}
output will be
1.normal way
Enter number of strings you need to enter: 5
Enter a word:laurie
Enter a word:dinesh
Enter a word:Erlich
Enter a word:Richard
Enter a word:Jian
sorted string in alphabetical order :
dinesh Erlich Jian laurie Richard
2.num > maximum
Enter number of strings you need to enter:11
Maximum number of words exceeds.Please enter maimum of 10 words
entered number is greter than limit so cannot perform sorting
3.zero entered
Enter number of strings you need to enter: 6
Enter a word:laurie
Enter a word:dinesh
Enter a word:Erlich
Enter a word:Richard
Enter a word:Jian
Enter a word:0
sorted string in alphabetical order :
dinesh Erlich Jian laurie Richard
whenever zero is entered then program will delete zero and sort other strings
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.