Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdio.h>
- #include <malloc.h>
- #include <stdlib.h>
- #include <string.h>
- #include <stdbool.h>
- #include <math.h>
- void replaceWithSpaces(char* Position, int number, int begining);
- void shiftToTheLeft(char* row, int start, int length);
- int getIntegerValue();
- char** allocateArray(int n, int m);
- void enterText(char** string, int n, int m);
- void displayString(char** string, int n);
- int converStringToInt(char* string, int start, int end);
- char* stringConnection(char* dest, const char* src);
- void convertIntToString(char* string, int number);
- int main()
- {
- char** string;
- int n, m, state;
- int len, lengthString = 0;
- printf("Enter a number of strings: \n");
- n = getIntegerValue();
- printf("\nEnter a number of symbols in string: \n");
- m = getIntegerValue();
- string = allocateArray(n, m);
- enterText(string, n, m);
- displayString(string, n);
- //delete words less than 4 elements
- for (int i = 0; i < n; i++)
- {
- int wordLength = 0;
- for (int j = 0; j < m + 1; j++)
- {
- if (string[i][j] >= '0' && string[i][j] <= '9')
- {
- wordLength = 0;
- continue;
- }
- if (string[i][j] == ' ' || string[i][j] == '\n'
- || string[i][j] == '\t' || string[i][j] == '\0')
- {
- if (wordLength < 4 && wordLength > 0)
- {
- shiftToTheLeft(string[i], j + 1, wordLength + 1);
- j = j - (wordLength + 1);
- }
- if (string[i][j] == '\0')
- {
- break;
- }
- wordLength = 0;
- continue;
- }
- wordLength++;
- }
- }
- //find numbers and find a sum
- char* numbers = "";
- char* finalString = "";
- int posStart = 0;
- int posEnd = 0;
- int sum = 0;
- //int* lengthNumber = (int*)malloc(n * sizeof(int));
- for (int i = 0; i < n; i++)
- {
- sum = 0;
- int newLength = getLength(string[i]);
- for (int j = 0; j < getLength(string[i]); j++)
- {
- if (string[i][j] >= '0' && string[i][j] <= '9')
- {
- posStart = j;
- while (string[i][j] >= '0' && string[i][j] <= '9')
- {
- j++;
- }
- posEnd = j - 1;
- sum += converStringToInt(string[i], posStart, posEnd);
- }
- }
- numbers = (char*)malloc((m) * sizeof(char));
- convertIntToString(numbers, sum);
- //sprintf(numbers,"%d", sum); //write a full function!! DON'T FORGET!!!!!!!!!!
- //finalString = stringConnection(string[i], numbers);
- string[i] = stringConnection(string[i], numbers);
- //lengthNumber = getLength(numbers);
- printf("Sum is %d\n", sum);
- //puts(finalString);
- puts(string[i]);
- }
- printf("\nNew text is: \n");
- for (int i = 0; i < n; i++)
- {
- puts(string[i]);
- }
- //delete spaces
- for (int i = 0; i < n; i++)
- {
- for (int j = 0; j < (getLength(string[i]) + 1); j++)
- {
- if (string[i][j] == ' ')
- {
- shiftToTheLeft(string[i], j + 1, 1);
- j = j - 1;
- }
- if (string[i][j] == '\n')
- {
- string[i][j] = ' ';
- }
- if (string[i][j] == '\0')
- {
- /*string[i][j - lengthNumber] = ' ';*/
- break;
- }
- }
- }
- displayString(string, n);
- for (int i = 0; i < n; i++)
- {
- free(string[i]);
- }
- free(string);
- return 0;
- }
- int getIntegerValue()
- {
- char c;
- int n;
- while ((scanf_s("%d%c", &n, &c) != 2 || c != '\n') && (n < 0))
- {
- printf("You entered incorrect value. Please try again: \n");
- rewind(stdin);
- }
- return n;
- }
- int getLength(char* word)
- {
- int k = 0;
- while (*(word + k) != '\0')
- {
- ++k;
- }
- return k;
- }
- void replaceWithSpaces(char* position, int number, int begining)
- {
- int b = 0;
- for (b = begining; b > begining - number; b--)
- {
- *(position + b) == ' ';
- }
- }
- void shiftToTheLeft(char* row, int start, int length)
- {
- int rowLength = getLength(row);
- for (int i = start; i < rowLength + 1; i++)
- {
- *(row + i - length) = *(row + i);
- }
- //row = (char*)realloc(row, (rowLength + 1 - (length + 1)) * sizeof(char));
- //row[rowLength + 1 - length] = '\0';
- }
- char** allocateArray(int n, int m)
- {
- char** strings;
- if (!(strings = (char**)malloc(n * sizeof(char*))))
- {
- printf("\nOoops,memory is not allocated\n");
- }
- for (int i = 0; i < n; i++)
- {
- strings[i] = (char*)calloc((m + 1), sizeof(char)); // including '\0'
- if (strings[i] == NULL)
- {
- printf("\nOoops, memory is not allocated\n");
- for (int j = 0; j < i; j++)
- {
- free(strings[i]);
- }
- free(strings);
- strings = NULL;
- }
- }
- return strings;
- }
- void enterText(char** string, int n, int m)
- {
- printf("Enter a string with %d strings and %d symbols: \n", n, m);
- rewind(stdin);
- for (int i = 0; i < n; i++)
- {
- fgets(string[i], m + 1, stdin);
- rewind(stdin);
- }
- }
- void displayString(char** string, int n)
- {
- printf("\nThe text you entered is: \n");
- for (int i = 0; i < n; i++)
- {
- puts(string[i]);
- }
- }
- int converStringToInt(char* string, int start, int end)
- {
- int temp = 0;
- int stepen = 0;
- int number = 0;
- for (int i = end; i >= start; i--)
- {
- temp = string[i] - '0';
- stepen = end - i;
- number += temp * pow(10, stepen);
- }
- return number;
- }
- char* stringConnection(char* dest, const char* src)
- {
- int i, j;
- int lengthString1 = getLength(dest);
- int lengthString2 = getLength(src);
- dest = (char*)realloc(dest, (lengthString1 + lengthString2 + 10) * sizeof(char));
- for (i = lengthString1 - 1; i < lengthString1 + lengthString2 + 1; i++)
- {
- for (j = 0; j < lengthString2 + 1; j++)
- {
- if (src[j] == '\0')
- {
- break;
- }
- dest[i + 1] = src[j];
- i++;
- }
- }
- dest[lengthString1 + lengthString2] = '\0';
- return dest;
- }
- void convertIntToString(char* string, int number)
- {
- string = (char*)malloc(10 * sizeof(char)); //amount of numerals
- int v = 0;
- while (number > 9)
- {
- string[v++] = (number % 10) + '0';
- number = number / 10;
- }
- string[v++] = number + '0';
- string[v] = '\0';
- char temp;
- for (int i = 0; i < v / 2; i++)
- {
- temp = string[i];
- string[i] = string[v - 1 - i];
- string[v - 1 - i] = temp;
- }
- v = 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement