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

  • Home
  • SEARCH
  • 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 543271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:32:02+00:00 2026-05-13T10:32:02+00:00

This program sorts lines alphabetically/numerically depending on the arguments passed to main. And im

  • 0

This program sorts lines alphabetically/numerically depending on the arguments passed to main.

And im working on this exercise from k&R now:
Add the option -f to fold upper and lower case together, so that case distinctions are not made during sorting; for example, a and A compare equal.

Is what i wrote in my_strcmp good? And will it work good in conjuction with -r and -n? (r – reverse order, n- numerical sort).

I figured id ask here for your opinions since on klc wiki the solution isnt presented like this.

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

#define MAXBUF 10000

#define MAXLINES 5000
char *lineptr[MAXLINES];

int readlines(char *lineptr[], char buffer[], int nlines);
void writelines(char *lineptr[], int nlines);

void qsort(void **lineptr, int left, int right, int (*comp)(void *, void *));
int numcmp(char *, char *);

int reverse = 0;
int numeric = 0;
int fold = 0;
int my_strcmp(char *s1, char *s2)
{

     char *p1 = (reverse) ? s2 : s1;
     char *p2 = (reverse) ? s1 : s2;


     if(fold) {
         while(toupper(*p1) == toupper(*p2)) {
            p1++, p2++;
            if(!*p1)
              return 0;
         }
         return toupper(*p1) - toupper(*p2);
     }

     return strcmp(p1, p2);
}

int main(int argc, char *argv[])
{
    int nlines;
    char buffer[MAXBUF];

    int i = 1;
    for(i = 1; i < argc; i++) {
          if(strcmp(argv[i], "-n") == 0) {
             numeric = 1;
          } else if(strcmp(argv[i], "-r") == 0) {
                 reverse = 1;
          } else if(strcmp(argv[i], "-f") == 0) {
                 fold = 1;
          } else {
                 printf("illegal argument\n");
          }
    }


    if((nlines = readlines(lineptr, buffer, MAXLINES)) >= 0) {
        qsort((void **)lineptr, 0, nlines - 1, 
        (numeric ? (int (*)(void *, void *))numcmp : (int (*)(void *, void *))my_strcmp));
        writelines(lineptr, nlines);
        getchar();
        return 0;
    } else {
           printf("input too big to sort\n");
           return 1;
    }

}

void writelines(char *lineptr[], int nlines)
{
    int i;

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

int getline(char s[], int lim)
{
    int c, i;

    for (i=0; i<lim-1 && (c=getchar())!=EOF && c!='\n'; ++i)
        s[i] = c;
    if (c == '\n') {
        s[i] = c;
        ++i;
    }
    s[i] = '\0';
    return i;
}

#define MAXLEN 1000
int readlines(char *lineptr[], char *buffer, int maxlines)
{
    int len, nlines;
    char *p, line[MAXLEN];

    nlines = 0;
    p = buffer;
    while ((len = getline(line, MAXLEN)) > 0)
        if (p - buffer + len > MAXBUF)
            return -1;
        else {
            line[len-1] = '\0'; /* delete newline */
            strcpy(p, line);
            lineptr[nlines++] = p;
            p += len;
        }
    return nlines;
}

void qsort(void *v[], int left, int right, int(*comp)(void *, void *))
{
     int i, last;
     void swap(void *v[], int, int);

     if(left >= right)
        return;

     swap(v, left, (left + right) / 2);
     last = left;
     for(i = left + 1; i <= right; i++) 
        if((*comp)(v[i], v[left]) < 0)
           swap(v, ++last, i);
     swap(v, left, last);
     qsort(v, left, last - 1, comp);
     qsort(v, last + 1, right, comp);
}

#include <stdlib.h>

int numcmp(char *p1, char *p2)
{
  char *s1 = reverse ? p2 : p1;
  char *s2 = reverse ? p1 : p2;
  double v1, v2;

  v1 = atof(s1);
  v2 = atof(s2);
  if (v1 < v2)
    return -1;
  else if (v1 > v2)                                     
    return 1;      
  else
    return 0;
}

void swap(void *v[], int i, int j)
{
     void *temp;

     temp = v[i];
     v[i] = v[j];
     v[j] = temp;
}

One more question. I was trying to add the option -d (directory order) – “which makes comparisons only on letters numbers and blanks. Make sure it works in conjuction with -f.” and im a bit baffled on how to edit my_strcmp. This is what i did:

int my_strcmp(char *s1, char *s2)
{

     char *p1 = (reverse) ? s2 : s1;
     char *p2 = (reverse) ? s1 : s2;

     if(directory) {
         while(!isdigit(*p1) && !isspace(*p1) && !isalpha(*p1) && *p1)
           p1++;

         while(!isdigit(*p2) && !isspace(*p2) && !isalpha(*p2) && *p2)
           p2++;
     }

     if(fold) {
         while(toupper(*p1) == toupper(*p2)) {
            p1++, p2++;
            if(!*p1)
              return 0;
            if(directory) {
               while(!isdigit(*p1) && !isspace(*p1) && !isalpha(*p1))
                   p1++;

               while(!isdigit(*p2) && !isspace(*p2) && !isalpha(*p2))
                   p2++;         
            }
         }
         return toupper(*p1) - toupper(*p2);
     }

     return my_strcmp2(p1, p2);
}

But im not so sure if its good.

I also wrote my_strcmp2, which handles the case if directory and fold are zero.

Then we just strcmp them, but we have to track if directory is 1 aswell…

int my_strcmp2(char *s1, char *s2)
{
    char *p1 = (reverse) ? s2 : s1;
    char *p2 = (reverse) ? s1 : s2;

     while(*p1 == *p1 && *p1) {
        p1++;
        p2++;

         if(directory) {
             while(!isdigit(*p1) && !isspace(*p1) && !isalpha(*p1) && *p1)
               p1++;

             while(!isdigit(*p2) && !isspace(*p2) && !isalpha(*p2) && *p2)
               p2++;
         }
     }
     return *p1 - *p2;
}
  • 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-13T10:32:03+00:00Added an answer on May 13, 2026 at 10:32 am

    When fold==1, my_strcmp will return 0 when p1 is a prefix of p2. You can fix this by moving the line if(!*p1) return 0 to the beginning of the while loop. Other than that it looks good.

    Edit:
    Regarding your second question: You are on the right track with incrementing p1 and p2 for ignored characters. However, this function won’t work in non-folding mode (it seems to call itself infinitely). (this was fixed by an edit to the question)

    I would make a helper function compareCharactes that would compare 2 characters with or without case sensitivity, depending on the value of fold. Then you could use your while loop whether fold is on or off.

    Edit2: OK, you keep changing your question… Anyway, if you take my advice on the compareCharacters function, there would be no need for the separate my_strcmp and my_strcmp2 functions. You could just write while (compareCharacters(*p1, *p2)==0) {.....}

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

Sidebar

Ask A Question

Stats

  • Questions 443k
  • Answers 443k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer date = datetime.datetime(2003,8,1,12,4,5) for i in range(5): date += datetime.timedelta(days=1)… May 15, 2026 at 6:21 pm
  • Editorial Team
    Editorial Team added an answer You have an UPDATE in your INSERT trigger. So the… May 15, 2026 at 6:21 pm
  • Editorial Team
    Editorial Team added an answer I would use the content_for helper (Rails Guide) (API). In… May 15, 2026 at 6:21 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.