Advertisement
Guest User

Untitled

a guest
Dec 14th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.93 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <malloc.h>
  5. #include <locale.h>
  6. //создаем динамическую строку любого размера
  7. char* createString() {
  8.     char* string = (char*)malloc(sizeof(char));
  9.     int length = 0;
  10.     char symbol;
  11.     symbol = getc(stdin);
  12.     while (symbol != '\0' && symbol != '\n') {
  13.         length++;
  14.         realloc(string, length * sizeof(char));
  15.         string[length - 1] = symbol;
  16.         symbol = getc(stdin);
  17.     }
  18.     string[length] = '\0';
  19.     return string;
  20. }
  21.  
  22. //проверка на буквы
  23. bool isLetter(char symbol) {
  24.     return ((symbol >= 'A' && symbol <= 'Z') || (symbol >= 'a' && symbol <= 'z'));
  25. }
  26.  
  27. //измеряем длинну слова
  28. int wordLength(char* string, int start) {
  29.     int length = 0;
  30.     while (isLetter(string[start + length]))
  31.         length++;
  32.     return length;
  33. }
  34.  
  35. //идем к следующему слову
  36. int nextWordStart(char* string, int lastWordEnd) {
  37.     lastWordEnd++;
  38.     while (!isLetter(string[lastWordEnd]) && string[lastWordEnd] != '\0')
  39.         lastWordEnd++;
  40.     return lastWordEnd;
  41. }
  42.  
  43. //проверка на последнее слово
  44. bool isLastWord(char* string, int start) {
  45.     start += wordLength(string, start);
  46.     if (string[start] == '\0')
  47.         return true;
  48.     else
  49.         return false;
  50. }
  51.  
  52. void main()
  53. {
  54.     int K = 0;
  55.     char newSymbol;
  56.     scanf("%d %c \n", &K, &newSymbol);
  57.     char* string = createString();
  58.     int index = nextWordStart(string, wordLength(string, 0) - 1); //пропускаем первое слово
  59.                                                        
  60.     while (!isLastWord(string, index)) {
  61.         int currentWordLength = wordLength(string, index);
  62.         if (currentWordLength > K) {
  63.             index += K - 1;
  64.             for (int i = K; i < currentWordLength; i++) {
  65.                 index++;
  66.                 string[index] = newSymbol;
  67.             }
  68.         }
  69.         index = nextWordStart(string, index);
  70.     }
  71.  
  72.     index = 0;
  73.     while (string[index] != '\0') {
  74.         printf("%c", string[index]);
  75.         index++;
  76.     }
  77.     scanf("%d", &index);
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement