Advertisement
Levii_Valenok

Untitled

Dec 2nd, 2020
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.89 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <malloc.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <stdbool.h>
  7. #include <math.h>
  8.  
  9. void replaceWithSpaces(char* Position, int number, int begining);
  10. void shiftToTheLeft(char* row, int start, int length);
  11. int getIntegerValue();
  12. char** allocateArray(int n, int m);
  13. void enterText(char** string, int n, int m);
  14. void displayString(char** string, int n);
  15. int converStringToInt(char* string, int start, int end);
  16. char* stringConnection(char* dest, const char* src);
  17. void convertIntToString(char* string, int number);
  18. int main()
  19. {
  20.     char** string;
  21.     int n, m, state;
  22.     int len, lengthString = 0;
  23.     printf("Enter a number of strings: \n");
  24.     n = getIntegerValue();
  25.  
  26.     printf("\nEnter a number of symbols in string: \n");
  27.     m = getIntegerValue();
  28.  
  29.     string = allocateArray(n, m);
  30.     enterText(string, n, m);
  31.  
  32.     displayString(string, n);
  33.  
  34.     //delete words less than 4 elements
  35.     for (int i = 0; i < n; i++)
  36.     {
  37.         int wordLength = 0;
  38.         for (int j = 0; j < m + 1; j++)
  39.         {
  40.  
  41.             if (string[i][j] >= '0' && string[i][j] <= '9')
  42.             {
  43.                 wordLength = 0;
  44.                 continue;
  45.  
  46.             }
  47.             if (string[i][j] == ' ' || string[i][j] == '\n'
  48.                 || string[i][j] == '\t' || string[i][j] == '\0')
  49.             {
  50.                 if (wordLength < 4 && wordLength > 0)
  51.                 {
  52.                     shiftToTheLeft(string[i], j + 1, wordLength + 1);
  53.                     j = j - (wordLength + 1);
  54.                 }
  55.  
  56.                 if (string[i][j] == '\0')
  57.                 {
  58.                     break;
  59.                 }
  60.  
  61.                 wordLength = 0;
  62.                 continue;
  63.             }
  64.  
  65.  
  66.             wordLength++;
  67.         }
  68.     }
  69.  
  70.     //find numbers and find a sum
  71.  
  72.     char* numbers = "";
  73.     char* finalString = "";
  74.    
  75.     int posStart = 0;
  76.     int posEnd = 0;
  77.     int sum = 0;
  78.     //int* lengthNumber = (int*)malloc(n * sizeof(int));
  79.    
  80.     for (int i = 0; i < n; i++)
  81.     {
  82.         sum = 0;
  83.         int newLength = getLength(string[i]);
  84.         for (int j = 0; j < getLength(string[i]); j++)
  85.         {
  86.             if (string[i][j] >= '0' && string[i][j] <= '9')
  87.             {
  88.                 posStart = j;
  89.                 while (string[i][j] >= '0' && string[i][j] <= '9')
  90.                 {
  91.                     j++;
  92.  
  93.                 }
  94.                 posEnd = j - 1;
  95.                 sum += converStringToInt(string[i], posStart, posEnd);
  96.  
  97.             }
  98.         }
  99.         numbers = (char*)malloc((m) * sizeof(char));
  100.         convertIntToString(numbers, sum);
  101.         //sprintf(numbers,"%d", sum); //write a full function!! DON'T FORGET!!!!!!!!!!
  102.         //finalString = stringConnection(string[i], numbers);
  103.         string[i] = stringConnection(string[i], numbers);
  104.         //lengthNumber = getLength(numbers);
  105.         printf("Sum is %d\n", sum);
  106.         //puts(finalString);
  107.         puts(string[i]);
  108.     }
  109.    
  110.     printf("\nNew text is: \n");
  111.     for (int i = 0; i < n; i++)
  112.     {
  113.         puts(string[i]);
  114.     }
  115.  
  116.     //delete spaces
  117.     for (int i = 0; i < n; i++)
  118.     {
  119.         for (int j = 0; j < (getLength(string[i]) + 1); j++)
  120.         {
  121.             if (string[i][j] == ' ')
  122.             {
  123.                 shiftToTheLeft(string[i], j + 1, 1);
  124.                 j = j - 1;
  125.             }
  126.             if (string[i][j] == '\n')
  127.             {
  128.                 string[i][j] = ' ';
  129.             }
  130.             if (string[i][j] == '\0')
  131.             {
  132.                 /*string[i][j - lengthNumber] = ' ';*/
  133.                 break;
  134.             }
  135.         }
  136.     }
  137.  
  138.     displayString(string, n);
  139.  
  140.     for (int i = 0; i < n; i++)
  141.     {
  142.         free(string[i]);
  143.     }
  144.     free(string);
  145.     return 0;
  146. }
  147.  
  148. int getIntegerValue()
  149. {
  150.     char c;
  151.     int n;
  152.     while ((scanf_s("%d%c", &n, &c) != 2 || c != '\n') && (n < 0))
  153.     {
  154.         printf("You entered incorrect value. Please try again: \n");
  155.         rewind(stdin);
  156.     }
  157.  
  158.     return n;
  159. }
  160.  
  161. int getLength(char* word)
  162. {
  163.     int k = 0;
  164.     while (*(word + k) != '\0')
  165.     {
  166.         ++k;
  167.     }
  168.     return k;
  169. }
  170.  
  171. void replaceWithSpaces(char* position, int number, int begining)
  172. {
  173.     int b = 0;
  174.     for (b = begining; b > begining - number; b--)
  175.     {
  176.         *(position + b) == ' ';
  177.     }
  178. }
  179. void shiftToTheLeft(char* row, int start, int length)
  180. {
  181.     int rowLength = getLength(row);
  182.     for (int i = start; i < rowLength + 1; i++)
  183.     {
  184.         *(row + i - length) = *(row + i);
  185.     }
  186.     //row = (char*)realloc(row, (rowLength + 1 - (length + 1)) * sizeof(char));
  187.  
  188.     //row[rowLength + 1 - length] = '\0';
  189.  
  190. }
  191. char** allocateArray(int n, int m)
  192. {
  193.     char** strings;
  194.     if (!(strings = (char**)malloc(n * sizeof(char*))))
  195.     {
  196.         printf("\nOoops,memory is not allocated\n");
  197.     }
  198.  
  199.  
  200.     for (int i = 0; i < n; i++)
  201.     {
  202.         strings[i] = (char*)calloc((m + 1), sizeof(char)); // including '\0'
  203.         if (strings[i] == NULL)
  204.         {
  205.             printf("\nOoops, memory is not allocated\n");
  206.             for (int j = 0; j < i; j++)
  207.             {
  208.                 free(strings[i]);
  209.             }
  210.             free(strings);
  211.             strings = NULL;
  212.         }
  213.     }
  214.     return strings;
  215. }
  216. void enterText(char** string, int n, int m)
  217. {
  218.     printf("Enter a string with %d strings and %d symbols: \n", n, m);
  219.     rewind(stdin);
  220.     for (int i = 0; i < n; i++)
  221.     {
  222.         fgets(string[i], m + 1, stdin);
  223.         rewind(stdin);
  224.     }
  225. }
  226. void displayString(char** string, int n)
  227. {
  228.     printf("\nThe text you entered is: \n");
  229.     for (int i = 0; i < n; i++)
  230.     {
  231.         puts(string[i]);
  232.     }
  233. }
  234. int converStringToInt(char* string, int start, int end)
  235. {
  236.     int temp = 0;
  237.     int stepen = 0;
  238.     int number = 0;
  239.     for (int i = end; i >= start; i--)
  240.     {
  241.         temp = string[i] - '0';
  242.         stepen = end - i;
  243.         number += temp * pow(10, stepen);
  244.     }
  245.     return number;
  246. }
  247.  
  248. char* stringConnection(char* dest, const char* src)
  249. {
  250.     int i, j;
  251.     int lengthString1 = getLength(dest);
  252.     int lengthString2 = getLength(src);
  253.     dest = (char*)realloc(dest, (lengthString1 + lengthString2 + 10) * sizeof(char));
  254.     for (i = lengthString1 - 1; i < lengthString1 + lengthString2 + 1; i++)
  255.     {
  256.         for (j = 0; j < lengthString2 + 1; j++)
  257.         {
  258.             if (src[j] == '\0')
  259.             {
  260.                 break;
  261.             }
  262.             dest[i + 1] = src[j];
  263.             i++;
  264.            
  265.         }
  266.     }
  267.  
  268.     dest[lengthString1 + lengthString2] = '\0';
  269.  
  270.     return dest;
  271.  
  272. }
  273. void convertIntToString(char* string, int number)
  274. {
  275.    
  276.     string = (char*)malloc(10 * sizeof(char)); //amount of numerals
  277.     int v = 0;
  278.     while (number > 9)
  279.     {
  280.         string[v++] = (number % 10) + '0';
  281.         number = number / 10;
  282.     }
  283.     string[v++] = number + '0';
  284.     string[v] = '\0';
  285.     char temp;
  286.     for (int i = 0; i < v / 2; i++)
  287.     {
  288.         temp = string[i];
  289.         string[i] = string[v - 1 - i];
  290.         string[v - 1 - i] = temp;
  291.     }
  292.     v = 0;
  293. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement