SlideShare uma empresa Scribd logo
1 de 2
Generate an array that contains strings as elements. Sort this array in ascending order. (C++)
Solution
solution.cpp
#include <iostream> //header file for input output function
using namespace std; //it tells the compiler to link std arr2 space
#include <cstring> //header file for string
int main()
{ //main function
// declare two arrays arr2d arr1 with 1-Dimension
// and arr2 with 2-Dimension
char arr1[10], arr2[10][10];
// normal variables...
int i, j,noofstrings;
cout<<"Enter the number of strings required to sort ";
cin>>noofstrings;
// for loop statement
for(i=0; i<noofstrings; i++)
{
cout<<" Enter the strings of an array "<<(i+1)<<": ";
cin>>arr2[i];
}
for(i=0; i<noofstrings-1; i++)
for(j = i+1; j<noofstrings; j++)
//sorting string using strcmp()
if(strcmp(arr2[i], arr2[j])>0)
{
// strcpy - copy the strings...
// compare and swap...
strcpy(arr1, arr2[i]);
strcpy(arr2[i], arr2[j]);
strcpy(arr2[j], arr1);
}
cout<<" Sorted strings are: ";
for (i =0; i<noofstrings; i++)
cout<<" "<<arr2[i];
cout<<endl;
return 0;
}
output
sh-4.3$ main
Enter the number of strings required to sort 3
Enter the strings of an array 1: burningstar
Enter the strings of an array 2: rockstar
Enter the strings of an array 3: suddenstar
Sorted strings are:
burningstarockstar
rockstar
suddenstar

Mais conteúdo relacionado

Semelhante a Generate an array that contains strings as elements- Sort this array i.docx

1.1 Nested Loops – Lab3C.cppLoops often have loops inside .docx
1.1 Nested Loops – Lab3C.cppLoops often have loops inside .docx1.1 Nested Loops – Lab3C.cppLoops often have loops inside .docx
1.1 Nested Loops – Lab3C.cppLoops often have loops inside .docxchristiandean12115
 
C++ Programming Homework Help
C++ Programming Homework HelpC++ Programming Homework Help
C++ Programming Homework HelpC++ Homework Help
 
array, function, pointer, pattern matching
array, function, pointer, pattern matchingarray, function, pointer, pattern matching
array, function, pointer, pattern matchingShakila Mahjabin
 
Lecture 15_Strings and Dynamic Memory Allocation.pptx
Lecture 15_Strings and  Dynamic Memory Allocation.pptxLecture 15_Strings and  Dynamic Memory Allocation.pptx
Lecture 15_Strings and Dynamic Memory Allocation.pptxJawadTanvir
 
Lecture#9 Arrays in c++
Lecture#9 Arrays in c++Lecture#9 Arrays in c++
Lecture#9 Arrays in c++NUST Stuff
 
2 BytesC++ course_2014_c3_ function basics&parameters and overloading
2 BytesC++ course_2014_c3_ function basics&parameters and overloading2 BytesC++ course_2014_c3_ function basics&parameters and overloading
2 BytesC++ course_2014_c3_ function basics&parameters and overloadingkinan keshkeh
 
C++ FUNCTIONS-1.pptx
C++ FUNCTIONS-1.pptxC++ FUNCTIONS-1.pptx
C++ FUNCTIONS-1.pptxShashiShash2
 
I have question in c++ program I need the answer as soon as possible.docx
I have question in c++ program I need the answer as soon as possible.docxI have question in c++ program I need the answer as soon as possible.docx
I have question in c++ program I need the answer as soon as possible.docxdelciegreeks
 
httplinux.die.netman3execfork() creates a new process by.docx
httplinux.die.netman3execfork() creates a new process by.docxhttplinux.die.netman3execfork() creates a new process by.docx
httplinux.die.netman3execfork() creates a new process by.docxadampcarr67227
 
Lecture 3: Strings and Dynamic Memory Allocation
Lecture 3: Strings and Dynamic Memory AllocationLecture 3: Strings and Dynamic Memory Allocation
Lecture 3: Strings and Dynamic Memory AllocationVivek Bhargav
 
Library functions in c++
Library functions in c++Library functions in c++
Library functions in c++Neeru Mittal
 
Lecture 5Arrays on c++ for Beginner.pptx
Lecture 5Arrays on c++ for Beginner.pptxLecture 5Arrays on c++ for Beginner.pptx
Lecture 5Arrays on c++ for Beginner.pptxarjurakibulhasanrrr7
 
Synapse india dotnet development overloading operater part 3
Synapse india dotnet development overloading operater part 3Synapse india dotnet development overloading operater part 3
Synapse india dotnet development overloading operater part 3Synapseindiappsdevelopment
 

Semelhante a Generate an array that contains strings as elements- Sort this array i.docx (20)

7512635.ppt
7512635.ppt7512635.ppt
7512635.ppt
 
1.1 Nested Loops – Lab3C.cppLoops often have loops inside .docx
1.1 Nested Loops – Lab3C.cppLoops often have loops inside .docx1.1 Nested Loops – Lab3C.cppLoops often have loops inside .docx
1.1 Nested Loops – Lab3C.cppLoops often have loops inside .docx
 
C++ Programming Homework Help
C++ Programming Homework HelpC++ Programming Homework Help
C++ Programming Homework Help
 
array, function, pointer, pattern matching
array, function, pointer, pattern matchingarray, function, pointer, pattern matching
array, function, pointer, pattern matching
 
Arrays in C++
Arrays in C++Arrays in C++
Arrays in C++
 
Lecture 15_Strings and Dynamic Memory Allocation.pptx
Lecture 15_Strings and  Dynamic Memory Allocation.pptxLecture 15_Strings and  Dynamic Memory Allocation.pptx
Lecture 15_Strings and Dynamic Memory Allocation.pptx
 
Lecture#9 Arrays in c++
Lecture#9 Arrays in c++Lecture#9 Arrays in c++
Lecture#9 Arrays in c++
 
C++11 - STL Additions
C++11 - STL AdditionsC++11 - STL Additions
C++11 - STL Additions
 
2 BytesC++ course_2014_c3_ function basics&parameters and overloading
2 BytesC++ course_2014_c3_ function basics&parameters and overloading2 BytesC++ course_2014_c3_ function basics&parameters and overloading
2 BytesC++ course_2014_c3_ function basics&parameters and overloading
 
Computer Programming- Lecture 6
Computer Programming- Lecture 6Computer Programming- Lecture 6
Computer Programming- Lecture 6
 
DataTypes.ppt
DataTypes.pptDataTypes.ppt
DataTypes.ppt
 
C++ FUNCTIONS-1.pptx
C++ FUNCTIONS-1.pptxC++ FUNCTIONS-1.pptx
C++ FUNCTIONS-1.pptx
 
C++ FUNCTIONS-1.pptx
C++ FUNCTIONS-1.pptxC++ FUNCTIONS-1.pptx
C++ FUNCTIONS-1.pptx
 
I have question in c++ program I need the answer as soon as possible.docx
I have question in c++ program I need the answer as soon as possible.docxI have question in c++ program I need the answer as soon as possible.docx
I have question in c++ program I need the answer as soon as possible.docx
 
httplinux.die.netman3execfork() creates a new process by.docx
httplinux.die.netman3execfork() creates a new process by.docxhttplinux.die.netman3execfork() creates a new process by.docx
httplinux.die.netman3execfork() creates a new process by.docx
 
Lecture 3: Strings and Dynamic Memory Allocation
Lecture 3: Strings and Dynamic Memory AllocationLecture 3: Strings and Dynamic Memory Allocation
Lecture 3: Strings and Dynamic Memory Allocation
 
Library functions in c++
Library functions in c++Library functions in c++
Library functions in c++
 
Lecture 5Arrays on c++ for Beginner.pptx
Lecture 5Arrays on c++ for Beginner.pptxLecture 5Arrays on c++ for Beginner.pptx
Lecture 5Arrays on c++ for Beginner.pptx
 
Unit 3 arrays and_string
Unit 3 arrays and_stringUnit 3 arrays and_string
Unit 3 arrays and_string
 
Synapse india dotnet development overloading operater part 3
Synapse india dotnet development overloading operater part 3Synapse india dotnet development overloading operater part 3
Synapse india dotnet development overloading operater part 3
 

Mais de mcarolyn

given a slab of material with a finite resistivitySolutiona) Since the.docx
given a slab of material with a finite resistivitySolutiona) Since the.docxgiven a slab of material with a finite resistivitySolutiona) Since the.docx
given a slab of material with a finite resistivitySolutiona) Since the.docxmcarolyn
 
Give two examples of how a market has been used internally within a co.docx
Give two examples of how a market has been used internally within a co.docxGive two examples of how a market has been used internally within a co.docx
Give two examples of how a market has been used internally within a co.docxmcarolyn
 
Give the conjugate base for each compound below- Acid Conjugate Base H.docx
Give the conjugate base for each compound below- Acid Conjugate Base H.docxGive the conjugate base for each compound below- Acid Conjugate Base H.docx
Give the conjugate base for each compound below- Acid Conjugate Base H.docxmcarolyn
 
Give an example of a conflict of an interest in a business setting (a.docx
Give an example of a conflict of an interest in a business setting (a.docxGive an example of a conflict of an interest in a business setting (a.docx
Give an example of a conflict of an interest in a business setting (a.docxmcarolyn
 
Give 5 importance of using relations and digraphs in computer science-.docx
Give 5 importance of using relations and digraphs in computer science-.docxGive 5 importance of using relations and digraphs in computer science-.docx
Give 5 importance of using relations and digraphs in computer science-.docxmcarolyn
 
Give 2 reasons why chlorine is often added to groundwater prior to any.docx
Give 2 reasons why chlorine is often added to groundwater prior to any.docxGive 2 reasons why chlorine is often added to groundwater prior to any.docx
Give 2 reasons why chlorine is often added to groundwater prior to any.docxmcarolyn
 
gggggggggggggggggggggggggggggggggSolutionElectrical engineering is a f.docx
gggggggggggggggggggggggggggggggggSolutionElectrical engineering is a f.docxgggggggggggggggggggggggggggggggggSolutionElectrical engineering is a f.docx
gggggggggggggggggggggggggggggggggSolutionElectrical engineering is a f.docxmcarolyn
 
Generally- costs incurred internally to create intangibles are A) capi.docx
Generally- costs incurred internally to create intangibles are A) capi.docxGenerally- costs incurred internally to create intangibles are A) capi.docx
Generally- costs incurred internally to create intangibles are A) capi.docxmcarolyn
 
GaP is an intrinsic semiconductor with a band gap of 2-26 eV- How woul.docx
GaP is an intrinsic semiconductor with a band gap of 2-26 eV- How woul.docxGaP is an intrinsic semiconductor with a band gap of 2-26 eV- How woul.docx
GaP is an intrinsic semiconductor with a band gap of 2-26 eV- How woul.docxmcarolyn
 
Functional Groups a) what is an alcohol- how do you name alcohols- b).docx
Functional Groups a) what is an alcohol- how do you name alcohols- b).docxFunctional Groups a) what is an alcohol- how do you name alcohols- b).docx
Functional Groups a) what is an alcohol- how do you name alcohols- b).docxmcarolyn
 
Future Sources- Inc- reported the following results for the year endin.docx
Future Sources- Inc- reported the following results for the year endin.docxFuture Sources- Inc- reported the following results for the year endin.docx
Future Sources- Inc- reported the following results for the year endin.docxmcarolyn
 
Given the following declarations- what result Is stored In each of the.docx
Given the following declarations- what result Is stored In each of the.docxGiven the following declarations- what result Is stored In each of the.docx
Given the following declarations- what result Is stored In each of the.docxmcarolyn
 
Given below is the partial stockholders equity section of the Balance.docx
Given below is the partial stockholders equity section of the Balance.docxGiven below is the partial stockholders equity section of the Balance.docx
Given below is the partial stockholders equity section of the Balance.docxmcarolyn
 

Mais de mcarolyn (13)

given a slab of material with a finite resistivitySolutiona) Since the.docx
given a slab of material with a finite resistivitySolutiona) Since the.docxgiven a slab of material with a finite resistivitySolutiona) Since the.docx
given a slab of material with a finite resistivitySolutiona) Since the.docx
 
Give two examples of how a market has been used internally within a co.docx
Give two examples of how a market has been used internally within a co.docxGive two examples of how a market has been used internally within a co.docx
Give two examples of how a market has been used internally within a co.docx
 
Give the conjugate base for each compound below- Acid Conjugate Base H.docx
Give the conjugate base for each compound below- Acid Conjugate Base H.docxGive the conjugate base for each compound below- Acid Conjugate Base H.docx
Give the conjugate base for each compound below- Acid Conjugate Base H.docx
 
Give an example of a conflict of an interest in a business setting (a.docx
Give an example of a conflict of an interest in a business setting (a.docxGive an example of a conflict of an interest in a business setting (a.docx
Give an example of a conflict of an interest in a business setting (a.docx
 
Give 5 importance of using relations and digraphs in computer science-.docx
Give 5 importance of using relations and digraphs in computer science-.docxGive 5 importance of using relations and digraphs in computer science-.docx
Give 5 importance of using relations and digraphs in computer science-.docx
 
Give 2 reasons why chlorine is often added to groundwater prior to any.docx
Give 2 reasons why chlorine is often added to groundwater prior to any.docxGive 2 reasons why chlorine is often added to groundwater prior to any.docx
Give 2 reasons why chlorine is often added to groundwater prior to any.docx
 
gggggggggggggggggggggggggggggggggSolutionElectrical engineering is a f.docx
gggggggggggggggggggggggggggggggggSolutionElectrical engineering is a f.docxgggggggggggggggggggggggggggggggggSolutionElectrical engineering is a f.docx
gggggggggggggggggggggggggggggggggSolutionElectrical engineering is a f.docx
 
Generally- costs incurred internally to create intangibles are A) capi.docx
Generally- costs incurred internally to create intangibles are A) capi.docxGenerally- costs incurred internally to create intangibles are A) capi.docx
Generally- costs incurred internally to create intangibles are A) capi.docx
 
GaP is an intrinsic semiconductor with a band gap of 2-26 eV- How woul.docx
GaP is an intrinsic semiconductor with a band gap of 2-26 eV- How woul.docxGaP is an intrinsic semiconductor with a band gap of 2-26 eV- How woul.docx
GaP is an intrinsic semiconductor with a band gap of 2-26 eV- How woul.docx
 
Functional Groups a) what is an alcohol- how do you name alcohols- b).docx
Functional Groups a) what is an alcohol- how do you name alcohols- b).docxFunctional Groups a) what is an alcohol- how do you name alcohols- b).docx
Functional Groups a) what is an alcohol- how do you name alcohols- b).docx
 
Future Sources- Inc- reported the following results for the year endin.docx
Future Sources- Inc- reported the following results for the year endin.docxFuture Sources- Inc- reported the following results for the year endin.docx
Future Sources- Inc- reported the following results for the year endin.docx
 
Given the following declarations- what result Is stored In each of the.docx
Given the following declarations- what result Is stored In each of the.docxGiven the following declarations- what result Is stored In each of the.docx
Given the following declarations- what result Is stored In each of the.docx
 
Given below is the partial stockholders equity section of the Balance.docx
Given below is the partial stockholders equity section of the Balance.docxGiven below is the partial stockholders equity section of the Balance.docx
Given below is the partial stockholders equity section of the Balance.docx
 

Último

Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - Englishneillewis46
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Association for Project Management
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxcallscotland1987
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the ClassroomPooky Knightsmith
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structuredhanjurrannsibayan2
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701bronxfugly43
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 

Último (20)

Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 

Generate an array that contains strings as elements- Sort this array i.docx

  • 1. Generate an array that contains strings as elements. Sort this array in ascending order. (C++) Solution solution.cpp #include <iostream> //header file for input output function using namespace std; //it tells the compiler to link std arr2 space #include <cstring> //header file for string int main() { //main function // declare two arrays arr2d arr1 with 1-Dimension // and arr2 with 2-Dimension char arr1[10], arr2[10][10]; // normal variables... int i, j,noofstrings; cout<<"Enter the number of strings required to sort "; cin>>noofstrings; // for loop statement for(i=0; i<noofstrings; i++) { cout<<" Enter the strings of an array "<<(i+1)<<": "; cin>>arr2[i]; } for(i=0; i<noofstrings-1; i++) for(j = i+1; j<noofstrings; j++) //sorting string using strcmp() if(strcmp(arr2[i], arr2[j])>0) { // strcpy - copy the strings... // compare and swap... strcpy(arr1, arr2[i]); strcpy(arr2[i], arr2[j]); strcpy(arr2[j], arr1); }
  • 2. cout<<" Sorted strings are: "; for (i =0; i<noofstrings; i++) cout<<" "<<arr2[i]; cout<<endl; return 0; } output sh-4.3$ main Enter the number of strings required to sort 3 Enter the strings of an array 1: burningstar Enter the strings of an array 2: rockstar Enter the strings of an array 3: suddenstar Sorted strings are: burningstarockstar rockstar suddenstar