Skip to main content
1 of 5
MARS
  • 5.2k
  • 3
  • 14
  • 23

Here I see a problem with the swaps function and the indexes of a [i], as I believe on the return of the swaps function is lost control of the indexes i, a simple way is to avoid this function and do a swap directly on main , I wrote this version of your program that I think works well:

#include <stdio.h>
#include <string.h>
#include <cs50.h>
#include<string.h>

int main(void)
{
    int n=GetInt();
    char* a[n];
    int c=0;
    for(int i=0;i<n;i++)
    {
        a[i]=GetString();
    }
    puts("");
    for(int j=0;j<n;j++)                                  
    {
        for(int i=0;i<n;i++)
        {
            if(j != i)
            {
                c=strcmp(a[j],a[i]);
                if(c>0)
                {
                    char* temp = a[j];
                    a[j] = a[i];
                    a[i] = temp;
                }
            }        
        }
    }
    for(int i=n;i < 0;i--)
    {
         printf("%s\n",a[i]);   
    }
    return 0;
}

By the way this question could be considered out of topic

MARS
  • 5.2k
  • 3
  • 14
  • 23