Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #define DEFAULT_LEN 1000
- void process_string(char * str, int len_str)
- {
- char words[30][12] = {0}; // заполним нулями
- int cnt = 0, j = 0; // cnt - количество слов, j - длина текущего слова
- // разбиваем по словам
- for(int i=0; i < len_str; ++i)
- {
- if ((str[i] == ' ' || str[i] == '.') && i > 0 && str[i-1] != ' ')
- {
- cnt ++;
- j = 0;
- }
- else
- {
- if (str[i] != ' ')
- {
- words[cnt][j] = str[i];
- ++j;
- }
- }
- }
- // обработка и вывод по словам
- printf("Words:\n");
- for(int i=0; i<cnt-1; ++i)
- {
- if ( strcmp(words[i], words[cnt-1]) ) // strcmp = 0, если строки равны
- {
- int m = strlen(words[i]); // длина строки
- // преобразование строки
- char first = words[i][0];
- for(int j=0; j<m-1; ++j)
- words[i][j] = words[i][j+1];
- words[i][m-1] = first;
- // вывод
- printf("%s\n", words[i]);
- }
- }
- }
- // прочитать строку
- int read_string(char * str)
- {
- int str_size = DEFAULT_LEN;
- int len_str = 0;
- while(1)
- {
- str[len_str] = getchar();
- ++len_str;
- if (len_str == str_size - 1)
- { // если размер строки привысил выделенный размер, то выделить еще 500
- str_size += DEFAULT_LEN / 2;
- str = (char *) realloc(str, str_size);
- }
- if (str[len_str-1] == '.') // если ".", то закончить ввод
- {
- str[len_str] = 0; // отмечаем, что строка окончилась
- str = (char *) realloc(str, len_str + 1); // освобождаем ненужную память
- break;
- }
- }
- return len_str;
- }
- int main()
- {
- char * str = (char *) calloc(DEFAULT_LEN, sizeof(char));;
- int len_str = read_string(str);
- // вызов функции обработки строки
- process_string(str, len_str);
- // вывод исходной строки
- printf("\nSource string: %s\n\n", str);
- free(str);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement