#include<iostream>
usingnamespace std;
// Function to print the sorted array of string
void printArraystring(string,int);
// Function to Sort the array of string
// according to lengths. This function
// implements Insertion Sort.
void sort(string s[], int n)
{
for (int i=1 ;i<n; i++)
{
string temp = s[i];
// Insert s[j] at its correct position
int j = i - 1;
while (j >= 0 && temp.length() < s[j].length())
{
s[j+1] = s[j];
j--;
}
s[j+1] = temp;
}
}
// Function to print the sorted array of string
void printArraystring(string str[], int n)
{
for (int i=0; i<n; i++)
cout << str[i] << " ";
}
int main()
{
string arr[] = {"Computer", "I", "thru", "am"};
int n = sizeof(arr)/sizeof(arr[0]);
// Function to perform sorting
sort(arr, n);
// Calling the function to print result
printArraystring(arr, n);
return 0;
}
and also cand you change the string arr[] = {"Computer", "I", "thru", "am"}; to be introduced fom the keyboard, please :)
What you have is a single program. The program contains 3 functions: sort, printArraystring, and main.
Shoving the code from sort and printArraystring into main can be done, but what you have here is a MUCH better way to go. I highly recommend that you keep the functions and not make is a single function.
As far as entering the strings from the keyboard, read up on I/O.