Advertisement
Guest User

Untitled

a guest
Nov 19th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.90 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3.  
  4. int str1_len(char* str1);
  5. int str2_len(char* str2);
  6. int str3_len(char* str3);
  7. char* str_cpy(char* str1, char* str2); //скопировать str1 в str2 строку;
  8. char* str_cat(char* str2,char* str); //записать вторую строку в первую
  9. int str_cmp(char* str2,char* str); // сравнение строк (вычитать символы 1 из другой
  10. char* str_str(char* str,char* str2); //поиск строки в строке
  11. int words_count(char* str1); // подсчет колличества слов
  12. int ch_str(char* str1, char ch); // кол-во символов в строке
  13. int sum_of_digits(char* str3); // сумма цифр
  14. int sum_of_decimal(char* str3); // сумма чисел
  15. char* reverse_str(char* str); // переворот всего массива
  16. char* reverse_substr(char* str, int start, int end); // переворот определенного куска массива
  17. void swap(char* a, char* b);
  18. int longest_word(char* str2);
  19. int shorter_word(char* str2);
  20. void print_words(char* str);
  21. char* reverse_words(char* str);
  22. char* s_gets(char* str, int buf_size);
  23.  
  24.  
  25.  
  26. int main()
  27. {    const int size = 50;
  28.     char str1[size] = "Hello world i am fine";
  29.     const int SIZE = 50;
  30.     char str2[SIZE] = "It is str2 string";
  31.     const int SiZe = 50;
  32.     char str3[size] = "Bomonka 12345 45689";
  33.     char ch;
  34.     printf("Введите число:");
  35.     scanf("%s",&ch);
  36.     int start = 0, end = 0;
  37.     printf("Введите Start:");
  38.     scanf("%d",&start);
  39.     printf("Введите end:");
  40.     scanf("%d",&end);
  41.     //printf("str1_len : %d\n",str1_len(str1));
  42.     //printf("str2_len : %d\n",str2_len(str2));
  43.     //printf("str_cpy: %s\n",str_cpy(str2,str1));
  44.     //printf("str_cat: %s\n",str_cat(str1,str2));
  45.     //printf("str_cmp: %d\n",str_cmp(str1,str2));
  46.     //printf("ch_str:%d\n",ch_str(str1,ch));
  47.     //printf("words_count:%d\n",words_count(str1));
  48.     //printf("sum_of_digits:%d\n",sum_of_digits(str3));
  49.     //printf("sum_of_decimal:%d\n",sum_of_decimal(str3));
  50.     //reverse_str(str1); // не рабит(
  51.     //printf("reverse_substr:%s\n",reverse_substr(str1,start,end));
  52.     printf("longest_word:%d\n",longest_word(str2));
  53.     printf("shorter_word:%d\n",shorter_word(str1));
  54.     return 0;
  55. }
  56.  
  57.  
  58. int str1_len(char* str1)
  59. {
  60.     int i = 0;
  61.     while(str1[i]){
  62.         i++;
  63.     }
  64.     return i;
  65. }
  66.  
  67. int str2_len(char* str2)
  68. {
  69.     int i = 0;
  70.     while (str2[i]){
  71.         i++;
  72.     }
  73.     return i;
  74. }
  75.  
  76.  
  77. int str3_len(char* str3)
  78. {
  79.     int i = 0;
  80.     while(str3[i]){
  81.         i++;
  82.     }
  83.     return i;
  84. }
  85.  
  86. char* str_cpy(char* str2, char* str1)
  87. {
  88.     for(int j = 0;str2[j];j++){
  89.             str2[j] = 0;
  90.             for(int i = 0; str1[i];i++){
  91.                 str2[i] = str1[i];
  92.         }
  93.     }
  94.     return str2;
  95. }
  96.  
  97. char* str_cat(char* str1,char* str2)
  98. {
  99.     int i = 0;
  100.     int STr1_len = str1_len(str1);
  101.     str1[STr1_len] = ' ';
  102.     while (str1[i]){
  103.         str1[i + STr1_len] = str2[i];
  104.         i++;
  105.     }
  106.     return str1;
  107. }
  108.  
  109. int str_cmp(char* str1,char* str2)
  110. {
  111.     int i = 0;
  112.     while (str1[i] && str2[i]){
  113.         i++;
  114.     }
  115.     if((str1[i] - str2[i])== 0) return 1;  else return 0;
  116. }
  117.  
  118.  
  119. int ch_str(char* str1, char ch)
  120. {
  121.     int i = 0,n = -1;
  122.     while(str1[i]){
  123.         n++;
  124.         if(str1[i] == ch)
  125.             return n;
  126.         i++;
  127.     }
  128.         return -1;
  129. }
  130.  
  131.  
  132.  
  133. int words_count(char* str1)
  134. {
  135.     int i = 0;
  136.     int counter = 0,word = 0;
  137.     char *dst = ".,-_\n\t!?&' '";
  138.     while(str1[i]) {
  139.         if(ch_str(dst,str1[i]) != -1) {
  140.             if(word) {
  141.                 counter++;
  142.                 word = 0;
  143.             }
  144.         }
  145.         else {
  146.             word = 1;
  147.         }
  148.         i++;
  149.     }
  150.     if (word) counter++;
  151.     return counter;
  152. }
  153.  
  154.  
  155. int sum_of_digits(char* str3)
  156. {
  157.     int sum = 0;
  158.     for(int i = 0; str3[i];i++){
  159.         if(str3[i] >='0' && str3[i]<='9')
  160.         {
  161.             sum +=str3[i] - '0';
  162.         }
  163.     }
  164.     return sum;
  165. }
  166.  
  167. int sum_of_decimal(char* str3)
  168. {
  169.     int sum = 0, counter = 0, i = 0;
  170.     while (str3[i]){
  171.         while(str3[i]>= '0' && str3[i]<= '9'){
  172.             counter = counter * 10 + str3[i] - '0';
  173.             i++;
  174.         }
  175.         i++;
  176.         sum += counter;
  177.         counter= 0;
  178.     }
  179.     return sum;
  180. }
  181.  
  182. char* reverse_str(char* str1)
  183. {
  184.     int i = 0;
  185.     while (str1[i]){
  186.         i++;
  187.     }
  188.     for(int j = i - 1; j >= 0; j--){
  189.         printf("reverse_str:%d",str1[j]);
  190.     }
  191.     return 0;
  192. }
  193.  
  194. void swap(char* a, char* b)
  195. {
  196.     char temp = *a;
  197.     *a = *b;
  198.     *b = temp;
  199. }
  200.  
  201.  
  202. char* reverse_substr(char* str1, int start, int end)
  203. {
  204.     while (start <= end){
  205.         swap(&str1[start++],&str1[end--]);
  206.     }
  207.     return str1;
  208. }
  209.  
  210. int longest_word(char* str2)
  211. {
  212.     int i = 0,current_task = 0,max_task = 0,word = 0;
  213.     char* dst = ".,-_\n\t!?&5' '";
  214.     while(str2[i]){
  215.         if(ch_str(dst,str2[i]) != -1) {
  216.             if(word) {
  217.                 if (current_task > max_task)
  218.                 max_task = current_task;
  219.                 current_task = 0;
  220.                 word = 0;
  221.         }
  222.     } else {
  223.         word = 1;
  224.         current_task++;
  225.         }
  226.             i++;
  227.     }
  228.     if(word && current_task > max_task)
  229.         max_task = current_task;
  230.         return max_task;
  231. }
  232.  
  233. int shorter_word(char* str)
  234. {
  235.     int i = 0,current_task = 0,min_task = 0,word = 0;
  236.     char* dst = ".,-_\n\t!?&5' '";
  237.     while(str[i]){
  238.         if(ch_str(dst,str[i]) != -1) {
  239.             if(word) {
  240.                 if (current_task < min_task)
  241.                     min_task = current_task;
  242.                 current_task = 0;
  243.                 word = 0;
  244.             }
  245.         } else {
  246.             word = 1;
  247.             current_task++;
  248.         }
  249.         i++;
  250.     }
  251.     if(word && current_task < min_task)
  252.         min_task = current_task;
  253.         return min_task;
  254. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement