Advertisement
Guest User

from Q to Z

a guest
Sep 16th, 2019
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. #include <stdlib.h> // qsort, toupper
  2. #include <string.h> // strlen
  3. #include <stdbool.h> // true
  4. #include <stdio.h> // puts, fflush, fgets
  5.  
  6. #define BUF_SIZE 80
  7.  
  8. int tovalid(int c)
  9. {
  10. c = toupper(c);
  11. if (c < 'Q' || c > 'Z') {
  12. c = 0;
  13. }
  14. return c;
  15. }
  16.  
  17. void filter(char* dst, char* src)
  18. {
  19. int c;
  20. while (c = (unsigned char)*src++) {
  21. if (c = tovalid(c)) {
  22. *dst++ = c;
  23. }
  24. }
  25. *dst = '\0';
  26. }
  27.  
  28. int charcmp(const void* a, const void* b)
  29. {
  30. int code_a = *(unsigned char*)a;
  31. int code_b = *(unsigned char*)b;
  32. return code_a - code_b;
  33. }
  34.  
  35. int main(void)
  36. {
  37. char input[BUF_SIZE];
  38. char output[BUF_SIZE];
  39. bool Isbull = true;
  40. while (Isbull) {
  41. puts("Enter string:");
  42. fflush(stdin);
  43. fgets(input, sizeof(input), stdin);
  44. if (input[0] == '\n') { break; }
  45. filter(output, input);
  46. puts(output);
  47. qsort(output, strlen(output), sizeof(char), charcmp);
  48. puts(output);
  49. Isbull = false;
  50. }
  51. getchar();
  52. return 0;
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement