Answer to Question #43946 in C++ for joney

Question #43946
Write the following function

a) Write a function that takes an array of strings and its size and returns the length of the

largest element in the list.

b) Write a function that takes an array of strings and returns a concatenation string of all its

elements separated by a white space.



c) Write a function strsort that takes an array of strings and its size and then it sorts the array

alphabetically. Find and use the implementation of selection sort algorithm (you can find

it online or in any text book).

d) Write a main function and test these functions using the following array of strings:

char *fruites[5] = {"apple", "banana", "orange", "apricot", "pineapple"};
1
Expert's answer
2014-07-08T14:13:48-0400
#include "conio.h"
#include "iostream"
#include <stdio.h>
#include <string>
using namespace std;

int FirstFunction(char *arr[], int num)
{
int j = 0;
int MaxEl;

char* a = arr[0];
while (a[j] != '\0'){
j++;
}
MaxEl = j;
for (int i = 0; i < num;i++){
char* a = arr[i];
while (a[j] != '\0'){
j++;
}
if (j > MaxEl)
{
MaxEl = j;
}
j = 0;
}
return MaxEl;
}

char *SecondFunction(char *arr[], int num)
{
char *s1 = arr[0], *s2 = arr[1];
char* s3;
char *temp;
s3 = (char*)malloc(strlen(s1) + 1);
strcpy(s3,s1);
temp = s3;
for (int i = 1; i < num;i++){
s1 = arr[i];
s3 = (char*)malloc(strlen(s1) + strlen(temp) + 2);
strcpy(s3, temp);
strcat(s3, " ");
strcat(s3, s1);
temp = s3;
}
return s3;
}

void strsort(char *arr[], int num)
{
for (int i = 0; i<num-1; i++)
{
int min = i;
for (int j = i + 1; j<num; j++)
{
if (strcmp(arr[j], arr[min]) < 0)
{
min = j;
}
}
char* dummy = arr[i];
arr[i] = arr[min];
arr[min] = dummy;
}

}


int main()
{
char *fruites[5] = { "apple", "banana", "orange", "apricot", "pineapple" };
cout <<"Lenght of largest element inlist : "<< FirstFunction(fruites, 5) << endl;
cout << SecondFunction(fruites, 5) << endl;
strsort(fruites, 5);
cout << "Sorted array : " << endl;
for (int i = 0; i < 5;i++)
{
cout << fruites[i] << endl;
}
_getch();
return 0;

}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS