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 3600130
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T20:27:22+00:00 2026-05-18T20:27:22+00:00

/* Program that reads a sequence of words from keyboard and prints the list

  • 0
/* Program that reads a sequence of words from keyboard
   and prints the list of words without duplicates and
   sorted in ascending lexicographic order.
   The input words are written one per line and the
   sequence is terminated by an empty line.
   The program works with at most MAX words, each at
   most MAXL characters long. Longer words are truncated
   and words in excess are ignored.
*/

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

#define MAXL           80  /* maximum word length */
#define MAX           100  /* maximum number of words */

/* word storage */
char storage[MAX][MAXL];
char *words[MAX];

void init(char *pnt[], char matr[][MAXL], int max);
int read_words (char *s[], int max);
void sort_strings (char *s[], int len);
void swap_char_pnt(char **xp, char **yp);
void print_words(char *s[], int n);
int find (char *s[], char w[], int n);

main()
{
  int nw; /* actual number of words */

  /* initialize array of pointers */
  init(words, storage, MAX);

  /* read and store words */
  printf("Enter words one per line\n");
  nw = read_words(words, MAX);

  printf("\nList of unsorted words:\n");
  print_words(words, nw);

  /* sort words */
  sort_strings(words, nw);

  /* print words */
  printf("\nList of sorted words:\n");
  print_words(words, nw);
}


/* initializes an array of pointers to the rows of
 a matrix of characters
 max is the number of pointers to initialize
*/
void init(char *pnt[], char matr[][MAXL], int max) {
  int i;

  for (i=0; i<max; i++)
    pnt[i] = matr[i];
}

/* Reads a sequence of words from stdin, one per line
 Reads at most max words and stores them in s
 Returns actual number of words read
*/
int read_words (char *s[], int max)
{
  int i;
  fgets(s[0], MAXL, stdin);
  for (i=1; i<max && fgets(s[i], MAXL, stdin)!=NULL; ) {
        if(!strcmp(s[i],"\n"))
    break;
    if (find(s, s[i], i)==-1)
    i++;
  }
  return i;
}

int find (char *s[], char w[], int n)
{
  int i;

  if (n<0)
    return -1;
  for (i=0; i<n; i++)
    if (!strcmp(s[i],w))
    break;
  if (i==n)
    return -1;
  else
    return i;
}

/* Sorts an aray of pointers to strings in ascending order
*/
void sort_strings (char *s[], int len)
{
  int i,k; /* vector index and counter */
  char swaps=1; /* boolean variable: true if any swap has occurred in last round */

  for (k=1; k<=len-1 && swaps; k++) {
  swaps=0;
  for (i=0; i<len-k; i++)
  if (strcmp(s[i],s[i+1])>0) {
      /* swap */
      swap_char_pnt(&s[i], &s[i+1]);
      swaps=1;
  }
  }
}

void swap_char_pnt(char **xp, char **yp) {
  char *temp;

  temp = *xp;
  *xp = *yp;
  *yp = temp;
}

void print_words(char *s[], int n)
{
  int i;

  for (i=0; i<n; i++)
    printf("%s", s[i]);

}

What Can i Do to Sort Text File in Ascending Order of Strings.

  • 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-05-18T20:27:22+00:00Added an answer on May 18, 2026 at 8:27 pm
    • Read all lines to an array of char* s,
    • define a string comparator function for qsort from <stdlib.h> header file:

      int compare(const void* a, const void* b) {
          return strcmp(*(const char**)a, *(const char**)b); 
      }
      
    • Use qsort() on that array, using this compare function.

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

Sidebar

Related Questions

#include<stdlib.h> #include<stdio.h> #include<string.h> //This program is a sorting application that reads a sequence of
I have a PHP program that reads from the database and generated formatted sheets.
I have a program that reads a raw list of in-game entities, and I
I'm trying to write a program that reads in entries from a file into
I want to write a program in which plays an audio file that reads
I have to write a program that read from a file that contains the
I'm trying to use the Win32 API to make a sub-thread that reads from
I'm writing a program that reads a stream of data and parses it into
I have a program that reads an XML file (for now, on local computer.)
I have written a program that reads in a File object (really an XML

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.