0

Hey guys am a newbie in c programming actually..I have a char array and am trying to keep the elements in an order. I have done it scuccefully but when i print the elements of the array an extra character ÿ happens in my code.

My code

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

char values[4] = "mmfa";
int cmpfunc (const void * a, const void  * b)
{
   return(*(char*)a - *(char*)b);
}

int main()
{
   int n;


   qsort(values, 5, sizeof(char), cmpfunc);
   printf("\nAfter sorting the list is: \n");

   for( n = 0 ; n < 5; n++ )
 {
    printf("%c ", values[n]);
 }

   return(0);
}

When i print this i get the result ÿ a f m m.I need to remove the ÿ..I have tried it by escaping the aray with \ but it didnt worked out.So how would i able to remove the ascii code from the result.

Thanx for the help

babx pox
  • 41
  • 4

2 Answers2

0
char values[4] = "mmfa";

Strings are terminated with \0 so chage your array size to char values[5] .

Note index 5 is for null character.

EDIT

     for( n = 0 ; n < 5; n++ )
   {
         printf("%c ", values[n]);
   }

In this loop n goes till 4 where as your array is till 3 .Remember array index starts with 0 .

Also while calling qsort parameters should be -

      qsort(values, 4, sizeof(char), cmpfunc);
                     ^because number of elements in array is 4 not 5 .
ameyCU
  • 16,489
  • 2
  • 26
  • 41
  • then i got a small box like infront of the program – babx pox Jul 19 '15 at 06:50
  • i did this code still it gives me a small symbol like box infront of the program' – babx pox Jul 19 '15 at 06:54
  • @babxpox please show definition of function qsort. – ameyCU Jul 19 '15 at 06:56
  • @babxpox please see above and now your code give correct output. – ameyCU Jul 19 '15 at 07:00
  • but the small symbol is still there..:o – babx pox Jul 19 '15 at 07:06
  • @babxpox you changed array size to `5` and in `qsort` to `4` .Then I don't think you should get wrong output. – ameyCU Jul 19 '15 at 07:08
  • which compiler are you using / – babx pox Jul 19 '15 at 07:09
  • @babxpox I am using gcc 4.8 version. – ameyCU Jul 19 '15 at 07:14
  • thanx it worked..i have a question ..i want the output of this program to a new window of different style ..when user click the .exe file of this program i want to have a different window opened for it and display the output ..will i have to use c sharp for this ? – babx pox Jul 19 '15 at 07:23
  • @babxpox Well actually when you will click `.exe` file of this program it open in a different window . And you can add background or text color accordingly by `c` only . But I don't get what you mean by style? – ameyCU Jul 19 '15 at 07:33
  • style means the background ans colour ..if you have good tutorials for that can you give me a link to acheieve this task ? – babx pox Jul 19 '15 at 07:35
  • @babxpox This link is good for it -http://stackoverflow.com/questions/20725769/change-background-color-of-console-output-in-c-and-c Method here is better than system command . – ameyCU Jul 19 '15 at 07:41
  • @babxpox Also you can use header graphics.h and its function -http://www.programmingsimplified.com/c/graphics.h see `setbkcolor()` in it . – ameyCU Jul 19 '15 at 07:44
  • but when i open the exe file it opens and closes so fast – babx pox Jul 19 '15 at 07:45
  • @babxpox Just add `getchar()` before `return`. – ameyCU Jul 19 '15 at 07:47
  • wow thanx ..finally one more question ..how can i add an input bar to accept the user input in c ??..is this provision available ? – babx pox Jul 19 '15 at 08:31
  • @babxpox you can get help from here -http://stackoverflow.com/questions/8667194/input-bar-at-console-bottom-in-c – ameyCU Jul 19 '15 at 08:40
  • @babxpox well leave it .Use this -http://www.dreamincode.net/forums/topic/160896-createwindow-input-box-timer-functioning-help/ – ameyCU Jul 19 '15 at 08:49
0

Declare the char array like this

char values[] = "mmfa";

and change the qsort call to

qsort(values, strlen(values), sizeof(char), cmpfunc);

Output

After sorting the list is: 
a f m m 

See Demo http://ideone.com/v3r9ir.

Shreevardhan
  • 12,233
  • 3
  • 36
  • 50