Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.26 KB | None | 0 0
  1. char* reverse_sub(char* str, int start, int end) {
  2.     while (start < end) {
  3.         swap(str + start++, str + end--);
  4.     }
  5.     return str;
  6. }
  7.  
  8. char* reverse(char* str) {
  9.     return reverse_sub(str, 0, strlen(str) - 1);
  10. }
  11.  
  12. int ch_str(char* str, char ch) {
  13.     int i = 0;
  14.     while (str[i]) {
  15.         if (str[i] == ch) {
  16.             return i;
  17.         }
  18.         i++;
  19.     }
  20.     return -1;
  21. }
  22.  
  23. int words_count(char* str) {
  24.     char sep[] = "., ()!?\n\t";
  25.     int i = 0, count = 0, in_word = 1;
  26.     while (str[i]) {
  27.         if (ch_str(sep, str[i]) != -1) {
  28.             if (in_word) {
  29.                 count++;
  30.                 in_word = 0;
  31.             }
  32.         }
  33.         else {
  34.             in_word = 1;
  35.         }
  36.         i++;
  37.     }//hello world\0
  38.     if (in_word) {
  39.         count++;
  40.     }
  41.     return count;
  42. }
  43.  
  44. int longest(char* str) {
  45.     char sep[] = "., ()!?\n\t";
  46.     int i = 0, max_len = 0, current = 0, in_word = 1;
  47.     while (str[i]) {
  48.         if (ch_str(sep, str[i]) != -1) {
  49.             if (in_word) {
  50.                 if (current > max_len) {
  51.                     max_len = current;
  52.                 }
  53.                 current = 0;
  54.                 in_word = 0;
  55.             }
  56.         }
  57.         else {
  58.             current++;
  59.             in_word = 1;
  60.         }
  61.         i++;
  62.     }
  63.     if (in_word) {
  64.         if (current > max_len) {
  65.             max_len = current;
  66.         }
  67.     }
  68.     return max_len;
  69. }
  70.  
  71. int shortest(char* str) {
  72.     char sep[] = "., ()!?\n\t";
  73.     int i = 0, min_len = INT_MAX, current = 0, in_word = 1;
  74.     while (str[i]) {
  75.         if (ch_str(sep, str[i]) != -1) {
  76.             if (in_word) {
  77.                 if (current < min_len) {
  78.                     min_len = current;
  79.                 }
  80.                 current = 0;
  81.                 in_word = 0;
  82.             }
  83.         }
  84.         else {
  85.             current++;
  86.             in_word = 1;
  87.         }
  88.         i++;
  89.     }
  90.     if (in_word) {
  91.         if (current < min_len) {
  92.             min_len = current;
  93.         }
  94.     }
  95.     return min_len;
  96. }
  97.  
  98. void print_words(char* str) {
  99.     char sep[] = "., ()!?\n\t";
  100.     int i = 0, in_word = 1;
  101.     while (str[i]) {
  102.         if (ch_str(sep, str[i]) != -1) {
  103.             if (in_word) {
  104.                 putchar('\n');
  105.                 in_word = 0;
  106.             }
  107.         }
  108.         else {
  109.             putchar(str[i]);
  110.             in_word = 1;
  111.         }
  112.         i++;
  113.     }
  114.     if (in_word) {
  115.         putchar('\n');
  116.     }
  117.     return;
  118. }
  119. char* reverse_words(char* str) {
  120.     char sep[] = "., ()!?\n\t";
  121.     int i = 0, in_word = 1, start = 0;
  122.     while (str[i]) {
  123.         if (ch_str(sep, str[i]) != -1) {
  124.             if (in_word) {
  125.                 reverse_sub(str, start, i - 1);
  126.                 in_word = 0;
  127.             }
  128.         }
  129.         else {
  130.             if (!in_word) {
  131.                 start = i;
  132.             }
  133.             in_word = 1;
  134.         }
  135.         i++;
  136.     }
  137.     if (in_word) {
  138.         reverse_sub(str, start, i - 1);
  139.     }
  140.     return str;
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement