Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.20 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* str); // сумма цифр
  14. int sum_of_decimal(char* str); // сумма чисел
  15. char* reverse_str(char* str); // переворот всего массива
  16. char* reverse_substr(char* str, int start, int end); // переворот определенного куска массива через swap
  17. void swap(char* a, char* b);
  18. int longest_word(char* str);
  19. int shorter_word(char* str);
  20. void print_words(char* str);
  21. char* reverse_words(char* str);
  22. char* s_gets(char* str, int buf_size);
  23. char* s_gets_c(char* str, int buf_size);
  24. char* remove_dup(char* str);
  25.  
  26.  
  27.  
  28. int main()
  29. {    const int size = 50;
  30.     char str1[size] = "Hello world i am fine!";
  31.     const int SIZE = 50;
  32.     char str2[SIZE] = "It is str2 string";
  33.     const int SiZe = 50;
  34.     char str3[size] = "worldddgkllabcd";
  35.     char ch;
  36.    // s_gets(str1,size);
  37.     //s_gets_c(str1,buf_size);
  38.     //printf("Введите число:");
  39.     //scanf("%s",&ch);
  40.     int start = 0, end = 0;
  41.     //printf("Введите Start:");
  42.    // scanf("%d",&start);
  43.    // printf("Введите end:");
  44.    // scanf("%d",&end);
  45.     //printf("str1_len : %d\n",str1_len(str1));
  46.     //printf("str2_len : %d\n",str2_len(str2));
  47.     //printf("str_cpy: %s\n",str_cpy(str2,str1));
  48.     //printf("str_cat: %s\n",str_cat(str1,str2));
  49.     //printf("str_cmp: %d\n",str_cmp(str1,str2));
  50.     //printf("ch_str:%d\n",ch_str(str1,ch));
  51.     //printf("words_count:%d\n",words_count(str1));
  52.     //printf("sum_of_digits:%d\n",sum_of_digits(str3));
  53.     //printf("sum_of_decimal:%d\n",sum_of_decimal(str3));
  54.     //reverse_str(str1);
  55.     //printf("reverse_substr:%s\n",reverse_substr(str1,start,end));
  56.     //printf("longest_word:%d\n",longest_word(str2));
  57.     //printf("shorter_word:%d\n",shorter_word(str1));
  58.     //printf("str_str:%s\n",str_str(str1,str3));
  59.     //print_words(str1);
  60.     //reverse_words(str1); не рабит(
  61.     printf("remove_dup:%s\n",remove_dup(str3));
  62.     return 0;
  63. }
  64.  
  65.  
  66. int str1_len(char* str1)
  67. {
  68.     int i = 0;
  69.     while(str1[i]){
  70.         i++;
  71.     }
  72.     return i;
  73. }
  74.  
  75. int str2_len(char* str2)
  76. {
  77.     int i = 0;
  78.     while (str2[i]){
  79.         i++;
  80.     }
  81.     return i;
  82. }
  83.  
  84.  
  85. int str3_len(char* str3)
  86. {
  87.     int i = 0;
  88.     while(str3[i]){
  89.         i++;
  90.     }
  91.     return i;
  92. }
  93.  
  94. char* str_cpy(char* str2, char* str1)
  95. {
  96.     for(int j = 0;str2[j];j++){
  97.             str2[j] = 0;
  98.             for(int i = 0; str1[i];i++){
  99.                 str2[i] = str1[i];
  100.         }
  101.     }
  102.     return str2;
  103. }
  104.  
  105. char* str_cat(char* str1,char* str2)
  106. {
  107.     int i = 0;
  108.     int STr1_len = str1_len(str1);
  109.     str1[STr1_len] = ' ';
  110.     while (str1[i]){
  111.         str1[i + STr1_len] = str2[i];
  112.         i++;
  113.     }
  114.     return str1;
  115. }
  116.  
  117. int str_cmp(char* str1,char* str2)
  118. {
  119.     int i = 0;
  120.     while (str1[i] && str2[i]){
  121.         i++;
  122.     }
  123.     if((str1[i] - str2[i])== 0) return 1;  else return 0;
  124. }
  125.  
  126.  
  127. char* str_str(char* str1,char* str2)
  128. {
  129.     int i = 0, j = 0, flag = 1;
  130.     while(str1[i]) {
  131.         flag = 1;
  132.         while(str2[j]){
  133.             if(str1[i] != str2[j]){
  134.                 flag = 0;
  135.                 break;
  136.             }
  137.             j++;
  138.         }
  139.         if(flag) return str2;
  140.         i++;
  141.     }
  142.     return NULL;
  143. }
  144.  
  145. int ch_str(char* str1, char ch)
  146. {
  147.     int i = 0,n = -1;
  148.     while(str1[i]){
  149.         n++;
  150.         if(str1[i] == ch)
  151.             return n;
  152.         i++;
  153.     }
  154.         return -1;
  155. }
  156.  
  157.  
  158.  
  159. int words_count(char* str1)
  160. {
  161.     int i = 0;
  162.     int counter = 0,word = 0;
  163.     char *dst = ".,-_\n\t!?&' '";
  164.     while(str1[i]) {
  165.         if(ch_str(dst,str1[i]) != -1) {
  166.             if(word) {
  167.                 counter++;
  168.                 word = 0;
  169.             }
  170.         }
  171.         else {
  172.             word = 1;
  173.         }
  174.         i++;
  175.     }
  176.     if (word) counter++;
  177.     return counter;
  178. }
  179.  
  180.  
  181. int sum_of_digits(char* str3)
  182. {
  183.     int sum = 0;
  184.     for(int i = 0; str3[i];i++){
  185.         if(str3[i] >='0' && str3[i]<='9')
  186.         {
  187.             sum +=str3[i] - '0';
  188.         }
  189.     }
  190.     return sum;
  191. }
  192.  
  193. int sum_of_decimal(char* str3)
  194. {
  195.     int sum = 0, counter = 0, i = 0;
  196.     while (str3[i]){
  197.         while(str3[i]>= '0' && str3[i]<= '9'){
  198.             counter = counter * 10 + str3[i] - '0';
  199.             i++;
  200.         }
  201.         i++;
  202.         sum += counter;
  203.         counter= 0;
  204.     }
  205.     return sum;
  206. }
  207.  
  208. char* reverse_str(char* str1)
  209. {
  210.     int i = 0;
  211.     while (str1[i]){
  212.         i++;
  213.     }
  214.     for(int j = i - 1; j >= 0; j--){
  215.         printf("%c",str1[j]);
  216.     }
  217.     printf("\n");
  218.     return 0;
  219. }
  220.  
  221. void swap(char* a, char* b)
  222. {
  223.     char temp = *a;
  224.     *a = *b;
  225.     *b = temp;
  226. }
  227.  
  228.  
  229. char* reverse_substr(char* str1, int start, int end)
  230. {
  231.     while (start <= end){
  232.         swap(&str1[start++],&str1[end--]);
  233.     }
  234.     return str1;
  235. }
  236.  
  237. int longest_word(char* str)
  238. {
  239.     int i = 0,current_task = 0,max_task = 0,word = 0;
  240.     char* dst = ".,-_\n\t!?&5' '";
  241.     while(str[i]){
  242.         if(ch_str(dst,str[i]) != -1) {
  243.             if(word) {
  244.                 if (current_task > max_task)
  245.                 max_task = current_task;
  246.                 current_task = 0;
  247.                 word = 0;
  248.         }
  249.     } else {
  250.         word = 1;
  251.         current_task++;
  252.         }
  253.             i++;
  254.     }
  255.     if(word && current_task > max_task)
  256.         max_task = current_task;
  257.         return max_task;
  258. }
  259.  
  260. int shorter_word(char* str)
  261. {
  262.     int i = 0,current_task = 0,min_task = str1_len(str),word = 0;
  263.     char* dst = ".,-_\n\t!?&5' '";
  264.     while(str[i]){
  265.         if(ch_str(dst,str[i]) != -1) {
  266.             if(word) {
  267.                 if (current_task < min_task)
  268.                     min_task = current_task;
  269.                 current_task = 0;
  270.                 word = 0;
  271.             }
  272.         } else {
  273.             word = 1;
  274.             current_task++;
  275.         }
  276.         i++;
  277.     }
  278.     if(word && current_task < min_task)
  279.         min_task = current_task;
  280.         return min_task;
  281. }
  282.  
  283. void print_words(char* str)
  284. {
  285.     char *dst = ".,-_\n\t!?&' '";
  286.     for(int i = 0;str[i];i++){
  287.         if(ch_str(dst,str[i]) != -1){
  288.             printf("\n");
  289.         } else {
  290.             printf("%c",str[i]);
  291.         }
  292.     }
  293.     printf("\n");
  294. }
  295. char* reverse_words (char* str)
  296. {
  297.     int j = 0, k = str1_len(str) - 1;
  298.     char *dst = ".,-_\n\t!?&' '";
  299.     for(int i = 0;str[i];i++){
  300.         if(ch_str(dst,str[i]) != -1){
  301.             printf("\n");
  302.         } else {
  303.             for(j,k;j < k;j++,k--){
  304.                 int temp = str[j];
  305.                 str[j] = str[k];
  306.                 str[k] = temp;
  307.             }
  308.                 printf("%c",str[i]);
  309.             }
  310.         }
  311.     printf("\n");
  312.     return str;
  313. }
  314.  
  315. char* s_gets(char* str, int buf_size){
  316.     int i = 0;
  317.     if(!fgets(str,buf_size,stdin))
  318.         return NULL;
  319.     while(str[i]){
  320.         if(str[i] == '\n'){
  321.             break;
  322.         }
  323.         i++;
  324.     }
  325.     str[i] = '\0';
  326.     while(getchar() != '\n');
  327.     return str;
  328. }
  329.  
  330. char* s_gets_c(char* str, int buf_size){
  331.     int i = 0;
  332.     char ch = '\0';
  333.     while((ch = getchar()) != '\n'){
  334.         if (i < buf_size){
  335.             str[i]= ch;
  336.         }
  337.     }
  338.     str[i] = '\0';
  339.     return str;
  340. }
  341.  
  342. char* remove_dup(char* str)
  343. {
  344.     int i = 0, count = 0, j = 0, flag = 1;
  345.     while(str[i]){
  346.         j = 0;
  347.         flag = 1;
  348.         while(j < count){
  349.             if (str[i] == str[j]){
  350.                 flag = 0;
  351.                 break;
  352.             }
  353.             j++;
  354.         }
  355.         if(flag) {
  356.             str[count] =str[i];
  357.             count++;
  358.         }
  359.         i++;
  360.     }
  361.     str[count] ='\0';
  362.     return str;
  363. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement