Good day to all. I would like to ask a question that, as an inexperienced programmer, I had difficulty. The essence of the task is that the text from the file and the array of delimiters are initially entered from the keyboard. then you need to perform two conversions: the first is to find the entered sequence of delimiters in the text and, if it is found more than three times, the places of all odd lines are interchanged in a circle, the second is to delete all words exceeding the user-specified length limit in odd lines.

At the moment I was able to implement text input and delimiters, search for odd lines and a search function that works successfully with single characters, but cannot find a sequence of delimiters from the array in the text.

#include "stdafx.h" #include<stdio.h> #include<conio.h> void symcount(char, char, int, int); int main(void) { setlocale(LC_ALL, "Russian"); const int numberOfCharactersToRead = 128; char* inputText = (char*)(malloc(sizeof(char) * numberOfCharactersToRead)); FILE *fp; fopen_s(&fp, "D:\\texxxt.txt", "r"); if (fp == NULL) { printf("\nФайл не найден\n"); system("pause"); return 0; } fgets(inputText, numberOfCharactersToRead, fp); printf("Введите последовательность разделителей: "); const int numberOfDelimitersToRead = 6; char* delimiters = (char*)(malloc(sizeof(char) * numberOfDelimitersToRead)); int indexer = 0; for (indexer = 0; indexer < numberOfDelimitersToRead; indexer++) { delimiters[indexer] = getchar(); } printf("\n Выведем нечётные строки для наглядности:"); //Вывод нечетных строк с указанием номеров строк int c; int y = 1; int newline = 1; while ((c = fgetc(fp)) != EOF) { if (y % 2 == 1) { if (newline) { printf("%03d | ", y ); } printf("%c", c ); } newline = (c == '\n'); if (newline) { y++; } } printf("\n"); system("pause"); //Считаем количество разделителей (вот тут и кроется проблема) int count = 0, inx; scanf_s("%p", &inputText); scanf_s("%p", &delimiters); //ищем в тексте разделители for (inx = 0; inputText[inx] != '\0'; inx++) { if (inputText[inx] == delimiters[indexer]) count++; } if (count == 0) printf("\nDeliniters '%p'are not present", delimiters); else printf("\nOccurence of delimiters '%c' : %d", delimiters, count); system("pause"); return 0; } 

I would be very grateful for the proposed ideas. Sorry if my questions seemed too silly :)

  • Specify: 1) you need to search for any of the separators or the exact sequence? 2) if the delimiter is the string ".,!?" , then what to do when finding the sequence "???" - assume that you have found three occurrences or one? - PinkTux
  • 1) The exact sequence that the user enters from the keyboard. 2) I think that three occurrences - Vladislav Lisitsyn
  • Then it is generally not clear what the problem is: man strstr . - PinkTux
  • See addition in the answer. - PinkTux
  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

1 answer 1

 #include <string.h> #include <stdio.h> /* * Возвращает количество разделителей в строке. * Разделители, идущие подряд, считаем одним. * Проще было бы использовать strtok(), но мы не хотим * модифицировать исходную строку. */ static size_t delimiters_count(const char * s, const char * delim) { size_t ndelim = 0; char * token = strpbrk(s, delim); while (token) { ndelim++; /* * Пропускаем разделители, следующие за текущим: */ while (*token && strchr(delim, *token)) { token++; } if (!*token) { break; } token = strpbrk(token, delim); } return ndelim; } /* * Пробуем: */ int main() { /* Должны найти: 1 2 3 4 5 */ const char text[] = "Hello, world!\nLet's dance?"; const char delim[] = " :;,.!?'\n\r\t"; printf("Delimiters found: %zu\n", delimiters_count(text, delim)); return 0; } 

Option for the exact occurrence of the separator (although it is at least strange from a practical point of view):

 #include <string.h> #include <stdio.h> /* * Возвращает количество вхождений заданной подстроки в строку. */ static size_t substring_count(const char * string, const char * substring) { size_t nsubstr = 0; size_t dlen = strlen(substring); char * token = strstr(string, substring); while (token) { nsubstr++; token += dlen; token = strstr(token, substring); } return nsubstr; } /* * Проверяем: */ int main() { /* Должны найти: 12 3 4 */ char text[] = "Hello, world! Let's dance?"; char substring[] = " "; printf("Substrings found: %zu\n", substring_count(text, substring)); return 0; }