2

I have been struggling to write a simple code to sort an array of char in C and been failing miserably. This is what I have so far:

int main()
{
  char line[128];
  char word[128];
  int i=0;
  int j;
  int length;

  while(fgets(line,sizeof line,stdin) != NULL)
  {
    length=0;
    i=0;

    while (line[i]!='\0')
      i++;

    length=i;

    line[i-1]=line[i];

    for (i=0;i<=length;i++)
      word[i]=line[i];

    for (i=length-1; i>=0; i--)
    {
      for (j=0;j<i;j++)
      {
        if (line[j] > line[i])
        {
          char temp;
          temp=line[j];
          line[j]=line[i];
          line[i]=temp;
        }
      }
    }

    printf("%s %s\n",line,word);
  }
  return 0;
}

Original file:

overflow
array
test
string
stack

Output file:

 overflow
 array
 test
 string
 stack

This is giving me rather unexpected results. Where am I going wrong?

15
  • 1
    Can you define "unexpected results"? It works perfectly for me. Commented Oct 14, 2010 at 3:52
  • @casablanca: If it works for me, then the problem is somewhere else in my code. Let me look into it Commented Oct 14, 2010 at 3:55
  • 2
    Maybe your length is set incorrectly? Commented Oct 14, 2010 at 3:56
  • This is a follow-up/duplicate of [ C program to sort characters in a string ](stackoverflow.com/questions/3929547/…). As I said there, the sort is inefficient but correct. The output there also shows it working (though there are other errors). Commented Oct 14, 2010 at 3:59
  • 1
    @JoshD: I think it removes the newline retrieved by fgets. Commented Oct 14, 2010 at 4:10

3 Answers 3

3

Change:

length = i;

to:

length = i - 1;

and it works.

When you do line[i-1] = line[i]; you are removing the \n (which is placed there by fgets) from the line and effectively reducing the string length by 1. You should take that into account.

With your current code, the length includes the null terminator, which gets sorted to the beginning of the string, hence your result is an empty string.

Sign up to request clarification or add additional context in comments.

Comments

2

Just as I suspected, by avoiding the CR/LF:

length=i;
line[i-1]=line[i];

you included the '\0' characted in the string to be sorted. It will get in the first place, so you'll have an empty string to print.

Comments

1

You are removing the \n from the line by overwriting it with \0 but your length calculation is done before this change.

To fix this, do

line[i-1]=line[i];

before you calculate the length.

Since your length is one more than the actual length, \0 is also taking part as a char to be sorted and since its the smallest of all char, it gets placed at the beginning of the string, effectively making your string empty.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.