-2

I got function to find longest sentence in array of sentences how can i now turn this to find the shortest and then copy that into char R[red]; and then manipulate over last word of the sentence of the shortests one to change first and last letter into uppercase

void longest (char SENTENCE[max][red],char R[red], int n){
    int longest = 0;
    for (int i = 0 ; i < n ; i++){
        if(strlen(SENTENCE[i]) > longest){
            longest = strlen(SENTENCE[i]);
            strcpy_s(R,red,SENTENCE[i]);
        }
    }

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335

2 Answers2

1

For starters there is the standard algorithm std::min_element that returns an iterator that points to the minimal element in a container. However using it with the function strlen will not be very efficient.

So you can indeed write such a function yourself. It can look the following way

void shortest( const char s[][red], char t[], size_t n )
{
    t[0] = '\0';

    if ( n != 0 )
    {
        size_t min_i   = 0;
        size_t min_len = std::strlen( s[0] );

        for ( size_t i = 1; i < n; i++ )
        {
            size_t len = std::strlen( s[i] );
            if ( len < min_len )
            {
                min_i   = i;
                min_len = len;
            }
        }

        std::strcpy( t, s[min_i] ); 
    }
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
-1

Made it this way

void shortest (char SENTENCE[max][red],char R[red], int n){
    int shortest = strlen(SENTENCE[0]);
    for (int i = 0 ; i < n ; i++){
        if(strlen(SENTENCE[i]) < shortest){
            shortest = strlen(SENTENCE[i]);
            strcpy_s(R,red,SENTENCE[i]);
        }
    }

}