Advertisement
Guest User

POO

a guest
Feb 19th, 2020
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.80 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int convertString(char s[]);
  5. bool isLetter(char s);
  6. void swapWords(char a[], char b[]);
  7. int length(char s[]);
  8. void coppy(char a[], char b[]);
  9. bool isSmaller(char a[], char b[]);
  10.  
  11. int main()
  12. {
  13.     /*
  14.     FILE* fs;
  15.     fs = fopen("in.txt", "r");
  16.     char string[11];
  17.     int sum = 0;
  18.     while(fgets(string, 11, fs))
  19.         sum += convertString(string);
  20.     fclose(fs);
  21.     printf("Suma este egala cu %d.", sum);*/
  22.     char string[100], words[10][100], j=0, nrOfWords=0;
  23.     scanf("%[^\n]", string);
  24.     for (int i = 0; string[i]; i++)
  25.     {
  26.         if (isLetter(string[i]))
  27.         {
  28.             words[nrOfWords][j] = string[i];
  29.             j++;
  30.         }
  31.         else
  32.         {
  33.             words[nrOfWords][j] = 0;
  34.             nrOfWords++;
  35.             j = 0;
  36.         }
  37.     }
  38.     words[nrOfWords][j] = 0;
  39.     for (int i = 0; i < nrOfWords; i++)
  40.     {
  41.         for (int j = i + 1; j <= nrOfWords; j++)
  42.             if (length(words[i]) < length(words[j]))
  43.                 swapWords(words[i], words[j]);
  44.             else
  45.                 if (length(words[i]) == length(words[j]) && isSmaller(words[i], words[j]))
  46.                     swapWords(words[i], words[j]);
  47.  
  48.     }
  49.     for (int i = 0; i <= nrOfWords; i++)
  50.         printf("%s\n", words[i]);
  51.     return 0;
  52. }
  53.  
  54. int convertString(char s[]) ///123
  55. {
  56.     int nr = 0;
  57.     for (int i = 0; s[i]; i++)
  58.         if (s[i] >= '0' && s[i] <= '9')
  59.             nr = nr * 10 + (s[i] - '0');
  60.     return nr;
  61. }
  62.  
  63. bool isLetter(char s)
  64. {
  65.     return (s >= 'a' && s <= 'z') || (s >= 'A' && s <= 'Z');
  66. }
  67.  
  68. int length(char s[])
  69. {
  70.     int nr = 0;
  71.     for (int i = 0; s[i]; i++)
  72.         nr++;
  73.     return nr;
  74. }
  75.  
  76. void swapWords(char a[], char b[])
  77. {
  78.     char aux[100];
  79.     coppy(aux, a);
  80.     coppy(a, b);
  81.     coppy(b, aux);
  82. }
  83.  
  84. void coppy(char a[], char b[])
  85. {
  86.     int i;
  87.     for (i = 0; b[i]; i++)
  88.         a[i] = b[i];
  89.     a[i] = 0;
  90. }
  91.  
  92. bool isSmaller(char a[], char b[])
  93. {
  94.     for (int i = 0, j = 0; a[i] && b[i]; i++, j++)
  95.         if (a[i] < b[i])
  96.             return 1;
  97.     return 0;
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement