Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8058499
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T09:22:33+00:00 2026-06-05T09:22:33+00:00

I have written a program which could sort 5 strings you inputed from small

  • 0

I have written a program which could sort 5 strings you inputed from small to big. However, it can’t work. I have worked at it for almost an hour, but I couldn’t find out the problem. Here is the code.

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

    main() {
    char *sz[5], *temp;
    int i, j;
    for(i = 0; i < 5; i++) {
        gets(sz[i]);
        fflush(stdin);
    }
    for(i = 0; i < 5; i++) {
        for(j = i+1; j < 5; j++) {
            if(strcmp(sz[i], sz[j]) > 0) {
                temp = sz[i];
                sz[i] = sz[j];
                sz[j] = temp;
            }
        }
        puts(sz[i]);
        puts("");
        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T09:22:35+00:00Added an answer on June 5, 2026 at 9:22 am

    First huge problem is that you are using a routine that should never have existed, and using it improperly:

    char *sz[5], *temp;
    int i, j;
    for(i = 0; i < 5; i++) {
        gets(sz[i]);
    

    You did not allocate any storage for gets() to store into, so it is simply scribbling on unrelated memory. (This often leads to security problems.)

    You should pay special attention to the BUGS section in your manpages:

    BUGS
       Never use gets().  Because it is impossible to tell without
       knowing the data in advance how many characters gets() will
       read, and because gets() will continue to store characters
       past the end of the buffer, it is extremely dangerous to use.
       It has been used to break computer security.  Use fgets()
       instead.
    

    Unlearn gets(3), now, and be a happier programmer.

    Use malloc() to allocate some memory for those character arrays.

    Ignacio hit another problem squarely on the head — you’re printing before your sort is finished. Add another loop to print, after the sorting. (Better yet, put the input, sort, and output into three separate functions. Perhaps you’re not there yet, but it would be well worth doing this sooner rather than later, as it makes testing your programs significantly easier to have printing functions you can use for debugging.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a program which strips the tags from a HTML website which
I have written a program in Javascript which reads numbers from a file ,sorts
I have a program which i have myself written in java, but I want
I have written this program, which sorts some ints using a functor: #include<iostream> #include<list>
I have a debugging program which I've written to attach to a process and
I am beginning to port a program which is written in C and have
i have written a c program to which i pass in a script like
I have written a program which I run after connecting to the box over
Let say I have written a small program that reads file_A and file_B as
I have written a program which will serialize and de-serialize, it does this fine

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.