Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- char* reverse_sub(char* str, int start, int end) {
- while (start < end) {
- swap(str + start++, str + end--);
- }
- return str;
- }
- char* reverse(char* str) {
- return reverse_sub(str, 0, strlen(str) - 1);
- }
- int ch_str(char* str, char ch) {
- int i = 0;
- while (str[i]) {
- if (str[i] == ch) {
- return i;
- }
- i++;
- }
- return -1;
- }
- int words_count(char* str) {
- char sep[] = "., ()!?\n\t";
- int i = 0, count = 0, in_word = 1;
- while (str[i]) {
- if (ch_str(sep, str[i]) != -1) {
- if (in_word) {
- count++;
- in_word = 0;
- }
- }
- else {
- in_word = 1;
- }
- i++;
- }//hello world\0
- if (in_word) {
- count++;
- }
- return count;
- }
- int longest(char* str) {
- char sep[] = "., ()!?\n\t";
- int i = 0, max_len = 0, current = 0, in_word = 1;
- while (str[i]) {
- if (ch_str(sep, str[i]) != -1) {
- if (in_word) {
- if (current > max_len) {
- max_len = current;
- }
- current = 0;
- in_word = 0;
- }
- }
- else {
- current++;
- in_word = 1;
- }
- i++;
- }
- if (in_word) {
- if (current > max_len) {
- max_len = current;
- }
- }
- return max_len;
- }
- int shortest(char* str) {
- char sep[] = "., ()!?\n\t";
- int i = 0, min_len = INT_MAX, current = 0, in_word = 1;
- while (str[i]) {
- if (ch_str(sep, str[i]) != -1) {
- if (in_word) {
- if (current < min_len) {
- min_len = current;
- }
- current = 0;
- in_word = 0;
- }
- }
- else {
- current++;
- in_word = 1;
- }
- i++;
- }
- if (in_word) {
- if (current < min_len) {
- min_len = current;
- }
- }
- return min_len;
- }
- void print_words(char* str) {
- char sep[] = "., ()!?\n\t";
- int i = 0, in_word = 1;
- while (str[i]) {
- if (ch_str(sep, str[i]) != -1) {
- if (in_word) {
- putchar('\n');
- in_word = 0;
- }
- }
- else {
- putchar(str[i]);
- in_word = 1;
- }
- i++;
- }
- if (in_word) {
- putchar('\n');
- }
- return;
- }
- char* reverse_words(char* str) {
- char sep[] = "., ()!?\n\t";
- int i = 0, in_word = 1, start = 0;
- while (str[i]) {
- if (ch_str(sep, str[i]) != -1) {
- if (in_word) {
- reverse_sub(str, start, i - 1);
- in_word = 0;
- }
- }
- else {
- if (!in_word) {
- start = i;
- }
- in_word = 1;
- }
- i++;
- }
- if (in_word) {
- reverse_sub(str, start, i - 1);
- }
- return str;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement