title: Sorting string lexicographically in c
body: <p>I want to sort words of a string in lexicographical order.</p>
  
  <p>For Example:</p>
  
  <p>I have a string: <code>I am Apple</code></p>
  
  <p>Output should be: <code>am Apple I</code></p>
  
  <p>Problem (output): </p>
  
  <pre><code>enter the string
  
  hello shamsh
  
  the sorted array:
  
  hello
  </code></pre>
  
  <p>It's not sorting the string and whole string is not being shown in the output, can anyone help me out here. Thanks!</p>
  
  <p>Program code:</p>
  
  <pre><code>#include&lt;stdio.h&gt;
  #include&lt;string.h&gt;
  void main()
  {
      char a[25][25],t[25];
      char s[200];
      char * pch;
      int count = 0;
      int i,j ,n;
      printf("enter the string\n");
      gets(s);
      pch = strtok (s," ,.-");
      for (i = 0;s[i] != '\0';i++)
      {
          if (s[i] == ' ')
              count++;    
      }
      count=count+1;
      i=0;
      while(pch != NULL)
      {
          strcpy(a[i],pch);
          pch = strtok (NULL, " ,.-");
          i++;
      }
  
      for(i=0;i&lt;count-1;i++)
      {
          for(j=i+1;j&lt;count;j++)
          {
              if(strcmp(a[i],a[j])&gt;0)
              {
                  strcpy(t,a[i]);
                  strcpy(a[i],a[j]);
                  strcpy(a[j],t);
              }
          }
      }
  printf("the sorted array:\n");
  for(i=0;i&lt;count;i++)
  printf("%s\n",a[i]);
  }
  </code></pre>