Nguythang

sorting string

Dec 4th, 2015
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.60 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5.  
  6. //remove redundant spaces in a string s
  7.  
  8. void removeRedSpaces(char s[]) {
  9.         int n, low, up, i, j; char x[100];
  10.         n = strlen(s);
  11.         i = 0; j = n - 1;
  12.         while(i < n && s[i] == ' ') i++;
  13.         low = i;
  14.         while(j > 0 && s[j] == ' ') j--;
  15.         up = j;
  16.         if(low > up) {
  17.                 strcpy(s, "");
  18.                 return;
  19.         }
  20.         strcpy(x, "");
  21.         i = low; j = 0;
  22.         while(i <= up) {
  23.                 while(i < up && s[i] == ' ') i++;
  24.                 while(i <= up && s[i] != ' ') x[j++] = s[i++];
  25.                 if(i <= up) x[j++] = ' ';
  26.         }
  27.         x[j] = '\0';
  28.         strcpy(s,x);
  29. }
  30. void split( char s[], char out[][30], int &length) //tach cac tu trong 1 chuoi
  31. {
  32.         removeRedSpaces(s);
  33.         int i = 0, j, k = 0;
  34.         while ( i < strlen(s))
  35.         {
  36.                 for ( j = i; s[j] != '\0' && s[j] != ' '; j ++)
  37.                
  38.                         out[ k ][ j - i ] = s[j];
  39.                         out[ k ][ j - i ] = '\0';
  40.                         i = j + 1;
  41.                         k++;
  42.         }
  43.         length = k;
  44. }
  45.  
  46. //Enter data for a string s
  47. void input(char s[]) {
  48.         printf("Enter a string s = ");
  49.         fflush(stdin);
  50.         gets(s);
  51. }
  52.  
  53. //Display a string s
  54. void display(char s[]) {
  55.         printf("%s\n",s);
  56. }
  57.  
  58. void fun(char s[]) {
  59.         int i, j, n;
  60.         char x[100][30], temp[30];
  61.         split(s, x, n);
  62.         for (i = n - 5; i < n ; i++)
  63.                 for (j = i + 1; j < n; j++)
  64.                         if (strcmp(x[i],x[j]) > 0)
  65.                                 { //doi cho
  66.                                         strcpy(temp,x[i]);
  67.                                         strcpy(x[i],x[j]);
  68.                                         strcpy(x[j],temp);
  69.                                 }
  70.        
  71.         // rebuild
  72.         int length = strlen(s);
  73.         strcpy(s, "");
  74.         for (i = 0; i < n; ++i) {
  75.                 strcat(s, x[i]);
  76.                 strcat(s, " ");
  77.         }
  78.         s[length] = '\0';
  79. }
  80.  
  81. int main()
  82. {
  83.         system("cls");
  84.         printf("\nTEST Q5 (2 marks):\n");
  85.         char s[100];
  86.         input(s);
  87.         printf("\nThe original string:\n");
  88.         display(s);
  89.         printf("\nThe string after processing:\n");
  90.         fun(s);
  91.         display(s);
  92.         //==THE OUTPUT AFTER THIS LINE WILL BE USED TO MARK YOUR PROGRAM==
  93.         printf("\nOUTPUT:\n");
  94.         display(s);
  95.         printf("\n");
  96.         system ("pause");
  97.         return(0);
  98. }
Advertisement
Add Comment
Please, Sign In to add comment